简体   繁体   中英

What is std::vector<int> initialised to?

std::vector<int> foo(5);  // creates 5-element int vector

what is the value (or that is the values) of foo? I understand that it creates a 5-element character vector, but what is the value of each, is it zero or undefined?

When you use this particular constructor of std::vector , the elements of the vector are value initialized.

See http://en.cppreference.com/w/cpp/container/vector/vector for more details on vector::vector .

See http://en.cppreference.com/w/cpp/language/value_initialization for more on value initialization.

For your case, the elements are of type int . That implies each of the elements the vector is initialized to zero.

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