C ++ 11 string initialization

I need to create a string of 100 characters.

Why the following

std::string myString = {100, 'A'}; 

give different results than

 std::string myString(100, 'A'); 

?

+7
source share
2 answers
 std::string myString = {100, 'A'}; 

initialized using a list of initializers. It creates a string with two characters: one with code 100 and "A"

 std::string myString(100, 'A'); 

calls the following constructor:

 string (size_t n, char c); 

which creates a string with 100 'A

+14
source

The first initializes it with the values ​​100 and A , and the second causes an overload of the constructor std::string .

+1
source

All Articles