views:

100

answers:

2

Shouldn't this work?

string s;
s = "some string";
+5  A: 

Yes!

It's default constructing a string, then assigning it from a const char*.

(Why did you post this question?... did you at least try it?)

Stephen
I was having a problem doing it with vectors. This answer helped me to find the problem, which lay elsewhere, and had to do with how vectors work.
Phenom
Ah, I see - glad it helped. Next time, you might post a little more detail about your problem and we'd probably enjoy helping you with that too :)
Stephen
+7  A: 

Shouldn't this work?

string s;
s = "some string";

Well, actually it's spelled std::string, but if you have a using namespace std; (evil) or using std::string; (somewhat less evil) before that, it should work - provided that you also have a #include <string> at the top of your file.

Note, however, that it is wasteful to first initialize s to be an empty string, just to replace that value in the very next statement. (And if efficiency wasn't your concern, why would you program in C++?) Better would be to initialize s to the right value immediately:

std::string s = "some string" 

or

std::string s("some string");
sbi