-4

I am just curious about this:

std::string s(10);

It doesn't work. I would like to know the reason why it doesn't make a string with 10 elements. This is probably an easy question but I can't figure it out.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Possible duplicate of [How to initialize an std::string with a length?](https://stackoverflow.com/questions/27370480/how-to-initialize-an-stdstring-with-a-length) – user1781290 Apr 12 '19 at 20:22
  • 1
    There's no version of class `std::string` constructor that takes an int. If you want for some reason to create one: `std::string s(10, '');` This will create a string object of size 10 empty characters. – Raindrop7 Apr 12 '19 at 20:23

1 Answers1

1

std::string has many constructors.

If You want a string with an explicit length, then a character has to be specified which will be used to fill this string:

std::string s(10, ' ');
Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31