【问题标题】:How to I delimit a string and store each part in different vectors如何分隔字符串并将每个部分存储在不同的向量中
【发布时间】:2017-10-06 21:34:53
【问题描述】:

我有一个包含很多行的文本文件,例如

约翰:学生:企业

可能:讲师:数学

鲍勃:学生:数学

我如何将它们拆分并将它们存储在不同的向量中,例如:

vector1:约翰、梅、鲍勃

vector2:学生、讲师、学生

vector3:商业、数学、数学

我当前的代码是:

ifstream readDetails(argv[1]);
while (getline (readEvents, line, ':')){
    cout << line << endl;
}

它只是将字符串拆分,但我想不出任何方法来分离字符串并将它们存储到向量中。

【问题讨论】:

    标签: c++ string vector getline


    【解决方案1】:

    您可以创建一个向量向量并使用标记索引。

    如果流有时可以包含较少的令牌,您将需要处理这种情况。

    #include <iostream>
    #include <string>
    #include <sstream>
    #include <vector>
    int main() {
    
        const int NUM_TOKENS = 3;
        std::vector<std::vector<std::string>> v(NUM_TOKENS);
        int token = 0;
        std::string str("Mary:Had:Lamb");
        std::istringstream split(str);
        std::string line;
        while (std::getline (split, line, ':')){
            v[token++].push_back(line);
            if ( token == NUM_TOKENS )
                token = 0;
    
        }
        return 0;
    }
    

    【讨论】:

      【解决方案2】:

      这可以使用嵌套向量(也称为 2D 向量)和嵌套循环来完成。外循环用于将输入拆分为行,内循环用于拆分每行的标记,每行以':' 分隔。

      #include <string>
      #include <sstream>
      #include <vector>
      
      int main() 
      {
          std::istringstream in{
              "john:student:business\n"
              "may:lecturer:math\n"
              "bob:student:math"
          };
      
          std::vector< std::vector< std::string > > v{ 3 }; // Number of tokens per line
      
          std::string line;
          // Loop over the lines
          while( std::getline( in, line ) ) {
              std::istringstream lineStrm{ line };
              // Loop to split the tokens of the current line
              for( auto& col : v ) {
                  std::string token;
                  std::getline( lineStrm, token, ':' );
                  col.push_back( token );
              }
          } 
      }
      

      Live demo on Coliru.

      【讨论】:

        【解决方案3】:

        创建字符串向量并相应地存储您的数据。不使用std::istringstream,基本上可以利用String类自带的substr()find(),因此:

        #include <iostream>
        #include <string>
        #include <fstream>
        #include <vector>
        
        using namespace std;
        
        int main ()
        {
            ifstream inFile("data.txt");
            string line;
            vector<string> vStr1,vStr2, vStr3;
        
            while( std::getline( inFile, line ) ){
                string::size_type idx1 = line.find(":");
                string::size_type idx2 = line.rfind(":");
                vStr1.push_back(line.substr(0,idx1));
                vStr2.push_back(line.substr(idx1+1,idx2-idx1-1));
                vStr3.push_back(line.substr(idx2+1));   
            }
        
            cout << "vector1: ";
            for(int i(0); i < vStr1.size(); ++i ){
                cout << vStr1[i] << " "; 
            }
            cout << endl;
        
            cout << "vector2: ";
            for(int i(0); i < vStr2.size(); ++i ){
                cout << vStr2[i] << " "; 
            }
            cout << endl;
        
            cout << "vector3: ";
            for(int i(0); i < vStr3.size(); ++i ){
                cout << vStr3[i] << " "; 
            }
            cout << endl;
        
          return 0;
        }
        

        结果是

        vector1: john may bob
        vector2: student lecturer student
        vector3: business math math
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-25
          相关资源
          最近更新 更多