【问题标题】:Split a string into an array in C++ [duplicate]在C ++中将字符串拆分为数组[重复]
【发布时间】:2012-01-16 21:41:45
【问题描述】:

可能重复:
How to split a string in C++?

我有一个数据输入文件,每一行都是一个条目。在每一行中,每个“字段”都由一个空格“”分隔,所以我需要按空格分隔行。其他语言有一个名为 split 的函数(C#、PHP 等),但我找不到 C++ 的函数。我怎样才能做到这一点?这是我的代码:

string line;
ifstream in(file);

while(getline(in, line)){

  // Here I would like to split each line and put them into an array

}

【问题讨论】:

    标签: c++ string tokenize


    【解决方案1】:
    #include <sstream>  //for std::istringstream
    #include <iterator> //for std::istream_iterator
    #include <vector>   //for std::vector
    
    while(std::getline(in, line))
    {
        std::istringstream ss(line);
        std::istream_iterator<std::string> begin(ss), end;
    
        //putting all the tokens in the vector
        std::vector<std::string> arrayTokens(begin, end); 
    
        //arrayTokens is containing all the tokens - use it!
    }
    

    顺便说一句,像我一样使用诸如std::getlinestd::ifstream 之类的限定名称。您似乎在代码中的某处写了using namespace std,这被认为是一种不好的做法。所以不要这样做:

    【讨论】:

    • 您能否提供一个链接来讨论为什么使用using namespace x 是一种不好的做法?
    • @jli:将链接添加到我的答案中。看看吧。
    • @Nawaz 谢谢,看看我的其他问题,我使用的语法以及我从 uni 的导师那里学习 C++ 的方式非常值得怀疑:S!!!!!!
    【解决方案2】:
    vector<string> v;
    boost::split(v, line, ::isspace);
    

    http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768

    【讨论】:

      【解决方案3】:

      我已经为我的类似需求编写了一个函数, 也许你可以使用它!

      std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) 
      {
          std::stringstream ss(s+' ');
          std::string item;
          while(std::getline(ss, item, delim)) 
          {
              elems.push_back(item);
          }
          return elems;
      }
      

      【讨论】:

        【解决方案4】:

        试试strtok。在 C++ 参考中查找它:。

        【讨论】:

        • strtok 是一个 C 库的东西,而发帖人询问如何用 C++ 正确地做到这一点。
        • 而 c++ 不是 c?(...... OMG 这么多年他们都在骗我:D)。从什么时候 c 库停止在 c++ 中工作(或变得不正确)?
        • 如果你混合使用它们,你会增加不必要的依赖,以及其他问题。
        • 请提供在 c++ 中使用 c 所涉及的问题和依赖项的链接? ...我的意思是多年来错误地在 c++ 中编译和使用 c 代码和库。
        • stackoverflow.com/questions/4025869/using-mixing-c-in-c-code 我猜依赖关系并不是真正的问题,但是 imo 做类似#include &lt;iostream&gt; #include &lt;cstdio&gt; 的事情变得多余了。
        【解决方案5】:

        以下代码使用strtok() 将字符串拆分为标记并将标记存储在向量中。

        #include <iostream>
        #include <algorithm>
        #include <vector>
        #include <string>
        
        using namespace std;
        
        
        char one_line_string[] = "hello hi how are you nice weather we are having ok then bye";
        char seps[]   = " ,\t\n";
        char *token;
        
        
        
        int main()
        {
           vector<string> vec_String_Lines;
           token = strtok( one_line_string, seps );
        
           cout << "Extracting and storing data in a vector..\n\n\n";
        
           while( token != NULL )
           {
              vec_String_Lines.push_back(token);
              token = strtok( NULL, seps );
           }
             cout << "Displaying end result in  vector line storage..\n\n";
        
            for ( int i = 0; i < vec_String_Lines.size(); ++i)
            cout << vec_String_Lines[i] << "\n";
            cout << "\n\n\n";
        
        
        return 0;
        }
        

        【讨论】:

          【解决方案6】:

          C++ 最适合与几乎标准的库提升一起使用。

          还有一个例子: http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768

          【讨论】:

            【解决方案7】:

            要么使用stringstream,要么从您的ifstream 中逐个读取令牌。

            用字符串流来做:

            string line, token;
            ifstream in(file);
            
            while(getline(in, line))
            {
                stringstream s(line);
                while (s >> token)
                {
                    // save token to your array by value
                }
            }
            

            【讨论】:

            • 当然,如果您愿意,可以使用 boost,或者使用其他 STL 函数从字符串流中复制。
            • 如果输入以空格结尾,则此内部 while 循环最后会生成一个额外的空标记。惯用的 C++ while(s &gt;&gt; token) 没有。
            • 这是真的。也可以编辑以使用该方法。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-08-19
            • 2011-12-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-03
            • 2016-01-15
            相关资源
            最近更新 更多