请问如何实现将string字符串换分隔符生成一个字符串集合

foolflyfish 2003-03-13 11:58:13
举例:

原始字符串 "abc|123|ABC"

要生成一个vector
包含三个元素:
abc 123 ABC
...全文
499 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
THQ 2003-04-03
  • 打赏
  • 举报
回复
Have a look!~
shornmao 2003-03-31
  • 打赏
  • 举报
回复
楼上的,我已经提供了那么简洁、正确、优雅的C++方案,你还拿出这么一大串代码,是为了分数吗?楼主早就忘了给分这一会事情了,你要不到的。
smartpiglet 2003-03-29
  • 打赏
  • 举报
回复
jennyvenus(头大不是我的错,但显摆就是我不对了)
应该改一下, char* string改为char string[],否则VC6运行时会出错;
我觉得用char*不太方便,我提供一个string的版本:

输入为string,分隔符为 char* ,结果保存在vector里

void SplitString( string inStr, char* sep, vector<string>* retStringVector )///分解字符串
{
int _iloop = 0;
int _curpos = -1;
int _oldpos = -1;
string _sTmpstr = "";
string _orgstr = "";
vector<int> _positions;
int _iLen = -1;
_orgstr = inStr;
_curpos = _orgstr.find(sep);

///查找字符串 names 中的所有","索引值并保存到数组

while ( _curpos != -1)
{
_positions.push_back( _curpos);
_curpos = _orgstr.find( sep , _curpos +1 );
}
for( int i = 0; i< _positions.size() ; i++)
{
_iLen = inStr.length();
if( i == 0)
{
_sTmpstr = inStr.substr( 0 , _positions[i]);
}
else
{
_sTmpstr = inStr.substr( _positions[i -1] +1, _positions[i] - (_positions[i-1]+1));
}
retStringVector->push_back(_sTmpstr);
_sTmpstr.empty();
}
_sTmpstr = inStr.substr( _positions[_positions.size()-1] +1, inStr.size() - _positions[_positions.size()-1] );
retStringVector->push_back(_sTmpstr);
_sTmpstr.empty();
_positions.pop_back();///清空
}
blas 2003-03-28
  • 打赏
  • 举报
回复
jennyvenus(头大不是我的错,但显摆就是我不对了) 说得对,就用<cstring>的strtok函数。
cnroky 2003-03-28
  • 打赏
  • 举报
回复
小弟也是初学看这样是否可以?
using namespace std;
string str("ADS|342|d346");
vector<char> Mystr;
string::iterator it;
for(it=str.begin();it!=str.end();it++)
{
if(*it=='|')
Mystr.push_back(' ');
else
Mystr.push_back(*it);
}
ziyue 2003-03-28
  • 打赏
  • 举报
回复
用迭代器之类的看看。。
zhaohangcom 2003-03-17
  • 打赏
  • 举报
回复
`
大家可否交流一下经验。因为我也不太了解关于字符串的操作,总是有一些错误。
例如,经常处理不好“回车键,tab 键”。很苦啊
用户 昵称 2003-03-15
  • 打赏
  • 举报
回复
use strtok function

/* Compile options needed: none
*/

#include <string.h>
#include <stdio.h>

char *string = "a string,of ,,tokens";
char *token;

void main(void)
{
token = strtok(string," ,"); /*There are two delimiters here*/
while (token != NULL){
printf("The token is: %s\n", token);
token = strtok(NULL," ,");
}
}
The output of this program is as follows:
The token is: a


The token is: string


The token is: of


The token is: tokens
fangrk 2003-03-14
  • 打赏
  • 举报
回复
上面错了,一样的。
fangrk 2003-03-14
  • 打赏
  • 举报
回复
win98和win2000下的结果不一样!
fangrk 2003-03-14
  • 打赏
  • 举报
回复
我昨天在家里做的测试,显示的内容是
ADS
342
$##FD
d346
家里是win2000 和 C++Builder 6

刚才用单位的电脑又试了一下,显示内容是
AD S
34 2
$# #FD
d346
单位的电脑是Win98 和 C++Builder 6

我还是中午回家重新看看吧!
shornmao 2003-03-14
  • 打赏
  • 举报
回复
你为什么说不对?我可是测试过代码的哦!
getline读取的时候不会跳过空白的,所有的内容都保留,除非你的字符串里包括'\n',那么后面的内容就不会被getline读取了。
shornmao 2003-03-14
  • 打赏
  • 举报
回复
当然应该是一样的,这是C++标准规定的,如果不一样,只能证明这个版本的标准库和标准不一致。
kicool 2003-03-13
  • 打赏
  • 举报
回复
gz
孩皮妞野 2003-03-13
  • 打赏
  • 举报
回复
www.boost.org/DOCumentation/TOKENIZER
孩皮妞野 2003-03-13
  • 打赏
  • 举报
回复
boost::tokenizer

or code it yourself.
fangrk 2003-03-13
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <sstream>

using namespace std;
int main()
{
string str("AD S|34 2|$# #FD|d346");
stringstream strstr(str);
while (strstr)
{
string substr;
getline(strstr,substr,'|');
cout<<substr<<"\n";
}
return 0;
}

就不对了!
shornmao 2003-03-13
  • 打赏
  • 举报
回复
我来写一段,只要标准库就可以了。
const string str("ADS|342|$##FD|d346");

stringstream strstr(str);
while (strstr)
{
string substr;
getline(strstr,substr,'|');
cout<<substr<<"\n";
}
fangrk 2003-03-13
  • 打赏
  • 举报
回复
胡乱写了一个:

#include <iostream>
#include <string>
#include <vector>

using namespace std;
void tokenizer(const string&,char,vector<string>&);
int main()
{
string str("ADS|342|$##FD|d346");
vector<string> strV;
tokenizer(str,'|',strV);
for(int i=0;i<strV.size();++i)
cout<<strV[i]<<' ';
return 0;
}
void tokenizer(const string& STR,char C,vector<string>& VS)
{
VS.clear();
int f1=-1,f2;
while(1){
f2=STR.find(C,++f1);
if(f2==string::npos) break;
VS.push_back(STR.substr(f1,f2-f1));
f1=f2;
}
VS.push_back(STR.substr(f1,STR.size()-f1));
}
孩皮妞野 2003-03-13
  • 打赏
  • 举报
回复
down下来随便看。

直接在浏览器上一个一个看也行。
加载更多回复(1)

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧