The basic_string member class, assign() C++ program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the basic_string class member, assign() to assign new character values to the contents of a string in C++ programming

To show: How to use assign() to assign new character values to the contents of a string in C++ programming

 

// the basic_string class member, assign() C++ example

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

// the first member function assigning the characters of a C-string to a string

string str1a;

const char *cstr1a = "Out There";

 

cout << "The C-string cstr1a is: " << cstr1a << "." << endl;

str1a.assign (cstr1a);

cout << "Assigning the C-string cstr1a to string str1 gives: "

<< str1a << "." << endl << endl;

 

// the second member function assigning a specific number of the of characters a C-string to a string

string str1b;

const char *cstr1b = "Out There";

cout << "The C-string cstr1b is: " << cstr1b << endl;

str1b.assign (cstr1b , 3);

cout << "Assigning the 1st part of the C-string cstr1b "

<< "to string str1 gives: " << str1b << "."

<< endl << endl;

 

// the third member function assigning a specific number of the characters from one string to another string

string str1c ("Hello "), str2c ("Wide World ");

cout << "The string str2c is: " << str2c << endl;

str1c.assign (str2c , 5 , 5);

cout << "The newly assigned string str1 is: "

<< str1c << "." << endl << endl;

 

// the fourth member function assigning the characters from one string to another string in two equivalent ways, comparing the assign and operator =

string str1d ("Hello"), str2d ("Wide"), str3d ("World");

cout << "The original string str1 is: " << str1d << "." << endl;

cout << "The string str2d is: " << str2d << endl;

str1d.assign (str2d);

cout << "The string str1 newly assigned with string str2d is: "

<< str1d << "." << endl;

cout << "The string str3d is: " << str3d << "." << endl;

str1d = str3d;

cout << "The string str1 reassigned with string str3d is: "

<< str1d << "." << endl << endl;

 

// the fifth member function assigning a specific number of characters of a certain value to a string

string str1e ("Hello ");

str1e.assign (4 , '!');

cout << "The string str1 assigned with exclamations is: "

<< str1e << endl << endl;

 

// the sixth member function assigning the value from the range of one string to another string

string str1f ("Hello "), str2f ("Wide World ");

cout << "The string str2f is: " << str2f << endl;

str1f.assign(str2f.begin() + 5 , str2f.end() - 1);

cout << "The string str1 assigned a range of string str2f is: "

<< str1f << "." << endl << endl;

 

return 0;

}

 

Output example:

 

The C-string cstr1a is: Out There.

Assigning the C-string cstr1a to string str1 gives: Out There.

 

The C-string cstr1b is: Out There

Assigning the 1st part of the C-string cstr1b to string str1 gives: Out.

 

The string str2c is: Wide World

The newly assigned string str1 is: World.

 

The original string str1 is: Hello.

The string str2d is: Wide

The string str1 newly assigned with string str2d is: Wide.

The string str3d is: World.

The string str1 reassigned with string str3d is: World.

 

The string str1 assigned with exclamations is: !!!!

 

The string str2f is: Wide World

The string str1 assigned a range of string str2f is: World.

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index