   std::string and char
  What''s the best way to concatenate a std::string with a char? 
std::string str = "Hello";char a[] = " world"str += a;  Like that?? ;  just fyi there is a really good tutorial out there somewhere about using STL for games, efficiency and performance and things like that. I don''t remember where it is... and I''m too lazy to look for it... so I guess this post isn''t much help to you at all huh? ;quote:Original post by issch  std::string str = "Hello";char a[] = " world"str += a;   Like that??   No, sorry, just a single char variable.std::string str = "some strin";char letter = ''g'';std::string anotherString = str + letter;  Does not work, but it''s not supposed to.  I''m trying to find a way to do something like that though. ;  Try including strings.h or cstrings.h in your code. I know in Borland 5.02 you had to use cstrings in order to use the functions in this class.    --{You fight like a dairy farmer!} ;quote:Original post by AliasNotFound    std::string str = "some strin";char letter = 'g';std::string anotherString = str + letter;Does not work, but it's not supposed to.Yes, that example does work.edit: wish there was an easier way to quote... unkn.Enigma1625  [edited by - Enigma1625 on July 31, 2003 5:25:20 PM];quote:Yes, that example does work.edit: wish there was an easier way to quote...dang, you''re right.  I guess that''s what I get for trying to be like my computer science professors and present code before seeing if it compiles correctly first. Anyway, here is what I did that didn''t work, I could not combine string literals with a single character#include <iostream>#include <string>using namespace std;void main(){   char letter = ''g'';   string str = "''" + letter + "''"; // this doesn''t work   cout << anotherString << endl;}  But this does work...#include <iostream>#include <string>using namespace std;void main(){   char letter = ''g'';   string str = "''";   string anotherString = str + letter + str;   cout << anotherString << endl;} ;quote:Original post by AliasNotFound  quote:Yes, that example does work.edit: wish there was an easier way to quote...dang, you''re right.  I guess that''s what I get for trying to be like my computer science professors and present code before seeing if it compiles correctly first. Anyway, here is what I did that didn''t work, I could not combine string literals with a single character#include <iostream>#include <string>using namespace std;void main(){   char letter = ''g'';   string str = "''" + letter + "''"; // this doesn''t work   cout << anotherString << endl;}    The reason this doesn''t work? Think about how it will be evaluated. The compiler will first try to evaluate "''" + letter + "''". There''s the problem... you can''t perform concatenation on char array''s like that.;quote:string str = "'" + letter + "'"; // this doesn't workThat's because you're trying to concatenate dumb types, for which concatenation is not defined. "'" is char array, not a string object. Letter is a char, not a string object. You can get what you want by explicitely saying it.string str = string("'") + letter + "'";Then it can first use:string operator+(const string& lhs, char rhs);And then finish the job with:string operator+(const string& lhs, char* rhs);You see, the compiler just needs a little push so that it can understand what you're talking about, and then it can deduce the rest. With plain arrays and basic types, it can't know what you want.   [edited by - civguy on July 31, 2003 6:37:40 PM];  Thanks everyone, I understand now.