-4

If I have a string x='wow' in Python, I can concatenate this string with itself using the __add__ function, like so:

x='wow'  
x.__add__(x)  
'wowwow'

How can I do this in C++?

Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
mzn.rft
  • 839
  • 3
  • 13
  • 20

5 Answers5

7

Semantically, the equivalent of your python code would be something like

std::string x = "wow";
x + x;

i.e. create a temporary string which is the concatenation of x with x and throw away the result. To append to x you would do the following:

std::string x = "wow";
x += x;

Note the double quotes ". Unlike python, in C++, single quotes are for single characters, and double-quotes for null terminated string literals.

See this std::string reference.

By the way, in Python you wouldn't usually call the __add__() method. You would use the equivalent syntax to the first C++ example:

x = 'wow'
x + x

The __add__() method is just the python way of providing a "plus" operator for a class.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • The question is about `__add__`, which is `+` in C++, not `+=`. – daknøk Oct 22 '12 at 15:29
  • @daknøk I see your point. The title says "appending to strings", but the code concatenates two strings and returns the result. I will fix my examples. – juanchopanza Oct 22 '12 at 15:36
3

You can use a std::string and operator+ or operator+= or a std::stringstream with operator <<.

std::string x("wow");
x = x + x;
//or
x += x;

There's also std::string::append.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
3

You can use the + operator to concatenate strings in C++:

std::string x = "wow";
x + x; // == "wowwow"

In Python you can also use + instead of __add__ (and + is considered more Pythonic than .__add__):

x = 'wow'  
x + x # == 'wowwow'
daknøk
  • 646
  • 5
  • 11
  • So the equivalent to Python's `x.__add__(x)` in C++ would be `operator+(x,x)`, right? Only that I'm not sure offhand whether `+` for strings isn't a member. _Sigh._) – sbi Oct 22 '12 at 18:31
  • @sbi yes, Python’s `__add__` is equivalent to C++’ `operator+`. – daknøk Oct 22 '12 at 18:40
1
std::string x = "wow"
x = x + x;
Hakan Serce
  • 11,198
  • 3
  • 29
  • 48
-2

Normally, when concatenating two distinct strings, you can simply use operator+= on the string you want to append to:

string a = "test1";
string b = "test2";

a += b;

Will correctly yield a=="test1test2"

However in this case you can't simply append a string to itself, because the act of appending changes both the source and the destination. In other words, this is incorrect:

string x="wow";
x += x;

Instead, a simple solution is to just create a temporary (verbose for clarity):

string x = "wow";
string y = x;
y += x;

...and then swap it back:

x = y;
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • Why is `x += x` incorrect? The standard *requires* it to work. – R. Martinho Fernandes Oct 22 '12 at 15:41
  • @R.MartinhoFernandes: Where? – John Dibling Oct 22 '12 at 15:41
  • 1
    The effects of `operator+=` are defined as the same as the append function that takes a `char const*` and a size, described in §21.4.6.2/9. That is worded in a such a way that anything works. – R. Martinho Fernandes Oct 22 '12 at 15:43
  • The passage you refer to describes the *effects* of `append`, not the *mechanism* by which it works. There is no explicit dispensation given in the Standard to allow `append`ing a string to itself. Stepping through the code for `insert` under MSVC10 I see that it does work, and this is probably the case under most reasonable compilers -- but I still don't see anywhere in the Standard where it is *guaranteed* to be correct. – John Dibling Oct 22 '12 at 17:08
  • 1
    Those effects are given for all cases; no exceptions are listed. – R. Martinho Fernandes Oct 22 '12 at 17:10