#include
#include
using namespace std;
/*vector类 #include
// <---------- 单向操作
//vector初始化
void test1()
{
vector
for (int i = 0; i < 5; i++)
{
v1.push_back(i);
}
vector
vector
for (vector
{
cout << *it << endl;
}
char a[] = {'q', 'w', 'e', 'r'};
vector
for (vector
{
cout << (char)*ite << endl;
}
vector
for (vector
{
cout << (char)*it5 << " ";
}
}
//vector容器基本操作:增加
void test2()
{
vector
v.push_back('B'); //向容器v尾部插入一个‘B’
for (vector
{
cout << *it << " ";
}
cout << endl;
v.insert(v.begin(), 'C'); //向容器v.begin()位置插入C
for (vector
{
cout << *it << " ";
}
cout << endl;
v.insert(v.begin(), 5, 'D'); //向容器v.begin()位置插入5个D
for (vector
{
cout << *it << " ";
}
cout << endl;
vector
for (vector
{
if (*it == 'C')
{
v.insert(it, v2.begin(), v2.end()); //插入数据是在该位置的前面插入
break;
}
}
for (vector
{
cout << *it << " ";
}
cout << endl;
}
//vector删除数据
void test3()
{
vector
v.push_back('B'); //向容器v尾部插入一个‘B’
v.push_back('c');
v.push_back('d');
v.push_back('E');
v.push_back('F');
v.push_back('h');
v.push_back('i');
v.push_back('G');
cout << "原始数据:";
for (vector
{
cout <<*it << " ";
}
cout << endl;
cout << "删除指点位置的元素c:";
for (vector
{
if (*it == 'c')
{
v.erase(it); //删除指点位置的元素
break;
}
}
for (vector
{
cout << *it << " ";
}
cout << endl;
cout << "删除指点区间的元素:";
vector
for (vector
{
if (*it == 'B')
{
myBegin = it;
}
else if (*it == 'E')
{
myEnd = it;
v.erase(myBegin, myEnd); //删除指点区间的元素,begin会被删除,end不会被删除
break;
}
}
for (vector
{
cout << *it << " ";
}
cout << endl;
v.pop_back(); //删除最后一个元素
cout << "删除最后一个元素:";
for (vector
{
cout << *it << " ";
}
cout << endl;
v.clear(); //清空容器v
if (v.empty()) //判定是否容器v是否为空
{
cout << "v容器为空" << endl;
}
else
{
cout << "v容器不为空" << endl;
}
}
//vector查找遍历
void test4()
{
vector
v.push_back('B'); //向容器v尾部插入一个‘B’
v.push_back('c');
v.push_back('d');
v.push_back('E');
v.push_back('F');
v.push_back('h');
v.push_back('i');
v.push_back('G');
cout << "原始数据:";
for (vector
{
cout << *it << " ";
}
cout << endl;
cout << "查找位置2:" << v.at(2) << endl; //查找指定位置元素
cout << "首元素:" << v.front() << endl; //查找首元素
cout << "尾元素:" << v.back() << endl; //查找尾元素
//上面三个函数返回都是引用
v.begin(); //第一个元素迭代器
v.end(); //最后一个元素迭代器
v.rbegin(); //返回最后一个元素迭代器,反向迭代器
v.rend(); //返回第一个元素迭代器,反向迭代器
}
//vector容器大小
void test5()
{
vector
v.push_back('B'); //向容器v尾部插入一个‘B’
v.push_back('c');
v.push_back('d');
v.push_back('E');
v.push_back('F');
v.push_back('h');
v.push_back('i');
v.push_back('G');
sort(v.begin(), v.end());
for (vector
{
cout << *it;
}
cout << endl;
cout << v.size() << endl; //返回容器中元素的个数
cout << v.capacity() << endl; //返回当前容器中能容纳的最大元素值(已经分配空间)
cout << v.max_size() << endl; //返回最大可允许的元素数量(即可以分配空间存放的元素数量)
vector
cout << "交换之前v:";
for (vector
{
cout << *it;
}
cout << endl;
cout << "交换之前v1:";
for (vector
{
cout << *it;
}
cout << endl;
v.swap(v1); //交换容器v和V1的值
cout << "交换之后v:";
for (vector
{
cout << *it;
}
cout << endl;
cout << "交换之后v1:";
for (vector
{
cout << *it;
}
cout << endl;
v1.assign(5,'B'); //设置v1中5个元素为B
cout << "设置v1中5个元素为B:";
for (vector
{
cout << *it ;
}
cout << endl;
v1.assign(6, 'c'); //设置v1中5个元素为B
v.assign(v1.begin(), v1.end());
for (vector
{
cout << *it;
}
cout << endl;
}
int main()
{
//test1();
//test2();
//test3();
//test4();
test5();
return 0;
}