tags:

views:

421

answers:

7
int main()
{
  char d = 'd';
  std::string y("Hello worl");
  y.append(d); // this fails
  std::cout << y;
  return 0;
}

I also tried, but also failed.

int main()
{
  char d[1] = { 'd' };
  std::string y("Hello worl");
  y.append(d);
  std::cout << y;
  return 0;
}

Sorry for this dumb question, but I've searched around google, what I could see are just "char array to char ptr", "char ptr to char array", etc.

Any kind of help would be appreciated!

Thanks.

+5  A: 

Use push_back():

std::string y("Hello worl");
y.push_back('d')
std::cout << y;
Ferdinand Beyer
+7  A: 
y += d;

I would use += operator instead of named functions.

AraK
why do you consider += better than push_back? Is it just less typing or do you have another reason?
Glen
It's less typing. In `gcc`, `basic_string::operator+=` is just a call in `push_back`.
eduffy
It is more natural IMO for strings. push_back is container function, and a string is a specialized one in STL :)
AraK
Let's turn that question around: Why do you consider push_back better than +=? In my opinion, += is clear and concise.
Jesper
@Jesper, I don't. I was just wondering if AraK knew something I didn't.
Glen
+1  A: 

Try the += operator link text, append() method link text, or push_back() method link text

The links in this post also contain examples of how to use the respective APIs.

Michael Berg
+1  A: 

In addition to the others mentioned, one of the string constructors take a char and the number of repetitions for that char. So you can use that to append a single char.

std::string s = "hell";
s += std::string(1, 'o');
Brian R. Bondy
A: 

If you are using the push_back there is no call for the string constructor. Otherwise it will create a string object via casting, then it will add the character in this string to the other string. Too much trouble for a tiny character ;)

progician
operator +=(char c); is overloaded for strings. In fact, the string constructor doesn't accept one character, see Brian answer ;)
AraK
A: 

To add a char to a std::string var using the append method, you need to use this overload:

std::string::append(size_type _Count, char _Ch)

Edit : Your're right I misunderstood the size_type parameter, displayed in the context help. This is the number of chars to add. So the correct call is

s.append(1, d);

not

s.append(sizeof(char), d);

Or the simpliest way :

s += d;
Patrice Bernassola
I think using sizeof is not semantically correct here (even though it happens to work as sizeof(char) is always one). The append method is naturally more useful if you want to append more copies of the same character!!!!!!!!!!
UncleBens
Why are you using `sizeof(char)` instead of `1`? You want to append exactly one repetition of `d`, so just say that. Using `sizeof` here is misleading since it suggests that one has to tell `append` the byte size of the data type used (which is not the case).
Ferdinand Beyer
As Unclebens said, the append method is really more useful when adding the same character many times.
Patrice Bernassola
A: 

You don't need to search the web for good STL documentation.

One of the best sites is here: http://www.sgi.com/tech/stl/basic%5Fstring.html

Initially written by the crater of the STL.

Martin York