I push_back an element into a vector by for-loop in 2 levels. After finishing, I use for-loop to print it again but can not see the value.
I tried the code on a CentOS-7.
#include<iostream>
#include<vector>
using namespace std;
class Y{
public:
int a;
};
class X{
public:
int a;
vector <Y> b;
};
int main(){
vector<X> root;
X x;
x.a = 55;
root.push_back(x);
for(auto e : root)
{
cout << e.a << ",";
Y y;
y.a = 66;
e.b.push_back(y);
for(auto e2 : e.b)
{
cout << e2.a << ",";
}
}
// Print it again
for(auto e : root)
{
cout << e.a << ",";
for(auto e2 : e.b)
{
cout << e2.a << ",";
}
}
cout << endl;
return 0;
}
I expect the output to be 55,66,55,66, but the actual output is 55,66,55,
Aucun commentaire:
Enregistrer un commentaire