目录
一、(constructor)构造函数声明
二、vector的迭代器
三、vector的增删改查
四、insert和erase,以及查找find(find不属于vector,是stl提供的算法)
五、三种遍历方式
六、源代码
vector是可变大小数组的序列容器
//构造
int TestVector1()
{//无参构造vector v1;//构造并初始化n个valvector v2(4, 100);//使用迭代器进行初始化构造vector v3(v2.begin(), v2.end());//拷贝构造vector v4(v3);return 0;
}
//迭代器
void PrintVector(const vector& v)
{//遍历打印vector::const_iterator it = v.begin();while (it != v.end()){cout << *it << " ";++it;}cout << endl;
}
void TestVector2()
{vector v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);//遍历打印vector::iterator it = v.begin();while (it != v.end()){cout << *it << " ";++it;}cout << endl;//修改it = v.begin();while (it != v.end()){*it *= 2;++it;}//使用反向迭代器进行遍历再打印vector::reverse_iterator rit = v.rbegin();while (rit != v.rend()){cout << *rit << " ";++rit;}cout << endl;PrintVector(v);
}
void TestVector4()
{vector 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 v{ 1,2,3,4 };auto pos = find(v.begin(), v.end(), 3);if (pos != v.end()){v.insert(pos, 30);}vector::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 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 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
using namespace std;
#include//构造
int TestVector1()
{//无参构造vector v1;//构造并初始化n个valvector v2(4, 100);//使用迭代器进行初始化构造vector v3(v2.begin(), v2.end());//拷贝构造vector v4(v3);return 0;
}//迭代器
void PrintVector(const vector& v)
{//遍历打印vector::const_iterator it = v.begin();while (it != v.end()){cout << *it << " ";++it;}cout << endl;
}
void TestVector2()
{vector v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);//遍历打印vector::iterator it = v.begin();while (it != v.end()){cout << *it << " ";++it;}cout << endl;//修改it = v.begin();while (it != v.end()){*it *= 2;++it;}//使用反向迭代器进行遍历再打印vector::reverse_iterator rit = v.rbegin();while (rit != v.rend()){cout << *rit << " ";++rit;}cout << endl;PrintVector(v);
}
//尾插和尾删
void TestVector4()
{vector 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 v{ 1,2,3,4 };auto pos = find(v.begin(), v.end(), 3);if (pos != v.end()){v.insert(pos, 30);}vector::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 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 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奋斗的一天!