#include<iostream>
using namespace std;
//vector容器初识 vector相当于一个数组容器
#include<vector>
#include<string>
//创建一个vecto容器
void test01()
{
vector<int> v;
//向容器中插入数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//遍历容器 it是个指针
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}
cout << "-------------------------------------------" << endl;
}
//vector容器中存放自定义数据类型
class person
{
public:
person(int age, string name)
{
this->m_age = age;
this->m_name = name;
}
string m_name;
int m_age;
};
void test02()
{
vector<person>v;
person p1(19, "刘庆斌");
person p2(19, "赵志超");
person p3(19, "汪荣秋");
person p4(19, "刘宸宇");
person p5(19, "张相龙");
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//遍历vector容器
for (vector<person>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:" << it->m_name << " " << "年龄:" << it->m_age << endl;
}
cout << "-------------------------------------------" << endl;
}
//vector容器存放指针
void test03()
{
vector<person*>v;
person p1(19, "刘庆斌");
person p2(19, "赵志超");
person p3(19, "汪荣秋");
person p4(19, "刘宸宇");
person p5(19, "张相龙");
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
//it在这里是指向指针的指针
for (vector<person*>::iterator it = v.begin(); it != v.end(); it++)
{
cout << "姓名:"<<(*it)->m_name << "年龄:"<<(*it)->m_age << endl;
}
cout << "-------------------------------------------" << endl;
}
//vector容器嵌套vector容器
void test04()
{
vector<vector<int>>v;//相当于一个二维数组
//创建小容器
vector<int>v1;
vector<int>v2;
vector<int>v3;
vector<int>v4;
//向小容器中添加数
for (int i = 0; i < 4; i++)
{
//二维数组的各个元素之间和循环次数有关系时才用这种方式输出
//一般的二维数组没有直接关系的vector容器不好表示 所以不建议用
v1.push_back(i + 1);
v2.push_back(i + 2);
v3.push_back(i + 3);
v4.push_back(i + 4);
}
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)//外层大循环
{
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)//内层小循环
{
cout << *vit << " ";
}
cout << endl;
}
}
void printvector(vector<int>& v)
{
for (vector<int>::iterator it=v.begin();it!=v.end();it++)
{
cout<<*it<<" ";
}
cout << endl;
cout << "-------------------" << endl;
}
//vector 的赋值操作
void test05()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i + 1);
}
printvector(v1);
//直接用等号的方式 重载operator=
vector<int>v2;
v2 = v1;
printvector(v2);
//利用assign赋值
v2.assign(v1.begin(),v1.end());
printvector(v2);
}
//vector容器的扩展和大小
void test06()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i + 1);
}
printvector(v1);
if (v1.empty())//如果v1.empty()=true 说明容器中是空的
{
cout << "v1为空" << endl;
}
else
cout << "v1不为空" << endl;//不为空我们输出他的容量和大小
cout << "v1的容量为:" << v1.capacity() << endl;
cout << "v1的大小为:" << v1.size() << endl;
//对vector容器进行扩充
v1.resize(15,10);//将容器的大小扩充到了十五 参数二用来改变默认值 表示用10来填充
printvector(v1);//如果扩充的比原来的长 则会默认用0来填充
cout<<"v1的容量为:" << v1.capacity() << endl;//计算容量 容量用于大于大小
}
//vector容器的插入删除
void test07()
{
vector<int>v1;
//尾插
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
printvector(v1);
//尾部删除
v1.pop_back();
printvector(v1);
//插入
v1.insert(v1.begin()+2, 2, 10);
printvector(v1);
//删除
v1.erase(v1.begin() + 2);
//清除
v1.clear();
}
void test08()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i + 1);
}
cout <<"容器中第一个数为:"<< v1.front() << endl;
cout <<"容器中最后一个数为:"<< v1.back() << endl;
}
//vector容器的互换
void test09()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printvector(v1);
vector<int>v2;
for (int i = 10; i > 0; i--)
{
v2.push_back(i);
}
printvector(v2);
v2.swap(v1);
printvector(v1);
printvector(v2);
}
void test10()
{
//巧用swap收缩内存
vector<int>v;
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
}
cout << "v的容量" << v.capacity() << endl;
cout << "v的大小" <<v.size() << endl;
//将容器大小变为3 但容器的容量不会变化 所以会导致内存浪费
v.resize(3);
cout << "v的容量" << v.capacity() << endl;
cout << "v的大小" << v.size() << endl;
vector<int>(v).swap(v);//创建一个虚拟容器 与其进行交换 虚拟容器使用后内存会立即释放
cout << "v的容量" << v.capacity() << endl;
cout << "v的大小" << v.size() << endl;
}
//vector容器的预留空间
void test11()
{
vector<int>v;
int num = 0;
int* p = NULL;
//如果不进行预留空间 当vector容器中数量很大时 容器会进行多次扩充 进程跑起来变慢
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
if (p != &v[0])
{
p = &v[0];
num++;
}
}
cout << "动态扩充的次数:" <<num<< endl;
//预留空间后 就不会多次动态扩充了
v.reserve(100000);
num = 0;
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
if (p != &v[0])
{
p = &v[0];
num++;
}
}
cout << "动态扩充的次数:" << num << endl;
}
int main()
{
test01();
test02();
test03();
test04();
test05();
test06();
test07();
test08();
test09();
test10();
test11();
return 0;
system("pause");
system("cls");
}
vector容器的用法
最新推荐文章于 2023-07-08 17:31:39 发布