目录
四、insert和erase,以及查找find(find不属于vector,是stl提供的算法)
vector是可变大小数组的序列容器

- //构造
- int TestVector1()
- {
- //无参构造
- vector<int> v1;
- //构造并初始化n个val
- vector<int> v2(4, 100);
- //使用迭代器进行初始化构造
- vector<int> v3(v2.begin(), v2.end());
- //拷贝构造
- vector<int> v4(v3);
- return 0;
- }
- //迭代器
- void PrintVector(const vector<int>& v)
- {
- //遍历打印
- vector<int>::const_iterator it = v.begin();
- while (it != v.end())
- {
- cout << *it << " ";
- ++it;
- }
- cout << endl;
- }
- void TestVector2()
- {
- vector<int> v;
- v.push_back(1);
- v.push_back(2);
- v.push_back(3);
- v.push_back(4);
- //遍历打印
- vector<int>::iterator it = v.begin();
- while (it != v.end())
- {
- cout << *it << " ";
- ++it;
- }
- cout << endl;
- //修改
- it = v.begin();
- while (it != v.end())
- {
- *it *= 2;
- ++it;
- }
- //使用反向迭代器进行遍历再打印
- vector<int>::reverse_iterator rit = v.rbegin();
- while (rit != v.rend())
- {
- cout << *rit << " ";
- ++rit;
- }
- cout << endl;
- PrintVector(v);
- }
- void TestVector4()
- {
- vector<int> v;
- v.push_back(1);
- v.push_back(2);
- v.push_back(3);
- v.push_back(4);
- auto it = v.begin();
- while (it != v.end())
- {
- cout << *it << " ";
- ++it;
- }
- cout << endl;
-
- v.pop_back();
- v.pop_back();
- it = v.begin();
- while (it != v.end())
- {
- cout << *it << " ";
- ++it;
- }
- cout << endl;
- }
- void TestVector5()
- {
- vector<int> v{ 1,2,3,4 };
- auto pos = find(v.begin(), v.end(), 3);
- if (pos != v.end())
- {
- v.insert(pos, 30);
- }
- vector<int>::iterator it = v.begin();
- while (it != v.end())
- {
- cout << *it << " ";
- ++it;
- }
- cout << endl;
- pos = find(v.begin(), v.end(), 3);
- // 删除pos位置的数据
- v.erase(pos);
-
- it = v.begin();
- while (it != v.end()) {
- cout << *it << " ";
- ++it;
- }
- cout << endl;
-
- }
- void TestVector6()
- {
- vector<int> v{ 1,2,3,4 };
- //通过[]读写第0个位置
- v[0] = 10;
- cout << v[0] << endl;
- //使用for+[]方式遍历
- for (size_t i = 0; i < v.size(); ++i)
- {
- cout << v[i] << " ";
- }
- cout << endl;
- vector<int> swapv;
- swapv.swap(v);
- //使用迭代器遍历
- auto it = swapv.begin();
- while (it != swapv.end())
- {
- cout << *it << " ";
- ++it;
- }
-
- // 3. 使用范围for遍历
- for (auto x : v)
- cout << x << " ";
- cout << endl;
- }
- #define _CRT_SECURE_NO_WARNINGS
- #include<iostream>
- using namespace std;
- #include<vector>
-
- //构造
- int TestVector1()
- {
- //无参构造
- vector<int> v1;
- //构造并初始化n个val
- vector<int> v2(4, 100);
- //使用迭代器进行初始化构造
- vector<int> v3(v2.begin(), v2.end());
- //拷贝构造
- vector<int> v4(v3);
- return 0;
- }
-
- //迭代器
- void PrintVector(const vector<int>& v)
- {
- //遍历打印
- vector<int>::const_iterator it = v.begin();
- while (it != v.end())
- {
- cout << *it << " ";
- ++it;
- }
- cout << endl;
- }
- void TestVector2()
- {
- vector<int> v;
- v.push_back(1);
- v.push_back(2);
- v.push_back(3);
- v.push_back(4);
- //遍历打印
- vector<int>::iterator it = v.begin();
- while (it != v.end())
- {
- cout << *it << " ";
- ++it;
- }
- cout << endl;
- //修改
- it = v.begin();
- while (it != v.end())
- {
- *it *= 2;
- ++it;
- }
- //使用反向迭代器进行遍历再打印
- vector<int>::reverse_iterator rit = v.rbegin();
- while (rit != v.rend())
- {
- cout << *rit << " ";
- ++rit;
- }
- cout << endl;
- PrintVector(v);
- }
- //尾插和尾删
- void TestVector4()
- {
- vector<int> v;
- v.push_back(1);
- v.push_back(2);
- v.push_back(3);
- v.push_back(4);
- auto it = v.begin();
- while (it != v.end())
- {
- cout << *it << " ";
- ++it;
- }
- cout << endl;
-
- v.pop_back();
- v.pop_back();
- it = v.begin();
- while (it != v.end())
- {
- cout << *it << " ";
- ++it;
- }
- cout << endl;
- }
- void TestVector5()
- {
- vector<int> v{ 1,2,3,4 };
- auto pos = find(v.begin(), v.end(), 3);
- if (pos != v.end())
- {
- v.insert(pos, 30);
- }
- vector<int>::iterator it = v.begin();
- while (it != v.end())
- {
- cout << *it << " ";
- ++it;
- }
- cout << endl;
- pos = find(v.begin(), v.end(), 3);
- // 删除pos位置的数据
- v.erase(pos);
-
- it = v.begin();
- while (it != v.end()) {
- cout << *it << " ";
- ++it;
- }
- cout << endl;
-
- }
- void TestVector6()
- {
- vector<int> v{ 1,2,3,4 };
- //通过[]读写第0个位置
- v[0] = 10;
- cout << v[0] << endl;
- //使用for+[]方式遍历
- for (size_t i = 0; i < v.size(); ++i)
- {
- cout << v[i] << " ";
- }
- cout << endl;
- vector<int> swapv;
- swapv.swap(v);
- //使用迭代器遍历
- auto it = swapv.begin();
- while (it != swapv.end())
- {
- cout << *it << " ";
- ++it;
- }
-
- // 3. 使用范围for遍历
- for (auto x : v)
- cout << x << " ";
- cout << endl;
- }
- int main()
- {
- TestVector1();
- TestVector2();
- TestVector4();
- TestVector5();
- TestVector6();
-
- return 0;
- }

拜拜啦,又是朝着money奋斗的一天!