字元串的交換是指将兩個字元串的内容互相交換。
字元串複制是指生成一個新的字元串其内容為原有字元串的内容。
#include <iostream>
int main()
{
std::string str1 = "Hello ";
std::string str2 = "World!";
//以下将字元串str1的内容與字元串str2的内容進行交換
swap(str1,str2);
std::cout << str1 << std::endl;
std::cout << str2 << std::endl;
std::string str3 = "Hello World!";
std::string str4;
//以下将字元串str3中的所有字元複制到字元串str4中
str4.assign(str3);
std::cout <<str4 << std::endl;
std::string str5;
//以下是将字元串str3中的第2個字元開始連續8個字元複制到字元串str5中。
str5.assign(str3,2,8);
std::cout << str5 << std::endl;
return 0;
}