简体   繁体   中英

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?

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM