首页 > 解决方案 > 如何将数据从文件c ++推送到向量

问题描述

我在“.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 

有人可以告诉我这有什么问题吗?

标签: c++vectorpush-back

解决方案


你已经定义了一个向量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 的用法!我将文件读取的逻辑与问题中的逻辑相同。在生产就绪代码中,绝对应该添加更多检查!


推荐阅读