Crez.晔霖 发表于 2019-11-2 18:38:48

C++:求助vector<string> 数组取元素个数返回0???

想用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这个数组的时候,返回了0
int main()
{
        string ABCD="A|B|C|D";
        vector<string> ABC;
        String_SplitString(ABCD, "|",ABC);
        cout << sizeof(ABC)/sizeof(ABC);
        return 0;
}
各位江湖高手,请问我出了什么问题,感谢各位大佬抽出你们的宝贵时间来看我的问题,希望给予解答,万分感谢!!

Crez.晔霖 发表于 2019-11-2 18:40:05

个人习惯,喜欢先把参数的值赋给变量

Crez.晔霖 发表于 2019-11-2 18:42:09

补充下,我试着在循环中用【cout<<Part_SubString】来输出所找到的,能正常返回ABCD四个字母

人造人 发表于 2019-11-2 20:18:42

给出完整的代码

Crez.晔霖 发表于 2019-11-2 20:55:12

人造人 发表于 2019-11-2 20:18
给出完整的代码

给了,上面的就是啊{:5_110:}

人造人 发表于 2019-11-2 21:38:01

Crez.晔霖 发表于 2019-11-2 20:55
给了,上面的就是啊

给出完整的代码是什么意思?

Crez.晔霖 发表于 2019-11-2 22:21:53

#include<iostream>
#include<string.h>
#include<string>
#include<Windows.h>
#include<algorithm>
#include<vector>
using namespace std;
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;
}
int main()
{
        string ABCD = "A|B|C|D";
        vector<string> ABC;
        String_SplitString(ABCD, "|", ABC);
        cout << sizeof(ABC) / sizeof(ABC);
        return 0;
}

python万 发表于 2019-11-3 14:59:26

不懂

Crez.晔霖 发表于 2019-11-4 21:40:00

问题已经得到解决,源代码附上:
#include<iostream>
#include<string>
#include<vector>
vector<string> String_SplitString(string WholeString, string SplitSymbol)
{
        size_t Position_SplitSymbol;
        vector<string> Result;
        string Part_WholeString = WholeString;
        string Part_SplitSymbol = SplitSymbol;
        Part_WholeString += Part_SplitSymbol;
        int StringLength = Part_WholeString.length();
        for (int i = 0; i < StringLength; i++)
        {
                Position_SplitSymbol = Part_WholeString.find(Part_SplitSymbol, i);
                if (Position_SplitSymbol < StringLength)
                {
                        string Part_SubString = Part_WholeString.substr(i, Position_SplitSymbol - i);
                        Result.push_back(Part_SubString);
                        i = Position_SplitSymbol + Part_SplitSymbol.length() - 1;
                }
        }
        return Result;
}
亲测可用哦{:5_109:}

Crez.晔霖 发表于 2019-11-4 21:41:23

就是自己得到解决应该怎么取消啊,没有最佳答案......
页: [1]
查看完整版本: C++:求助vector<string> 数组取元素个数返回0???