I don't fully understand this usage of std::string

Hello,

So, I am fooling around with a word wrap/auto indent (etc…) function for a text program and I have it working, but I took this code from somewhere else (though I did add to it) and I understand what it’s doing except for one line.

os << std::string(indent, ’ ');

I have only seen string initialized with a name. For example:

std::string name = “”;

With the first code, it has no name. I get that std::string(indent, ’ ') is creating a string with the value of “indent” and it’s creating the indent using a space ’ ’ which then gets added to the ostream (os), but how is it doing this as an unnamed string? I don’t understand, I wasn’t taught this and I can’t for the life of me find a single site online that shows this as an example of how to use std::string. It is always initialized with a name.

Can someone explain how this is working? I really want to understand what every line is doing and this is the only one I can’t figure out in the whole code. I can post the whole thing if necessary.

Thanks in advance!

I found one reference that might be helpful (about string constructors) cplusplus.com

The idea seems to be that doing

std::string myString = "I am a string";

is the same as

std::string myString("I am a string");

and doing

std::string strFiveXs(5, 'x');

creates a string of the character 'x' repeated 5 times "xxxxx"

#include <iostream> int main() { std::string strFiveXs(5, 'x'); std::cout << strFiveXs; return 0; }

Hello,

Thanks for that. I don’t think it’s a constructor though. I had someone else point something out to me that I didn’t know you could do which is you can create a string (or any type) without saving it to a variable if you just want to use it once and then evaporate afterwards. Even in your examples, your strings have names (MyString, StrFiveXs) whereas my example does not have a name and isn’t saved as a variable anywhere.

So for example:

std::string name = " ";

… is the same as…

std::string(5, ' ');

… only in the latter example, it’s just an anonymous string that isn’t saved anywhere after it’s used. Both strings print five spaces. I will have to look into this more. Thank you for your reply!

I guess it would be accurate to say that in the case of os << std::string(5, ' ') , string is being used as a function whereas in std::string name = " " , string is being used to declare a type. String works differently in both instances and I guess the compiler tells string, when used as a function, that (5, ' ') is telling string to repeat ’ ’ five times.

Really makes me wish Codecademy’s C++ courses were more robust and explored more of these uses. There’s so many libraries with so many functions that it can be overwhelming at times. :smile:

yes, doing

std::cout << "xxxxx";

would be the same as doing

std::cout << std::string(5, 'x');  // constructor with no variable

and you don’t have to save the string as a variable if you won’t use it again.

Okay, so you’re calling it a constructor. Is it not accurate to say it’s being used as a function in that instance?

Constructors are things I should really use more often as I’m not great with them yet.

Yes, a constructor is a special type of function (or it works like one).