Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;用用户输入填充对象向量_C++_Vector - Fatal编程技术网

C++ C++;用用户输入填充对象向量

C++ C++;用用户输入填充对象向量,c++,vector,C++,Vector,我对向量和类都是新手。也就是说,我找到了一些关于如何创建对象向量的帖子。我想知道如何从用户输入创建对象向量?假设程序要求用户提供他/她想要创建的员工(类)数量。用户希望添加5名员工。所以用户必须输入员工的姓和名。我心里有一个for循环,但我不确定如何获取用户输入(可能使用getline和push_back?)并将其存储在向量中 //Lets say class.h looks like this class Employee { private: string lastName;

我对向量和类都是新手。也就是说,我找到了一些关于如何创建对象向量的帖子。我想知道如何从用户输入创建对象向量?假设程序要求用户提供他/她想要创建的员工(类)数量。用户希望添加5名员工。所以用户必须输入员工的姓和名。我心里有一个for循环,但我不确定如何获取用户输入(可能使用getline和push_back?)并将其存储在向量中

//Lets say class.h looks like this
class Employee
{
private:
    string lastName;
    string firstName;
public:
    void setLastname(string);
    void setFirstname(string);
    string getLastname() const;
    string getFirstname() const;
}

你的
Employee
类应该有一个构造函数。收集输入时,需要所有构造函数参数。然后,要将
雇员
添加到
向量
,请调用
雇员。回置(构造函数参数)

你的
雇员
类应该有一个构造函数。收集输入时,需要所有构造函数参数。然后,将<代码>雇员<代码>添加到<代码>向量 >中,调用<代码>雇员.EMPONTHORE BULL(cTor参数) ./P> < P>其他用户提供了一个很好的例子,说明如何在现代C++中使用<代码> EndoStReP<代码>。 如果使用C++11之前的版本,则没有
emplace\u back
方法。在这种情况下,您可以使用老式的
push_back
手动收集
向量中的数据。您可以简单地定义一个向量:

vector<Employee> employees;

还可以定义一个构造函数,它同时使用<代码> FieldNe> /Cuff>和 LSTNEX/<代码>作为参数,使之更易于处理。

< P>其他用户提供了一个很好的例子,说明如何在现代C++中使用<代码> EMPTACTION B/COD>。 如果使用C++11之前的版本,则没有
emplace\u back
方法。在这种情况下,您可以使用老式的
push_back
手动收集
向量中的数据。您可以简单地定义一个向量:

vector<Employee> employees;

您还可以定义一个构造函数,将
firstname
lastname
作为参数,以使其更易于使用。

std::vector
有一个名为
emplace\u back
的好方法

将新元素追加到容器的末尾。元素是通过std::allocator_traits::construct构造的,它通常使用placement new在容器提供的位置就地构造元素

因此,您唯一缺少的是能够使用它的适当构造函数

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

using namespace std;

class Employee
{
private:
    string firstName;
    string lastName;
public:
    void setLastname(string);
    void setFirstname(string);
    string getLastname() const{return lastName;}
    string getFirstname() const{return firstName;}

    //create a constructor
    Employee(string firstName, string lastName)
        : firstName(firstName), lastName(lastName)
    {}
};

int main()
{
    vector<Employee> emp;
    int count = 0;
    cout << "Enter the amount of employees to add:" << endl;
    cin >> count;

    string firstName, lastName;
    for(int i = 0; i < count; ++i)
    {
        cout << "Please enter the first and last names" << endl;

        cin >> firstName;
        cin >> lastName;

        emp.emplace_back(firstName, lastName);
    }

    for(const Employee & e : emp)
    {
        cout << "Employee:" << endl;
        cout << e.getFirstname() << " " << e.getLastname() << endl;
    }
}
#包括
#包括
#包括
使用名称空间std;
班级员工
{
私人:
字符串名;
字符串lastName;
公众:
void setLastname(字符串);
void setFirstname(字符串);
字符串getLastname()常量{return lastName;}
字符串getFirstname()常量{return firstName;}
//创建一个构造函数
员工(字符串firstName,字符串lastName)
:firstName(firstName)、lastName(lastName)
{}
};
int main()
{
矢量电磁脉冲;
整数计数=0;
不能计数;
字符串firstName,lastName;
对于(int i=0;i>姓氏;
emp.emp.emplace_back(姓、名);
}
用于(施工人员和设备:emp)
{

cout
std::vector
有一个很好的方法叫做
emplace\u back
,它

将新元素追加到容器的末尾。元素通过std::allocator_traits::construct构造,它通常使用placement new在容器提供的位置就地构造元素

因此,您唯一缺少的是能够使用它的适当构造函数

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

using namespace std;

class Employee
{
private:
    string firstName;
    string lastName;
public:
    void setLastname(string);
    void setFirstname(string);
    string getLastname() const{return lastName;}
    string getFirstname() const{return firstName;}

    //create a constructor
    Employee(string firstName, string lastName)
        : firstName(firstName), lastName(lastName)
    {}
};

int main()
{
    vector<Employee> emp;
    int count = 0;
    cout << "Enter the amount of employees to add:" << endl;
    cin >> count;

    string firstName, lastName;
    for(int i = 0; i < count; ++i)
    {
        cout << "Please enter the first and last names" << endl;

        cin >> firstName;
        cin >> lastName;

        emp.emplace_back(firstName, lastName);
    }

    for(const Employee & e : emp)
    {
        cout << "Employee:" << endl;
        cout << e.getFirstname() << " " << e.getLastname() << endl;
    }
}
#包括
#包括
#包括
使用名称空间std;
班级员工
{
私人:
字符串名;
字符串lastName;
公众:
void setLastname(字符串);
void setFirstname(字符串);
字符串getLastname()常量{return lastName;}
字符串getFirstname()常量{return firstName;}
//创建一个构造函数
员工(字符串firstName,字符串lastName)
:firstName(firstName)、lastName(lastName)
{}
};
int main()
{
矢量电磁脉冲;
整数计数=0;
不能计数;
字符串firstName,lastName;
对于(int i=0;i>姓氏;
emp.emp.emplace_back(姓、名);
}
用于(施工人员和设备:emp)
{
库特
您需要一个以两个字符串作为输入的构造函数。显然,您可以不使用它,但这会减少冗长

员工(字符串paramLastName,字符串paramFirstName): lastName(move(paramLastName)),firstName(move(paramLastName)){}

你需要这样称呼它

vector<Employee> inp;
string tmp1, tmp2;
while(std::cin>>tmp1 >> tmp2) {
    inp.emplace_back(tmp1, tmp2);
}
矢量inp;
字符串tmp1、tmp2;
而(std::cin>>tmp1>>tmp2){
进位后(tmp1、tmp2);
}
(假设您将使用c++11支持进行编译)

您需要一个以两个字符串作为输入的构造函数。显然,您可以不使用它,但这会减少冗长

员工(字符串paramLastName,字符串paramFirstName): lastName(move(paramLastName)),firstName(move(paramLastName)){}

你需要这样称呼它

vector<Employee> inp;
string tmp1, tmp2;
while(std::cin>>tmp1 >> tmp2) {
    inp.emplace_back(tmp1, tmp2);
}
矢量inp;
字符串tmp1、tmp2;
而(std::cin>>tmp1>>tmp2){
进位后(tmp1、tmp2);
}

(假设您将使用c++11支持进行编译)

std::cin
std::getline
std::vector
应该这样做。基本技能是将大问题分解为子问题。例如,为程序设计循环时,完全不考虑如何获得用户输入(如果有的话),只考虑如何获得用户输入,同样也不考虑如何创建
>Employee
对象,以及如何将其存储在向量中。
std::cin
std::getline
std::vector
应该这样做。基本技能是将大问题分解为子问题。例如,以一种完全不需要参考的方式为程序设计循环