user3728078
user3728078

Reputation:

C++ vector push_back()

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?

Upvotes: 0

Views: 141

Answers (2)

Cory Kramer
Cory Kramer

Reputation: 117856

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.

Upvotes: 5

Oncaphillis
Oncaphillis

Reputation: 1908

myVector may be a std::vector<std::vector<T>>

Upvotes: 1

Related Questions