【问题标题】:How to push data to a vector from a file c++如何将数据从文件c ++推送到向量
【发布时间】:2020-08-15 14:20:24
【问题描述】:

我在“.csv”文件中有一些数据表。我想将这些数据从该文件推送到向量。我尝试了 push_back。但它对我没有用。我实际上是一个初学者c++。有人可以帮我解决这个问题吗?

struct contacts {
    string name;
    string nickName;
    string phoneNumber;
    string carrier;
    string address;
};

vector <contacts> proContactFile;

这是我从文件中读取值的部分(下)

void readContactDetails() {
    ifstream ContactFile;
    ContactFile.open("Contact.csv");
    string readString;
    if (ContactFile) {

        while (!(ContactFile.eof())) {
            getline(ContactFile, readString); // Read the column headers
            for (int i = 0; i <= 10; i++) {
                getline(ContactFile, limit[i].name, ','); // ',' is the separator
                getline(ContactFile, limit[i].nickName, ',');
                getline(ContactFile, limit[i].phoneNumber, ',');
                getline(ContactFile, limit[i].carrier, ',');
                getline(ContactFile, limit[i].address); // Read until the end of the line

            }
            proContactFile.push_back(ContactFile);
        }
    }
    else {
        cout << "Error"<< endl;//Show an error message if the file isn't opened
    }
}

它总是显示错误信息 ->

no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=contacts, _Alloc=std::allocator<contacts>]" matches the argument list 

谁能告诉我这是怎么回事?

【问题讨论】:

  • 您的vectorcontacts 类型的容器,但您是pushing ifstream
  • 你有一个vector &lt;contacts&gt;,所以你添加/push_back 的所有内容都必须是contacts 类型,但你试图添加ContactFile 这是一个ifstream不是 contacts.
  • limit 是什么类型?
  • @drescherjm 在向量之后我放了一些类似下面的东西来设置一个限制。 ........ const int LIMIT = 10;联系人限制[LIMIT];
  • const int LIMIT = 10; contacts limit[LIMIT]; 您的代码正在尝试将 11 个项目读入这个大小为 10 的数组中。如果您有这个限制数组,那么您不需要 proContactFile 向量,因为它们的服务相同目的。

标签: c++ vector push-back


【解决方案1】:

您已经定义了contacts 的向量。唯一可以推回它的是contacts 的实例。通常你的代码应该是这样的:

vector <contacts> proContactFile;       // define a vector
contacts new_contact;                   // define an instance of `contacts` struct
new_contact.name = "name";              // put some data
...
proContactFile.push_back(new_contact);  // push back that instance into the vector

emplace_back 还有另一种方法:

vector <contacts> proContactFile;               // define a vector
auto &contact = proContactFile.emplace_back();  // create an instance in-place and get a ref
new_contact.name = "name";                      // work with this instance through reference

您也可以为构造函数提供数据:

auto &contact = proContactFile.emplace_back("name", "nickname", ...);

猜你需要这样的东西:

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

using namespace std;

struct contacts {
    string name;
    string nickName;
    string phoneNumber;
    string carrier;
    string address;
};

vector <contacts> proContactFile;

void readContactDetails() {
    ifstream ContactFile;
    ContactFile.open("Contact.csv");
    string readString;
    if (ContactFile) {

        if (!(ContactFile.eof())) {
            getline(ContactFile, readString); // Read the column headers
            // reserve place in the vector, since we know how much entries we are going to add
            proContactFile.reserve(10);

            // do not know what `10` is!!!
            for (int i = 0; i < 10; i++) {
                // create a new element in the vector and get reference to it
                auto &contact = proContactFile.emplace_back();  

                // read data into the members of the struct
                getline(ContactFile, contact.name, ','); // ',' is the separator
                getline(ContactFile, contact.nickName, ',');
                getline(ContactFile, contact.phoneNumber, ',');
                getline(ContactFile, contact.carrier, ',');
                getline(ContactFile, contact.address); // Read until the end of the line
            }
        }
    }
    else {
        cout << "Error"<< endl;//Show an error message if the file isn't opened
    }
}

免责声明:代码演示了 std::vector 的用法!我将文件读取的逻辑与问题中的逻辑相同。在生产就绪代码中,绝对应该添加更多检查!

【讨论】:

  • while (!(ContactFile.eof())) { 可能应该用 if () 替换,除非文件每 11 行有一个标题
  • 好的它仍然给出一个错误。它说“无法推断自动类型”。但我得到了你答案背后的整个想法。我想现在我可以弄清楚了。 :D
  • @drescherjm 我用 if() 尝试过。但它给了我一个错误。这就是我保留 while() 的原因。
  • @drescherjm,注意,谢谢。这似乎更正确,尽管我并没有真正检查过那部分。应添加免责声明。
  • @SeektheFreak,那时不应该是编译器。那么问题出在其他地方。你可以在这里玩代码:godbolt.org/z/K91vhK
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
相关资源
最近更新 更多