MCPcopy Create free account
hub / github.com/EricPengShuai/Interview / main

Function main

STL/vector.cpp:15–61  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13}
14
15int main()
16{
17 /*
18 vector相关:
19 默认初始化,vector 为空, size 为0,适用于动态元素
20 其他初始化方法
21 vector<int> vt1(vt);
22 vector<int> vt2(7);
23 vector<int> vt3(vt.begin(), vt.end());
24 vector<int> vt4(7, 4);
25 */
26 vector<int> vt;
27
28 vt.push_back(1);
29 vt.push_back(2);
30 vt.__emplace_back(3); // c++17提出
31 vt.pop_back(); // 弹出最后一个元素
32
33
34 // find指定元素 包含在头文件algorithm中
35 vector<int>::iterator it = find(vt.begin(), vt.end(), 2);
36 if (it != vt.end()) {
37 cout << "Find(2) in " << *it << endl;
38 } else {
39 cout << "Can't find(2)" << endl;
40 }
41
42 // remove相关: http://c.biancheng.net/view/429.html
43 vector<int> v; // 建立一个vector<int> 用1-10填充它
44 v.reserve(10); // 这个只是改变capicity() https://blog.csdn.net/amusi1994/article/details/81106455
45 for (int i = 1; i <= 10; ++i) {
46 v.push_back(i);
47 }
48 cout << "init vector:" << endl;
49 showVector(v);
50 v[1] = v[3] = v[5] = 99;
51 cout << "change element:" << endl;
52 showVector(v);
53 auto iter = remove(v.begin(), v.end(), 99); // 删除所有等于99的元素,其实并不是
54 cout << "remove(99):" << endl;
55 showVector(v);
56 v.erase(iter, v.end()); // 这才是真正的删除了
57 cout << "erase(99):\n";
58 showVector(v);
59 cout << endl;
60 return 0;
61}

Callers

nothing calls this directly

Calls 3

showVectorFunction · 0.85
pop_backMethod · 0.80
push_backMethod · 0.45

Tested by

no test coverage detected