【问题标题】:Iterate over array of objects from txt file迭代txt文件中的对象数组
【发布时间】:2017-10-17 23:36:11
【问题描述】:

我有一个记录用逗号分隔的文件:

city.txt:

1,NYC
2,ABQ
...

我想遍历每一行:ID 和名称。我已经创建了代码:

#include <iostream>
#include <string>
using namespace std;

class City {
    int id;
    string name;

public:
    City() {}
    City(int id, int name)
    {
        this->id = id;
        this->name = name;
    }

    void load_file()
    {
        ifstream v_file("cities.txt");
        if (v_file.is_open()) {
            while (!v_file.eof()) {
            //...
            }
        }
        v_file.close();
    }
}

int main()
{
    City array_city[1000];

    array_city.load_file();

    return 0;
}

您能告诉我如何将所有行加载到数组array_city 并对其进行迭代吗?我不知道在load_file 方法中的while 块中放置什么。我不知道天气,方法load_file应该有void类型。不幸的是,我必须在数组上这样做。

【问题讨论】:

  • 你不应该自己做作业吗?
  • 数组不是类,它们不能有方法。您需要从加载函数返回数组,可能使用std::vector
  • @manni66 在作业问题上有rules,它们并不是完全禁止的。也就是说,OP应该澄清这是一项任务,如果它是
  • 弄清楚如何逐行读取文件。谷歌是你的朋友。然后弄清楚你接下来要做什么。
  • 两件事:1)不要关闭你自己的文件,依靠RAII为你做。 2) 看看stackoverflow.com/questions/5605125/…

标签: c++ arrays file class oop


【解决方案1】:

在 while 循环中使用 EOF 不是一个好主意。阅读更多Why is iostream::eof inside a loop condition considered wrong?


中,向量应该优先于数组。但是,您的老师知道更多建议在这里使用数组。出于这个原因,我提供了一个带有数组的解决方案:

  1. 逐行读取文件
  2. 提取id和字符串
  3. 将其分配给数组的第 i 个单元

代码:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class City {
    int id;
    string name;

public:
    City() {}
    City(int id, string name) : id(id), name(name)
    {
    }
    void print()
    {
        cout << "ID = " << id << ", name = " << name << endl;
    }
};

void load_file(City* cities, const int n)
{
    ifstream v_file("cities.txt");
    if (v_file.is_open()) {
        int number, i = 0;
        string str;
        char c;
        while (v_file >> number >> c >> str && c == ',' && i < n)
        {
            //cout << number << " " << str << endl;
            cities[i++] = {number, str};
        }
    }
    v_file.close();
}

int main()
{
    City cities[4]; // assuming there are 4 cities in the file
    load_file(cities, 4);
    for(unsigned int i = 0; i < 4; ++i)
        cities[i].print();

    return 0;
}

std::vector 相同的解决方案,如果您有兴趣。 =) 如果你还没有学过它们,我建议你跳过这部分,稍后在课程中这样做时再回来。

使用City 的向量。 Read the file line by line,然后通过构造你的类的实例将你读到的每一行推回到向量中,你就完成了!

例子:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;

class City {
    int id;
    string name;

public:
    City() {}
    City(int id, string name) : id(id), name(name)
    {
    }
    void print()
    {
        cout << "ID = " << id << ", name = " << name << endl;
    }
};

void load_file(vector<City>& cities)
{
    ifstream v_file("cities.txt");
    if (v_file.is_open()) {
        int number;
        string str;
        char c;
        while (v_file >> number >> c >> str && c == ',' && i < n)
        {
            //cout << number << " " << str << endl;
            cities.push_back({number, str});
        }
    }
    v_file.close();
}

int main()
{
    vector<City> cities;
    load_file(cities);
    for(unsigned int i = 0; i < cities.size(); ++i)
        cities[i].print();

    return 0;
}

输入:

Georgioss-MacBook-Pro:~ gsamaras$ cat cities.txt 
1,NYC
2,ABQ
3,CCC
4,DDD

输出:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -Wall -std=c++0x main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
ID = 1, name = NYC
ID = 2, name = ABQ
ID = 3, name = CCC
ID = 4, name = DDD

【讨论】:

  • 精彩的解释,@gsamaras!感谢您提供 2 个示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 2018-02-23
  • 2013-10-01
  • 2019-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多