1.基本使用举例
下面的代码给出了vector的基本使用举例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;
typedef vector<
int
> INTVECTOR;
int
main(
void
)
{
vector<
int
> num;
// STL中的vector容器
int
element;
// 从标准输入设备读入整数,
// 直到输入的是非整型数据为止
while
(cin >> element)
//ctrl+Z 结束输入
num.push_back(element);
// STL中的排序算法
sort(num.begin(), num.end());
// 将排序结果输出到标准输出设备
for
(
int
i =
0
; i < num.size(); i ++)
cout << num[i] <<
" "
;
cout<<endl;
//vec1对象初始为空
INTVECTOR vec1;
//vec2对象最初有10个值为6的元素
INTVECTOR vec2(
10
,
6
);
//vec3对象最初有3个值为6的元素
INTVECTOR vec3(vec2.begin(),vec2.begin()+
3
);
//声明一个名为i的双向迭代器
INTVECTOR::iterator i;
//从前向后显示vec1中的数据
cout<<
"vec1.begin()--vec1.end():"
<<endl;
for
(i =vec1.begin(); i !=vec1.end(); ++i)
cout << *i <<
" "
;
cout << endl;
//从前向后显示vec2中的数据
cout<<
"vec2.begin()--vec2.end():"
<<endl;
for
(i =vec2.begin(); i !=vec2.end(); ++i)
cout << *i <<
" "
;
cout << endl;
//从前向后显示vec3中的数据
cout<<
"vec3.begin()--vec3.end():"
<<endl;
for
(i =vec3.begin(); i !=vec3.end(); ++i)
cout << *i <<
" "
;
cout << endl;
//测试添加和插入成员函数
vec1.push_back(
2
);
vec1.push_back(
4
);
vec1.insert(vec1.begin()+
1
,
5
);
//在第一个位置后插入5
vec1.insert(vec1.begin()+
1
,vec3.begin(),vec3.end());
//在第一个位置后插入ve3开始到结束的数字
cout<<
"push() and insert():"
<<endl;
for
(i =vec1.begin(); i !=vec1.end(); ++i)
cout << *i <<
" "
;
cout << endl;
//测试赋值成员函数
vec2.assign(
8
,
1
);
//重新给vec2赋了8个值1
cout<<
"vec2.assign(8,1):"
<<endl;
for
(i =vec2.begin(); i !=vec2.end(); ++i)
cout << *i <<
" "
;
cout << endl;
//测试引用类函数
cout<<
"vec1.front()="
<<vec1.front()<<endl;
cout<<
"vec1.back()="
<<vec1.back()<<endl;
cout<<
"vec1.at(4)="
<<vec1.at(
4
)<<endl;
cout<<
"vec1[4]="
<<vec1[
4
]<<endl;
//测试移出和删除
vec1.pop_back();
//最高位前移一位(去掉最高位)
cout<<
"vec1.pop_back():"
<<endl;
for
(i =vec1.begin(); i !=vec1.end(); ++i)
cout << *i <<
" "
;
cout << endl;
vec1.erase(vec1.begin()+
1
,vec1.end()-
2
);
//清除 vec1.begin()+1到vec1.end()-2
cout<<
"vec1.erase():"
<<endl;
for
(i =vec1.begin(); i !=vec1.end(); ++i)
cout << *i <<
" "
;
cout << endl;
//显示序列的状态信息
cout<<
"vec1.capacity(): "
<<vec1.capacity()<<endl;
//capacity() 告诉你最多添加多少个元素才会导致容器重分配内存
cout<<
"vec1.max_size(): "
<<vec1.max_size()<<endl;
cout<<
"vec1.size(): "
<<vec1.size()<<endl;
//size() 是告诉你容器当中目前实际有多少个元素
cout<<
"vec1.empty(): "
<<vec1.empty()<<endl;
//vector序列容器的运算
cout<<
"vec1==vec3: "
<<(vec1==vec3)<<endl;
//相等返回1,不相等返回0
cout<<
"vec1<=vec3: "
<<(vec1<=vec3)<<endl;
//小于等于返回1,否则返回0
system(
"pause"
);
return
0
;
}
|
2.内存分配与释放
1.vector的内存增长
vector其中一个特点:内存空间只会增长,不会减小。为了支持快速的随机访问,vector容器的元素以连续方式存放,每一个元素都紧挨着前一个元素存储。设想一下,当vector添加一个元 素时,为了满足连续存放这个特性,都需要重新分配空间、拷贝元素、撤销旧空间,这样性能难以接受。因此STL实现者在对vector进行内存分配时,其实际分配的容量要比当前所需的空间多一些。就是说,vector容器预留了一些额外的存储区,用于存放新添加的元素,这样就不必为每个新元素重新分配整个容器的内存空间。
在调用push_back时,每次执行push_back操作,相当于底层的数组实现要重新分配大小;这种实现体现到vector实现就是每当 push_back一个元素,都要重新分配一个大一个元素的存储,然后将原来的元素拷贝到新的存储,之后在拷贝push_back的元素,最后要析构原有的vector并释放原有的内存。例如下面程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include <iostream>#include <cstdlib>#include <vector>using namespace std;
class
Point
{
public
:
Point()
{
cout <<
"construction"
<< endl;
}
Point(
const
Point& p)
{
cout <<
"copy construction"
<< endl;
} ~Point()
{
cout <<
"destruction"
<< endl;
}
};
int
main()
{
vector<Point> pointVec;
Point a;
Point b;
pointVec.push_back(a);
pointVec.push_back(b);
cout<<pointVec.size()<<std::endl;
return
0
;
}
|
输出结果:
其中执行
pointVec.push_back(a);
此时vector会申请一个内存空间,并调用拷贝构造函数将a放到vector中
再调用
pointVec.push_back(b);
此时内存不够 需要扩大内存,重新分配内存 这时再调用拷贝构造函数将a拷贝到新的内存,再将b拷入新的内存,同时有人调用Point拷贝构造函数,最后释放原来的内存 此时调用Point的析构函数。
2.vector的内存释放
由于vector的内存占用空间只增不减,比如你首先分配了10,000个字节, 然后erase掉后面9,999个,留下一个有效元素,但是内存占用仍为10,000个。所有内存空间是在vector析构时候才能被系统回收。 empty()用来检测容器是否为空的,clear()可以清空所有元素。但是即使clear(),vector所占用的内存空间依然如故,无法保证内存 的回收。
如果需要空间动态缩小,可以考虑使用deque。如果vector,可以用swap()来帮助你释放内存。具体方法如下:
vector<Point>().swap(pointVec); //或者pointVec.swap(vector<Point> ())
标准模板:
template < class T >void ClearVector( vector< T >& vt ) { vector< T > vtTemp; veTemp.swap( vt ); }
swap()是交换函数,使vector离开其自身的作用域,从而强制释放vector所占的内存空间,总而言之,释放vector内存最简单的方 法是vector<Point>().swap(pointVec)。当时如果pointVec是一个类的成员,不能把 vector<Point>().swap(pointVec)写进类的析构函数中,否则会导致double free or corruption (fasttop)的错误,原因可能是重复释放内存。(前面的pointVec.swap(vector<Point> ())用G++编译没有通过)
3.其他情况释放内存
如果vector中存放的是指针,那么当vector销毁时,这些指针指向的对象不会被销毁,那么内存就不会被释放。如下面这种情况,vector中的元素时由new操作动态申请出来的对象指针:
#include <vector> using namespace std; vector<void *> v;
每次new之后调用v.push_back()该指针,在程序退出或者根据需要,用以下代码进行内存的释放:
for (vector<void *>::iterator it = v.begin(); it != v.end(); it ++) if (NULL != *it) { delete *it; *it = NULL; } v.clear();