struct Thing
{
int id;
std::vector<int> v2;
};
std::vector<Thing> v1;
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i )
{
Thing pic;
cin>>pic.id;
v1.push_back(pic);
int x;
cin>>x;
v1.back().v2.push_back(x);
}
}
v1 is not an empty vector. I can't understand the line v1.back().v2.push_back(x);
what's the actual meaning of this line
CodePudding user response:
v1.back()
is the last element of v1
with the type of Thing
v1.back().v2
is the v2
member of that element with the type of vector<int>
So you are calling the push_back()
on a vector<int>
with an int
what is fine
Note that if v1
is empty then v1.back()
is UB
Edit:
v1.back().v2.push_back(x);
is kind of like
{
Thing& t=v1.back();
vector<int>& v=t.v2;
v.push_back(x);
}