代码_各类基础操作Vector Map Set

一、vector基本操作

#include <iostream>
#include <vector> 
int main
{
//几种vector声明
vector<int>v1;        //定义空的vector
vector<int>v2(10);    //产生大小为10的vector
vector<int>v3(10, -1);    //产生大小为10,并且每个元素都是-1的vector
vector<int>v4(v3);        //用一个vector产生一个vecotr

int arr[5] = { 1, 2, 3, 4, 5 };
vector<int>v5(arr, &arr[5]);    //以区间[beg;end)做为初值的vector

cout << "当前元素数量" << v5.size() << endl;
cout << "v1大小是否为0:" << (v1.empty() ? "空" : "不空") << endl;
cout << "v2大小是否为0:" << (v2.empty() ? "空" : "不空") << endl;
cout << "v1可容纳的元素最大数量:" << v1.max_size() << endl;    //电脑内存4GB
cout << "v2可容纳的元素最大数量:" << v2.max_size() << endl;
cout << "v2重新分配前所能容纳的元素最大数:" << v2.capacity() << endl;
v2.reserve(20); //给v2重新分配20个空间
cout << "v2.reserve(20)后重新分配前所能容纳的元素最大数:" << v2.capacity() << endl;
cout << "v3 v4是否相等:" << (v3 == v4 ? "相等" : "不等") << endl;


v1.push_back(11);//在尾部添加一个元素1
v1.push_back(22);
v1.push_back(33);

//迭代器是随机存取迭代器,对vector来说就是指针,迭代器持续有效
//除非在一个较小索引位置插入删除元素或者内存重新分配
vector<int>::iterator ita; //声明一个迭代器
int i = 0;
for (ita = v1.begin(), i = 0; ita != v1.end(); i++, ita++)//v1.begin()指向v1的第一个元素,v1.end()指向最后元素的下一位置
{
cout << "v1中的" << i << "值:" << v1[i] << endl;
}
v1.pop_back();//在尾部删除一个元素
for (ita = v1.begin(), i = 0; ita != v1.end(); i++, ita++)
{
cout << "v1中的" << i << "值:" << v1[i] << endl;
}

//跟v1.begin()和v1.end()对应的有v1.rbegin()和v1.rend(),
//v1.begin()指向逆向的第一个元素,v1.end()指向逆向最后元素的下一位置,使用    vector<int>::reverse_iterator ita;

v2 = v1; //将v1的元素全部拷到v2
for (ita = v2.begin(), i = 0; ita != v2.end(); i++, ita++)
{
cout << "v2中的" << i << "值:" << v2[i] << endl;
}
//v2.clear();
v2.assign(3, 44);
for (ita = v2.begin(), i = 0; ita != v2.end(); i++, ita++)
{
cout << "v2中的" << i << "值:" << v2[i] << endl;
}
v2.assign(arr, &arr[5]);
for (ita = v2.begin(), i = 0; ita != v2.end(); i++, ita++)
{
cout << "v2中的" << i << "值:" << v2[i] << endl;
}

v1.swap(v2);
//swap(v1,v2);
cout << "v1第2个元素" << v1.at(1) << endl;    //越界抛出异常
//cout<<"v1第6个元素"<<v1[5]<<endl;  //越界,不检查
cout << "v1首个元素" << v1.front() << endl;
cout << "v1最后元素" << v1.back() << endl;

vector<int>::iterator pos = v1.begin();
v1.insert(pos, 11);
//v1.insert(pos,4,55); //如果直接用就是错的,因为迭代器失效了
//v1.insert(pos,arr,&arr[5]);
for (ita = v1.begin(), i = 0; ita != v1.end(); i++, ita++)
{
cout << "v1中的" << i << "值:" << v1[i] << endl;
}
cout << endl;
pos = v1.begin(); //因为直接用迭代器失效,所以重新声明下
v1.erase(++pos); //删除制定位置
for (ita = v1.begin(), i = 0; ita != v1.end(); i++, ita++)
{
cout << "v1中的" << i << "值:" << v1[i] << endl;
}
cout << endl;
pos = v1.begin();
v1.erase(pos, pos + 3);
for (ita = v1.begin(), i = 0; ita != v1.end(); i++, ita++)
{
cout << "v1中的" << i << "值:" << v1[i] << endl;
}
cout << endl;

//v1.resize(5); //将元素数量改成5,如果v1变大,则多的按默认走
v1.resize(5, 9); //将元素数量改成5,如果v1变大,则多的赋值成9
for (ita = v1.begin(), i = 0; ita != v1.end(); i++, ita++)
{
cout << "v1中的" << i << "值:" << v1[i] << endl;
}
cout << endl;

v1.clear(); //清空


system("pause");
return 0;
}

二、map基本操作

猜你喜欢

转载自blog.csdn.net/zhengbq_seu/article/details/82469040