Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.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,我有一个类Person并创建一个Person类型向量。我想知道当我调用带有向量索引的Person类型函数时会发生什么。这是否在数组中存储对象或什么请简要解释,提前感谢 #include<iostream> #include<string> #include<vector> using namespace std; class Person { int age; string name; public: Person(){}; void getdata() {

我有一个类Person并创建一个Person类型向量。我想知道当我调用带有向量索引的Person类型函数时会发生什么。这是否在数组中存储对象或什么请简要解释,提前感谢

#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Person
{
int age;
string name;
public:
Person(){};
void getdata()
{
    cout << "Enter your name: ";
    getline(cin >> ws, name);
    cout << "Enter your age: ";
    cin >> age;
}
void showdata()
{
    cout << "\nHello " << name << " your age is " << age;
}
};
void main()
{
vector<Person> myVector(3); 
unsigned int i = 0;
for (i; i < 3; i++)
    myVector[i].getdata();//Does this create & save an objects in that  location   Please explain briefly, Thanks
for (i=0; i < 3; i++)       //What if I do this like
                            /*for (i=0; i < 3; i++)
                                { Person p;
                                myVector.puskback(p); }
                                or what if I want a new data then what??*/

    myVector[i].showdata();
system("pause");
}
#包括
#包括
#包括
使用名称空间std;
班主任
{
智力年龄;
字符串名;
公众:
Person(){};
void getdata()
{
cout>ws,name);
年龄;
}
void showdata()
{

cout否,它不创建对象。所有对象都是在创建向量时创建的。它所做的是对已构建的对象调用getdata()。
您可以按照建议的方式进行后推,在本例中,您希望最初创建一个空向量(现在您正在创建一个包含3个元素的向量)

不,它不会创建对象。所有对象都是在创建向量时创建的。它会对已构建的对象调用getdata()。 您可以按照建议的方式进行后推,在这种情况下,您希望最初创建一个空向量(现在您正在创建一个包含3个元素的向量)

请考虑

class A
{
public:
    A(){}
    foo(){}
};
...
int main()
{
     std::vector<A> v1(3);
     //this creates a vector with 3 As by calling the empty constructor on each
     //means means you can readily manipulate these 3 As in v1
     for(int i = 0; i < v1.size(); i++)
         v1[i].foo();

     std::vector<A> v2;
     //this creates a vector of As with no pre-instantiated As
     //you have to create As to add to the vector
     A a1;
     v2.push_back(a1);
     //you can now manipulate a1 in v2
     v2[0].foo(); 

     //You can add to v1 after having initially created it with 3 As
     A a2;
     v1.push_back(a2);

     //You have all the flexibility you need.
}
A类
{
公众:
A(){}
foo(){}
};
...
int main()
{
std::载体v1(3);
//这将通过调用每个对象上的空构造函数来创建一个带有3 As的向量
//意味着你可以像在v1中一样轻松地操作这3个
对于(int i=0;i
考虑

class A
{
public:
    A(){}
    foo(){}
};
...
int main()
{
     std::vector<A> v1(3);
     //this creates a vector with 3 As by calling the empty constructor on each
     //means means you can readily manipulate these 3 As in v1
     for(int i = 0; i < v1.size(); i++)
         v1[i].foo();

     std::vector<A> v2;
     //this creates a vector of As with no pre-instantiated As
     //you have to create As to add to the vector
     A a1;
     v2.push_back(a1);
     //you can now manipulate a1 in v2
     v2[0].foo(); 

     //You can add to v1 after having initially created it with 3 As
     A a2;
     v1.push_back(a2);

     //You have all the flexibility you need.
}
A类
{
公众:
A(){}
foo(){}
};
...
int main()
{
std::载体v1(3);
//这将通过调用每个对象上的空构造函数来创建一个带有3 As的向量
//意味着你可以像在v1中一样轻松地操作这3个
对于(int i=0;i
的文档。阅读愉快。不,
myVector
已分配但为空。它类似于设置
vector myVector(3);
然后期望
myVector[1]
返回一个有意义的值。@romeric No,它不是空的。它有三个元素。当然,分配了三个元素,但它们没有设置为任何值。它们最多为零。在数字领域,这些被称为空向量。因此
myVector[1]
不segfault,但同时它不返回有意义的值。这是我的论点。@GuillaumeRacicot:他的类已经有了移动构造函数和移动赋值运算符,因为他没有做任何阻止编译器添加它们的操作。'mpty。它类似于设置
vector myVector(3);
然后期望
myVector[1]
返回一个有意义的值。@romeric No,它不是空的。它有三个元素。当然,分配了三个元素,但它们没有设置为任何值。它们最多为零。在数字领域,这些被称为空向量。因此
myVector[1]
不segfault,但同时它不会返回有意义的值。这是我的论点。@GuillaumeRacicot:他的类已经有了移动构造函数和移动赋值运算符,因为他没有做任何事情来阻止编译器添加它们。非常感谢@jafar的帮助非常感谢@jafar的帮助非常感谢@jafar的帮助非常多