cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Strings library : operator+
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Strings library
char_traits
classes:
· string
global functions:
· getline
· operator+
· operator<<
· operator>>
· comparison operators
· swap

-

operator+ function
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);
<string>

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

// 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

template<class charT, chass 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, chass traits, class Allocator>
  basic_string<charT,traits,Allocator>
    operator+ (const charT* lhs,
               const basic_string<charT,traits,Allocator>& rhs);
template<class charT, chass traits, class Allocator>
  basic_string<charT,traits,Allocator>
    operator+ (charT lhs,
               const basic_string<charT,traits,Allocator>& rhs);
template<class charT, chass traits, class Allocator>
  basic_string<charT,traits,Allocator>
    operator+ (const basic_string<charT,traits,Allocator>& lhs,
               const charT* rhs);
template<class charT, chass traits, class Allocator>
  basic_string<charT,traits,Allocator>
    operator+ (const basic_string<charT,traits,Allocator>& lhs,
               charT rhs);

See also

string::append Append to string (public member function)
string::assign Assign content to string (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us