马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
想用C++完成用分割符将string字符串分割成一个个数组,例子:
现在有一个字符串为: A|B|C|D
我想用代码来实现,最后返回一个数组,四个元素分别为“A”、“B”、“C”、“D”void String_SplitString(string WholeString, string SplitSymbol,vector<string>ResultArray) //字符串分割到数组
{
string Part_WholeString = WholeString;
string Part_SplitSymbol = SplitSymbol;
string Part_SubString;
size_t Position_Start=0, Position_SplitSymbol;
vector<string> Part_ResultArray ;
Part_ResultArray.clear();
Position_SplitSymbol = Part_WholeString.find_first_of(Part_SplitSymbol, Position_Start);
do
{
if (Position_SplitSymbol != Part_WholeString.npos)
{
Part_SubString = Part_WholeString.substr(Position_Start, Position_SplitSymbol - Position_Start);
Part_ResultArray.push_back(Part_SubString);
Position_Start = Position_SplitSymbol + Part_SplitSymbol.length();
Position_SplitSymbol = Part_WholeString.find(Part_SplitSymbol, Position_Start);
if (Position_Start == Part_WholeString.npos)
{
break;
}
}
} while (Position_SplitSymbol != Part_WholeString.npos);
Part_SubString = Part_WholeString.substr(Position_Start);
Part_ResultArray.push_back(Part_SubString);
ResultArray = Part_ResultArray;
}
这是我写的代码,但是最后我取ResultArray这个数组的时候,返回了0int main()
{
string ABCD="A|B|C|D";
vector<string> ABC;
String_SplitString(ABCD, "|",ABC);
cout << sizeof(ABC)/sizeof(ABC[0]);
return 0;
}
各位江湖高手,请问我出了什么问题,感谢各位大佬抽出你们的宝贵时间来看我的问题,希望给予解答,万分感谢!! |