在不使用向量的情况下将字符串拆分为 C++ 中的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16029324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:01:03  来源:igfitidea点击:

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

c++arraysstringvector

提问by txp111030

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

我试图将一个由空格分隔的字符串插入到一个字符串数组中,而不在 C++ 中使用向量。例如:

using namespace std;
int main() {
    string line = "test one two three.";
    string arr[4];

    //codes here to put each word in string line into string array arr
    for(int i = 0; i < 4; i++) {
        cout << arr[i] << endl;
    }
}

I want the output to be:

我希望输出是:

test
one
two
three.

I know there are already other questions asking string > array in C++, but I could not find any answer satisfying my conditions: splitting a string into an array WITHOUT using vector.

我知道已经有其他问题在 C++ 中询问字符串 > 数组,但我找不到任何满足我的条件的答案:不使用向量将字符串拆分为数组。

回答by didierc

It is possible to turn the string into a stream by using the std::stringstreamclass (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 tokenizeword from it:

可以使用std::stringstream类将字符串转换为流(其构造函数将字符串作为参数)。构建完成后,您可以>>在其上使用运算符(例如在基于常规文件的流上),它将从中提取或标记单词:

#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;
    }
}

回答by Alexey

#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;
}

回答by Vijendra Singh

#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;
}

回答by Frerich Raabe

Here's a suggestion: use two indices into the string, say startand end. startpoints to the first character of the next string to extract, endpoints to the character after the last one belonging to the next string to extract. startstarts at zero, endgets 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.

这是一个建议:在字符串中使用两个索引,例如startendstart指向下一个要提取的字符串的第一个字符,end指向属于下一个要提取的字符串的最后一个字符之后的字符。start从零开始,end获取start.之后第一个字符的位置。然后您将字符串[start..end)添加到您的数组中。您继续前进,直到碰到字符串的末尾。

回答by Chirag Vij

#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



空白处拆分