c++ - 如何填充类型为 "User"的 vector ,该 vector 具有 2 个分量 "username"和 "password"

标签 c++ vector populate

所以我已经设法打开一个文件并读取该文件中的不同输入。问题是我如何获取这些单独的输入(用户名和密码的多次迭代)并填充类型为 User 的 vector ,其中包含两件事:用户名和密码? 这个类保存在另一个名为 User.h 的文件中,其中包含

class User
{
private:
    string username;
    string password;
public:
// etc etc etc...
};

使用用户类:

void BBoard::setup(const string & input_file)
{
    //ifstream filename;
    ////find a way to turn a string into a char pointer
    //filename.open(input_file);

    string username;
    string password;
    fstream f;

    f.open("test");
    while(f>>username)
    {
        f>>password;
        cout << username << " " << password << endl;
        //make User object and push to user_list
        user_list.at(i).username
        if(f.eof()) break;
    }
    f.close();
}

测试文件内容:

user1 password1
user2 password2
user3 password3

最佳答案

假设您的列表包含值(而不是指针):

vector<User> user_list;
user_list.push_back(User(username, password));

如果你知道有多少个元素,并且User有一个默认的构造函数,你可以调用user_list.reserve(n)来提高效率。

关于c++ - 如何填充类型为 "User"的 vector ,该 vector 具有 2 个分量 "username"和 "password",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16431367/

相关文章:

c++ - 传递参数(将 std::string 转换为 LPWSTR 的概率)

node.js - Mongoose 填充没有 ObjectId 的字段?

PHP MySQL : select from database and populate form

c++ - 程序崩溃并显示 0xC000000D 并且没有异常 - 我该如何调试它?

C++ - 如何不错过来自多个线程的多个通知?

python - 如何用Python对向量场进行插值?

c++ - 创建大量对象指针

javascript - 哪个更好?为什么? RaphaelJS 还是 HTML5 Canvas?

mongodb - Mongoose:深人口(填充人口密集的领域)

c++ - 从父类(super class)调用子类中的虚函数