vector的swap用法
(2014-07-30 22:05:13)
标签:
vectorswap |
分类: C /C语言 |
-
template
<</SPAN>class T> voidswap ( T& a, T& b ) -
{
-
T c(a); a=b; b=c; -
}
-
但是vector的swap成员函数是怎么实现的?
- void
swap(vector<_Tp, _Alloc>& __x) { -
__STD::swap(_M_start, __x._M_start); -
__STD::swap(_M_finish, __x._M_finish); -
__STD::swap(_M_end_of_storage, __x._M_end_of_storage); -
}
void swap(vector<_Tp, _Alloc>& __x) { __STD::swap(_M_start, __x._M_start); __STD::swap(_M_finish, __x._M_finish); __STD::swap(_M_end_of_storage, __x._M_end_of_storage); }
仅仅是交换了指向的首尾指针和容量指针
-
-
-
#include
-
#include
-
#include
-
#include
-
-
using
namespace std; -
-
int
main () -
{
-
-
int x //= 10, y = 20; x:10 y:20 -
swap(x, y); // x:20 y:10 -
-
vector<<SPAN style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; BORDER-RIGHT-STYLE: none; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(46,139,87); BORDER-LEFT-STYLE: none; FONT-WEIGHT: bold; PADDING-TOP: 0px" class=datatypes>int first //(4, x), second (6, y); first:4x20 second:6x10 -
swap(first, second); // first:6x10 second:4x20 -
-
cout << "first contains:" ; -
//使用一般的iterator方式输出first -
for (vector<</SPAN>int>::iterator it=first.begin(); it != first.end(); ++it) -
{ -
cout << " " << *it; -
} -
cout << endl; -
-
cout << "second contains: ;" -
//使用copy()来实现second的输出 -
copy(second.begin(), second.end(), ostream_iterator<<SPAN style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; BORDER-RIGHT-STYLE: none; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(46,139,87); BORDER-LEFT-STYLE: none; FONT-WEIGHT: bold; PADDING-TOP: 0px" class=datatypes>int " " )); -
cout << endl; -
-
return 0; -
}
-
vector<
T >().swap(X)
-
-
-
#include
-
#include
-
#include
-
#include
-
-
using
namespace std; -
-
int
main () -
{
-
int x = 10; -
vector<<SPAN style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; BORDER-RIGHT-STYLE: none; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(46,139,87); BORDER-LEFT-STYLE: none; FONT-WEIGHT: bold; PADDING-TOP: 0px" class=datatypes>int myvector(10000, x); -
-
//这里打印仅仅是元素的个数不是内存大小 -
cout << "myvector size:" -
<< myvector.size() -
<< endl; -
-
//swap交换函数释放内存:vector().swap(X); -
//T:int ; myvertor代表X -
vector<<SPAN style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; BORDER-RIGHT-STYLE: none; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; BORDER-TOP-STYLE: none; COLOR: rgb(46,139,87); BORDER-LEFT-STYLE: none; FONT-WEIGHT: bold; PADDING-TOP: 0px" class=datatypes>int -
-
//两个输出仅用来表示swap前后的变化 -
cout << "after swap :" -
<< myvector.size() -
<< endl; -
-
return 0; -
}
std::vector().swap(X)
作用相当于:
{
std::vector
temp(X); temp.swap(X);
}
可以用类似的方法实现vector和string的适当收缩
- vector<</SPAN>int>
vec(100000, 0); - for
( inti = 0; i < 100000-2; ++i) vec.pop_back(); - cout
<< vec.capacity() <<endl; - vector<</SPAN>int>(vec).swap(vec);
- cout
<< vec.capacity() << endl; vector vec(100000, 0); for (int i = 0; i < 100000-2; ++i) vec.pop_back(); cout << vec.capacity() <<endl; vector(vec).swap(vec); cout << vec.capacity() << endl;
注:本文转自:http://blog.csdn.net/sunmenggmail/article/details/8605538