请问C++ 中string类型变量中有一个swap()库函数,请问怎么用?
来源:百度知道 编辑:UC知道 时间:2024/06/05 15:20:25
string的成员函数swap是用来交换两个string的值得
而你处理的是同一个string内的两个不同的元素
所以你得不到你想要的结果。
#include <string>
#include <iostream>
using namespace std;
int main()
{
string name("这是先前的");
cout<<name<<endl;
string swapname("这是调换后的");
name.swap(swapname);
cout<<name<<endl;
return 0;
}
stl中的说明
// Swap.
#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
template <class _CharT, class _Traits, class _Alloc>
inline void swap(basic_string<_CharT,_Traits,_Alloc>& __x,
basic_string<_CharT,_Traits,_Alloc>& __y) {
__x.swap(__y);
}
#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
类中的申明
void swap(basic_string& __s) {
__STD::swap(_M_start, __s._M_start);
__STD::swap(_M_finish, __s._M_finish);
__STD::swap(_M_end_of_storage, __s._M_end_