C++ String Library - operator=



Description

It assigns a new value to the string, replacing its current contents.

Declaration

Following is the declaration for std::string::operator=

tring& operator= (const string& str);

Parameters

  • str − It is an another string object.

  • s − Pointer to an array of characters.

  • c − Character to fill the string.

  • il − It is an initializer_list object.

Return Value

It returns *this.

Exceptions

Never throw any exceptions.

Example

In below example for std::string::operator=.

#include <string>

int main () {
   std::string str1, str2, str3;
   str1 = "Test string: ";
   str2 = 'abc';
   str3 = str1 + str2;

   std::cout << str3  << '\n';
   return 0;
}

The sample output should be like this −

Test string: c  
string.htm
Advertisements