字符串的交换是指将两个字符串的内容互相交换。
字符串复制是指生成一个新的字符串其内容为原有字符串的内容。
#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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)