The C++ assign() 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 C++ assign() to assign new character values to the contents of a string in C++ programming
To show: How to use the C++ assign() member function to assign new character values to the contents of a string in C++ programming
// the C++ assign(), string assignment program example
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
// assigning the characters of a C-string to a string
string str1, str3;
const char *str2 ="StRiNg assign()";
cout<<"str2, C string is: "<<str2<<endl;
str1.assign(str2);
cout<<"Operation: str1.assign(str2)"<<endl;
cout<<"Assigning the C-string str2 to str1 string: const char *str2 = \"StRiNg assign()\" "<<str1<<endl;
// assigning a number of a C-string characters to a string
const char *str4 = "Another StRiNg assign()";
cout<<"\nstr4 C string is: "<<str4<<endl;
str3.assign(str4, 11);
cout<<"Operation: str3.assign(str4, 11)"<<endl;
cout<<"Assigning some portion of the str4 to str3 string: "<<str3<<endl;
// assigning a number of the characters from one string to another string
string str5("First "), str6("Second sTrInG");
cout<<"\nstr5 string is: "<<str5<<endl;
cout<<"str6 string is: "<<str6<<endl;
str5.assign(str6, 7, 6);
cout<<"Operation: str5.assign(str6, 7, 6)"<<endl;
cout<<"Newly assigned str5 string is: "<<str5<<endl;
// assigning the characters from one string to another string in two equivalent ways, comparing the assign and operator =
string str7("First"), str8("Second"), str9("Third");
cout<<"\nstr7 string is: "<<str7<<endl;
cout<<"str8 string is: "<<str8<<endl;
cout<<"str9 string is: "<<str9<<endl;
str7.assign(str8);
cout<<"Operation: str7.assign(str8)"<<endl;
cout<<"Newly assigned str7 with str8 string is: "<<str7<<endl;
str7 = str9;
cout<<"Operation: str7 = str9"<<endl;
cout<<"String str7 reassigned with str9 string is: "<<str7<<endl;
// assigning a specific number of characters of a certain value to a string
string str10("Working STrInG");
cout<<"\nstr10 string is: "<<str10<<endl;
str10.assign(3,'!');
cout<<"Operation: str10.assign(3, '!')"<<endl;
cout<<"str10 string assigned with character '!' is: "<<str10<<endl;
// assigning a value from a range of one string to another string
string str11("Comes "), str12("the END ");
cout<<"\nstr11 string is: "<<str11<<endl;
cout<<"str12 string is: "<<str12<<endl;
str11.assign(str12.begin() + 4, str12.end() - 1);
cout<<"Operation: str11.assign(str12.begin()+4, str12.end()-1)"<<endl;
cout<<"str11 assigned a range of str12 string is: "<<str11<<endl;
return 0;
}
Output example:
str2, C string is: StRiNg assign()
Operation: str1.assign(str2)
Assigning the C-string str2 to str1 string: const char *str2 = "StRiNg assign()" StRiNg assign()
str4 C string is: Another StRiNg assign()
Operation: str3.assign(str4, 11)
Assigning some portion of the str4 to str3 string: Another StR
str5 string is: First
str6 string is: Second sTrInG
Operation: str5.assign(str6, 7, 6)
Newly assigned str5 string is: sTrInG
str7 string is: First
str8 string is: Second
str9 string is: Third
Operation: str7.assign(str8)
Newly assigned str7 with str8 string is: Second
Operation: str7 = str9
String str7 reassigned with str9 string is: Third
str10 string is: Working STrInG
Operation: str10.assign(3, '!')
str10 string assigned with character '!' is: !!!
str11 string is: Comes
str12 string is: the END
Operation: str11.assign(str12.begin()+4, str12.end()-1)
str11 assigned a range of str12 string is: END
Press any key to continue . . .