-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.

2
  • Possible duplicate of How to initialize an std::string with a length? Commented Apr 12, 2019 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. Commented Apr 12, 2019 at 20:23

1 Answer 1

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, ' ');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.