forked from zjpzhao/JLU_data_structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstl-vector.cpp
More file actions
56 lines (42 loc) · 949 Bytes
/
stl-vector.cpp
File metadata and controls
56 lines (42 loc) · 949 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int>a;
vector<int>b(10);
vector<char>c(5,6);
vector<char>d(c);
vector<int>v;
v.reserve(10); //预留空间,节省时间,size不变
cout<<v.size()<<endl;
v.push_back(9);
v.push_back(1);
v.push_back(2);
//v.pop_back();
vector<int>::iterator itr=v.begin();
v.insert(itr+1,1,5); //在迭代器之后插入n个元素,迭代器可以用做加法移动
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
cout<<endl;
v.erase(itr);
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
cout<<endl;
v.erase(itr,itr+2); //删除从第一个迭代器开始,到第二个迭代器之前的所有元素,注意野指针
for(int i=0;i<v.size();i++)
cout<<v[i]<<" ";
cout<<endl;
v.empty();
v.size();
v.clear();
v.front();
v.back();
v.begin();
v.end();
/*
v.max_size();
v.capacity();
*/
return 0;
}