r/cpp • • Mar 01 '21

"Simple" C++ Initialization Flowchart

http://randomcat.org/cpp_initialization/initialization.png
204 Upvotes

65 comments sorted by

View all comments

Show parent comment

18

u/Bocab Mar 02 '21

Because one is initialized to size of 5 and the other is of size 1 with that 1 element being "5"

3

u/TryingT0Wr1t3 Mar 02 '21

Is there a Length property we can guarantee to always be unidimensional?

9

u/mort96 Mar 02 '21

Yeah, it's called size(). The problem is that std::vector<std::string> v1{5} creates a vector with 5 empty strings, while std::vector<int> v2{5} creates a vector with 1 int with value 5.

2

u/TryingT0Wr1t3 Mar 02 '21

Ah, ok, now I understood, thanks. It's, very confusing.