C ++从具有多个分隔符的文件中读取矩阵

这个问题在这里已经有了答案:

  • 使用字符串分隔符(标准C ++)在C ++中解析(拆分)一个字符串11个答案
  • 迭代字符串最优雅的方式[已关闭] 74个答案

  • string line;
    while(getline(infile, line, '|'))
    {
        stringstream rowstream(line);
        string row;
        while(getline(rowstream, row, ';'))
        {
               stringstream elementstream(row);
                string element;
                while(getline(elementstream, element, ','))
                {
                    cout << element << endl;                    
                }
        }
    }
    

    使用上面的代码,你可以建立逻辑来存储个人element只要你喜欢。


    我使用这个自己的函数将一个字符串拆分为一个字符串向量:

    /**
     * brief   Split a string in substrings
     * param   sep  Symbol separating the parts
     * param   str  String to be splitted
     * return  Vector containing the splitted parts
     * pre     The separator can not be 0
     * details Example :
     * code
     * std::string str = "abc.def.ghi..jkl.";
     * std::vector<std::string> split_str = split('.', str); // the vector is ["abc", "def", "ghi", "", "jkl", ""]
     * endcode
     */
    std::vector<std::string> split(char sep, const std::string& str);
    
    std::vector<std::string> split(char sep, const std::string& str)
    {
      assert(sep != 0 && "PRE: the separator is null");
      std::vector<std::string> s;
      unsigned long int i = 0;
      for(unsigned long int j = 0; j < str.length(); ++j)
      {
        if(str[j] == sep)
        {
          s.push_back(str.substr(i, j - i));
          i = j + 1;
        }
      }
      s.push_back(str.substr(i, str.size() - i));
      return s;
    }
    

    然后,期待你有一个班级矩阵,你可以做一些事情:

    std::string matrices_str;
    std::ifstream matrix_file(matrix_file_name.c_str());
    matrix_file >> matrices_str;
    const std::vector<std::string> matrices = split('|', matrices_str);
    std::vector<Matrix<double> > M(matrices.size());
    for(unsigned long int i = 0; i < matrices.size(); ++i)
    {
      const std::string& matrix = matrices[i];
      const std::vector<std::string> rows = split(';', matrix);
      for(unsigned long int j = 0; j < rows.size(); ++j)
      {
        const std::string& row = matrix[i];
        const std::vector<std::string> elements = split(',', row);
        for(unsigned long int k = 0; k < elements.size(); ++k)
        {
          const std::string& element = elements[k];
          if(j == 0 && k == 0)
            M[i].resize(rows.size(), elements.size());
          std::istringstream iss(element);
          iss >> M[i](j,k);
        }
      }
    }
    

    或者,压缩代码:

    std::string matrices_str;
    std::ifstream matrix_file(matrix_file_name.c_str());
    matrix_file >> matrices_str;
    const std::vector<std::string> matrices = split('|', matrices_str);
    std::vector<Matrix<double> > M(matrices.size());
    for(unsigned long int i = 0; i < matrices.size(); ++i)
    {
      const std::vector<std::string> rows = split(';', matrices[i]);
      for(unsigned long int j = 0; j < rows.size(); ++j)
      {
        const std::vector<std::string> elements = split(',', matrix[i]);
        for(unsigned long int k = 0; k < elements.size(); ++k)
        {
          if(j == 0 && k == 0)
            M[i].resize(rows.size(), elements[k].size());
          std::istringstream iss(elements[k]);
          iss >> M[i](j,k);
        }
      }
    }
    

    你可以使用finite state machine概念。 您需要为每个步骤定义状态。 读一个字符,然后决定它是什么(数字或分隔符)。

    这里是你如何做到这一点的概念。 更多阅读请在互联网上查看。 text parsingfinite state machinelexical analyzerformal grammar

    enum State
    {
        DECIMAL_NUMBER,
        COMMA_D,
        SEMICOLON_D,
        PIPE_D,
        ERROR_STATE,
    };
    
    char GetChar()
    {
        // implement proper reading from file
        static char* input = "1,2;3,4|0,1;1,0|5,3;3,1|";
        static int index = 0;
    
        return input[index++];
    }
    
    State GetState(char c)
    {
        if ( isdigit(c) )
        {
            return DECIMAL_NUMBER;
        }
        else if ( c == ',' )
        {
            return COMMA_D;
        }
        else if ( c == ';' )
        {
            return SEMICOLON_D;
        }
        else if ( c == '|' )
        {
            return PIPE_D;
        }
    
        return ERROR_STATE;
    }
    
    int main(char* argv[], int argc)
    {
        char c;
        while ( c = GetChar() )
        {
            State s = GetState(c);
            switch ( c )
            {
            case DECIMAL_NUMBER:
                // read numbers
                break;
            case COMMA_D:
                // append into row
                break;
            case SEMICOLON_D:
                // next row
                break;
            case PIPE_D:
                // finish one matrix
                break;
            case ERROR_STATE:
                // syntax error
                break;
            default:
                break;
            }
        }
        return 0;
    }
    
    链接地址: http://www.djcxy.com/p/19581.html

    上一篇: C++ Read matrices from file with multiple delimiters

    下一篇: creating a string split function in C++