I just saw in a tutorial someone who used in the same file both:
myVector.back().push_back();
myVector.push_back();
What's the difference?
Cory Kramer
119k19 gold badges176 silver badges233 bronze badges
asked Nov 7, 2014 at 19:40
user3728078
2 Answers 2
The first had to be something like
vector<vector<T>>
Otherwise it would not work. back() returns the element at the back of the vector. When you say
myVector.back().push_back();
it would be accessing the last vector<T>, then calling push_back() on that inner vector
If it is the case that myVector is a vector<vector<T>>, then
myVector.push_back();
would be pushing back an empty vector<T> whereas
myVector.back().push_back();
would be pushing back a default T onto the last vector<T> in myVector.
answered Nov 7, 2014 at 19:41
Cory Kramer
119k19 gold badges176 silver badges233 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Cory Kramer
In that case,
back() would return an instance of your class that was the last element in the vector. So unless your class has a push_back() method, then the first version will not compile.myVector may be a std::vector<std::vector<T>>
answered Nov 7, 2014 at 19:41
Oncaphillis
1,91813 silver badges15 bronze badges
Comments
lang-cpp