cplusplus.com
C++ : Reference : Strings library : string : operator=
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
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
string
string::string
member constants:
string::npos
member functions:
string::append
string::assign
string::at
string::begin
string::capacity
string::clear
string::compare
string::copy
string::c_str
string::data
string::empty
string::end
string::erase
string::find
string::find_first_not_of
string::find_first_of
string::find_last_not_of
string::find_last_of
string::get_allocator
string::insert
string::length
string::max_size
string::operator+=
string::operator=
string::operator[]
string::push_back
string::rbegin
string::rend
string::replace
string::reserve
string::resize
string::rfind
string::size
string::substr
string::swap


string::operator=

public member function
string& operator= ( const string& str );
string& operator= ( const char* s );
string& operator= ( char c );

String assignment

Sets a copy of the argument as the new content for the string object.

The previous content is dropped.

The assign member function provides a similar functionality with additional options.

Parameters

str
string object. A copy of the content of this object is used as the new content for the object.
s
A pointer to an array containing a null-terminated character sequence (C string), which is copied as the new content for the object.
c
Character. The content is set to a single character.

Return Value

*this

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// string assigning
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str1, str2, str3;
  str1 = "Test string: ";   // c-string
  str2 = 'x';               // single character
  str3 = str1 + str2;       // string

  cout << str3  << endl;
  return 0;
}


Output:
Test string: x

Basic template member declarations

( basic_string<charT,traits,Allocator> )
1
2
3
4
5
basic_string<charT,traits,Allocator>&
  operator= ( const basic_string<charT,traits,Allocator>& str );
basic_string<charT,traits,Allocator>&
  operator= ( const charT* s );
basic_string<charT,traits,Allocator>& operator= ( char c );


See also