splitting a string into an array in C++ without using vector

前端 未结 5 1608
轮回少年
轮回少年 2020-11-28 10:53

I am trying to insert a string separated by spaces into an array of strings without using vector in C++. For example:

using namespace std;
int main(         


        
相关标签:
5条回答
  • 2020-11-28 11:07
    #include <iostream>
    #include <sstream>
    #include <iterator>
    #include <string>
    
    using namespace std;
    
    template <size_t N>
    void splitString(string (&arr)[N], string str)
    {
        int n = 0;
        istringstream iss(str);
        for (auto it = istream_iterator<string>(iss); it != istream_iterator<string>() && n < N; ++it, ++n)
            arr[n] = *it;
    }
    
    int main()
    {
        string line = "test one two three.";
        string arr[4];
    
        splitString(arr, line);
    
        for (int i = 0; i < 4; i++)
           cout << arr[i] << endl;
    }
    
    0 讨论(0)
  • 2020-11-28 11:07

    Here's a suggestion: use two indices into the string, say start and end. start points to the first character of the next string to extract, end points to the character after the last one belonging to the next string to extract. start starts at zero, end gets the position of the first char after start. Then you take the string between [start..end) and add that to your array. You keep going until you hit the end of the string.

    0 讨论(0)
  • 2020-11-28 11:11
    #define MAXSPACE 25
    
    string line =  "test one two three.";
    string arr[MAXSPACE];
    string search = " ";
    int spacePos;
    int currPos = 0;
    int k = 0;
    int prevPos = 0;
    
    do
    {
    
        spacePos = line.find(search,currPos);
    
        if(spacePos >= 0)
        {
    
            currPos = spacePos;
            arr[k] = line.substr(prevPos, currPos - prevPos);
            currPos++;
            prevPos = currPos;
            k++;
        }
    
    
    }while( spacePos >= 0);
    
    arr[k] = line.substr(prevPos,line.length());
    
    for(int i = 0; i < k; i++)
    {
       cout << arr[i] << endl;
    }
    
    0 讨论(0)
  • 2020-11-28 11:16

    It is possible to turn the string into a stream by using the std::stringstream class (its constructor takes a string as parameter). Once it's built, you can use the >> operator on it (like on regular file based streams), which will extract, or tokenize word from it:

    #include <iostream>
    #include <sstream>
    
    using namespace std;
    
    int main(){
        string line = "test one two three.";
        string arr[4];
        int i = 0;
        stringstream ssin(line);
        while (ssin.good() && i < 4){
            ssin >> arr[i];
            ++i;
        }
        for(i = 0; i < 4; i++){
            cout << arr[i] << endl;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 11:26
    #include <iostream>
    #include <sstream>
    #include <vector>
    using namespace std;
    
    int main() {
    
        string s1="split on     whitespace";
        istringstream iss(s1);
        vector<string> result;
        for(string s;iss>>s;)
            result.push_back(s);
        int n=result.size();
        for(int i=0;i<n;i++)
            cout<<result[i]<<endl;
        return 0;
    }
    

    Output:-

    split
    on
    whitespace

    0 讨论(0)
提交回复
热议问题