The C++ swap() member function 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 swap() member function to exchange the arrays of characters of two strings in C++ programming
To show: How to use the C++ swap() member function to exchange the arrays of characters of two strings in C++ programming
// the C++ swap() member function program example
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
// declaring an object of type basic_string<char>
string str1("StringOne");
string str2("StringTwo");
cout<<"Before swapping string str1 and str2:"<<endl;
cout<<"str1 string is = "<<str1<<endl;
cout<<"str2 string is = "<<str2<<endl;
swap(str1, str2);
cout<<"\nOperation: swap(str1, str2)"<<endl;
cout<<"After swapping string str1 and str2:"<<endl;
cout<<"str1 string is = "<<str1<<endl;
cout<<"str2 string is = "<<str2<<endl;
return 0;
}
Output example:
Before swapping string str1 and str2:
str1 string is = StringOne
str2 string is = StringTwo
Operation: swap(str1, str2)
After swapping string str1 and str2:
str1 string is = StringTwo
str2 string is = StringOne
Press any key to continue . . .