I was reading c++ primer and came across an example of list initialization for vector where the author mentioned that if list initialization isn't possible, the compiler then looks for other ways to initialize the object. Below is such an example for vector of string.
vector<string> v8{10, "hi"};
This creates a vector of 10 elements with value "hi". This I think is because list initialization falls back on below constructor to create the vector.
vector( size_type count, const T& value, const Allocator& alloc = Allocator());
I tried similar kind of list initialization for String
std::string s1{10, 'c'};
I was expecting it to create a string "cccccccccc" because there is a constructor available for it
basic_string( size_type count, CharT ch, const Allocator& alloc = Allocator() );
But it prints only "c"? Which string constructor is getting used here?
Aucun commentaire:
Enregistrer un commentaire