Vector的几个基本操作
程序员文章站
2022-03-01 23:18:51
...
先贴代码,以后再附加文字说明,Vector的几个基本操作
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int x[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
vector<int> v(x, x+10);
vector<vector<int>> v2;
for (int i = 0; i < 10; i++)
{
v2.push_back(v);
}
cout << "input one" << endl;
for (int i = 0; i < 10; i++)
{
cout << v[i] << " ";
}
cout << endl;
cout << "input two" << endl;
vector<int>::iterator itr;
for (itr= v.begin() ; itr != v.end() ; itr++)
{
cout << *itr << " ";
}
cout << endl;
vector<int>::iterator itr2 = find(v.begin(), v.end(), 7);
cout << *itr2 << endl;
v.insert(v.begin(), 100);
vector<int>::iterator itr3;
for (itr3 = v.begin(); itr3 != v.end(); itr3++)
{
cout << *itr3 << " ";
}
cout << endl;
v.insert(v.begin(), 3, 100);
vector<int>::iterator itr4;
for (itr4 = v.begin(); itr4 != v.end(); itr4++)
{
cout << *itr4 << " ";
}
cout << endl;
v.erase(v.begin());
vector<int>::iterator itr5;
for (itr5 = v.begin(); itr5 != v.end(); itr5++)
{
cout << *itr5 << " ";
}
cout << endl;
v.erase(v.begin(),v.begin()+4);
vector<int>::iterator itr6;
for (itr6 = v.begin(); itr6 != v.end(); itr6++)
{
cout << *itr6 << " ";
}
cout << endl;
cout << "size" << v.size() << endl;
cout << "empty" << v.empty() << endl;
v.clear();
cout << "empty" << v.empty() << endl;
}
上一篇: vector用法
下一篇: vector动态数组的基本操作