tags:

views:

291

answers:

4

Is there a simple way of creating a std::string out of an const char[] ?

I mean something simpler then:

std::stringstream stream;
stream << const_char;

std::string string = stream.str();
+11  A: 

std::string has a constructor that takes a const char*, so you can just do:

const char* charArray;
std::string str(charArray);
Nathaniel Flath
+1  A: 
std::string str = const_char;
Mehrdad Afshari
+10  A: 

std::string has multiple constructors, one of which is string( const char* str );.

You can use it like this:

std::string myString(const_char);

You could also use assignment, if you need to set the value at some time later than when the variable is declared:

myString = const_char;
crashmstr
+1  A: 

Just use the std::string constructor. You can pass it a char*:

char* charArray = "My String";
std::string stdstr( charArray );
Dusty Campbell