【问题标题】:splitting a string into an array in C++ without using vector在不使用向量的情况下将字符串拆分为 C++ 中的数组
【发布时间】:2013-04-08 09:39:20
【问题描述】:

我正在尝试使用 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;
    }
}

我希望输出是:

test
one
two
three.

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

【问题讨论】:

标签: c++ arrays string vector


【解决方案1】:

可以使用std::stringstream 类将字符串转换为流(其构造函数将字符串作为参数)。构建完成后,您可以在其上使用 &gt;&gt; 运算符(就像在基于常规文件的流上一样),它将从中提取或 tokenize 单词:

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

【讨论】:

  • 这个答案很简单,切中要害,更重要的是有效!非常感谢!
  • 它为我拆分了单个字符。
  • Hymm,如果“line”变量的末尾有空格。然后你删除
【解决方案2】:
#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;
}

【讨论】:

    【解决方案3】:
    #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;
    }
    

    【讨论】:

    • string::find 如果没有找到正在搜索的字符串而不是 0,则返回 string::npos
    • std::string::npos 是一个静态成员常量值,对于 size_t 类型的元素具有最大可能值。该常量定义为值 -1,因为 size_t 是无符号整数类型,所以它是该类型的最大可能表示值。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:
      #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;
      }
      

      输出:-

      拆分

      空格

      【讨论】:

      • 点评来源: 您好,请不要只回答源代码。尝试对您的解决方案如何工作提供一个很好的描述。请参阅:How do I write a good answer?。谢谢
      猜你喜欢
      • 1970-01-01
      • 2014-09-29
      • 2012-09-06
      • 2014-01-16
      • 2011-06-20
      • 1970-01-01
      • 2020-04-09
      • 1970-01-01
      相关资源
      最近更新 更多