【问题标题】:Reading an Input File And Store The Data Into an Array (beginner)!读取输入文件并将数据存储到数组中(初学者)!
【发布时间】:2016-04-05 19:17:06
【问题描述】:

输入文件:

1 4 red
2 0 blue
3 1 white
4 2 green
5 2 black

我想要做的是获取每一行并将其存储到二维数组中。 例如:

array[0][0] = 1
array[0][1] = 4
array[0][2] = red
array[1][0] = 2
array[1][1] = 0
array[1][2] = blue
etc..

我正在处理的代码:

    #include <iostream>
    #include <fstream> 
    #include <string>
    #include <sstream>
    #include <vector>

    using namespace std;

    int convert_str_to_int(const string& str) {
        int val;
        stringstream ss;
        ss << str;
        ss >> val;
        return val;
    }


    string getid(string str){

        istringstream  iss(str);
        string pid;
        iss >> pid;
        return pid;
    }
    string getnumberofcolors(string str){
        istringstream  iss(str);
        string pid,c;
        iss >> pid>>c;
        return c;
    }

    int main() {
        string lineinfile ;
        vector<string> lines;

        ifstream infile("myinputfile.txt"); 
        if ( infile ) {
            while ( getline( infile , lineinfile ) ) {
            lines.push_back(lineinfile);
            }
        }
        //first line - number of items
        int numofitems = convert_str_to_int(lines[0]);

        //lopps items info 
        string ar[numofitems ][3];
        int i = 1;

        while(i<=numofitems ){
            ar[i][0] = getid(lines[i]);
            i++;
        }
        while(i<=numofitems ){
            ar[i][1] = getarrivel(lines[i]);
            i++;
        }

        infile.close( ) ;
        return 0 ;
    }

当我添加第二个 while 循环时,我的程序由于某种原因停止工作! 有没有其他方法可以解决这个问题或我的程序的解决方案来解决它。

【问题讨论】:

    标签: c++ arrays string iostream ifstream


    【解决方案1】:

    最好向你展示如何做得更好:

    #include <fstream> 
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main() {
        ifstream infile("myinputfile.txt");  // Streams skip spaces and line breaks
    
        //first line - number of items
        size_t numofitems;
        infile >> numofitems;
    
        //lopps items info 
        vector<pair<int, pair<int, string>> ar(numofitems); // Or use std::tuple
    
        for(size_t i = 0; i < numofitems; ++i){
            infile >> ar[i].first >> ar[i].second.first >> ar[i].second.second;
        }
    
        // infile.close( ) ; // Not needed -- closed automatically
        return 0 ;
    }
    

    您可能正在解决某种简单的算法任务。看看std::pair 和std::tuple,它们不仅作为两个元素的容器很有用,还因为它们的自然比较运算符。

    【讨论】:

      【解决方案2】:

      给出的答案确实是比你的更好的解决方案。我想我应该指出你的一些设计缺陷并给出一些改进它的提示。

      • 您重新定义了标准中已存在的函数,即 std::stoi() 将字符串转换为整数。记住,如果一个函数 已经存在,可以重复使用它,不要认为你必须重新发明什么 已经被发明了。如果您不确定,请搜索您最喜欢的 C++ 参考指南。
      • 解决方案“按原样”存储数据,而您将其存储为完整字符串。这真的没有意义。你事先知道数据是什么,利用它来发挥你的优势。另外,当您存储这样的一行数据时,必须先对其进行解析、转换和构造,然后才能以任何方式使用它,而在解决方案中,数据构造一次且仅一次。
      • 因为事先知道数据的格式,所以加载信息的更好方法是定义结构以及输入/输出运算符。这看起来像这样:

        struct MyData
        {
           int         num1;
           int         num2;
           std::string color;
        
           friend std::ostream& operator << (std::ostream& os, const MyData& d); 
           friend std::istream& operator >> (std::istream& os, const MyData& d);
        };
        

        然后你可以简单地做这样的事情:

        ...
        MyData tmp; 
        outfile << tmp; 
        vData.push_back(tmp);
        ...
        

        它们不是意图问题,我们显然是从流中读取数据类型并将其存储在容器中。如果有的话,您在做什么比您的原始解决方案或提供的解决方案更清楚。

      【讨论】:

        猜你喜欢
        • 2017-06-25
        • 2019-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多