Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Strings library
char_traits
classes:
string
global functions:
getline
operator+
operator<<
operator>>
comparison operators
swap


operator+

function
<string>
string operator+ (const string& lhs, const string& rhs);
string operator+ (const char* lhs, const string& rhs);
string operator+ (char lhs, const string& rhs);
string operator+ (const string& lhs, const char* rhs);
string operator+ (const string& lhs, char rhs);

Add strings

Returns a string object whose contents are the combination of the content of lhs followed by those of rhs.

Because the diversity of left-hand parameter types, this function is implemented as an overload of the global operator+ function.

Parameters

lhs
A string object, a c-string or a character. Its content forms the beginning of the returned object.
rhs
If lhs is a string object, this is another string object, a c-string or a character.
If lhs is not a string object, this is a string object.
In either case, the content of rhs follows that of lhs in the returned object.
Notice that at least one of the arguments used has to be a string object.

Return value

A string object with the content of both lhs and rhs.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// adding strings
#include <iostream>
#include <string>
using namespace std;
main ()
{
  string firstlevel ("com");
  string secondlevel ("cplusplus");
  string scheme ("http://");
  string hostname;
  string url;
  hostname = "www." + secondlevel + '.' + firstlevel;
  url = scheme + hostname;
  cout << url << endl;
  return 0;
}


Output:
http://www.cplusplus.com

Basic template declaration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
template<class charT, class traits, class Allocator>
  basic_string<charT,traits,Allocator>
    operator+ (const basic_string<charT,traits,Allocator>& lhs,
               const basic_string<charT,traits,Allocator>& rhs);
template<class charT, class traits, class Allocator>
  basic_string<charT,traits,Allocator>
    operator+ (const charT* lhs,
               const basic_string<charT,traits,Allocator>& rhs);
template<class charT, class traits, class Allocator>
  basic_string<charT,traits,Allocator>
    operator+ (charT lhs,
               const basic_string<charT,traits,Allocator>& rhs);
template<class charT, class traits, class Allocator>
  basic_string<charT,traits,Allocator>
    operator+ (const basic_string<charT,traits,Allocator>& lhs,
               const charT* rhs);
template<class charT, class traits, class Allocator>
  basic_string<charT,traits,Allocator>
    operator+ (const basic_string<charT,traits,Allocator>& lhs,
               charT rhs);


See also