十五.STL(Standard Template Library) 之vector 容器
代码片内有详解
:
1.Code
:
#include<iostream>#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
// STL (Standard Template Library : 标准模板库)
// Standard Template Library 中有六大部分:
// 容器 算法 迭代器 仿函数 适配器(配接器) 空间配置器
// 一. vector (向量)容器
void Display(int sd)
{
cout << sd << endl;
}
void test1()
{
//创建
vector<int> V; //创建 vector 容器对象,并且通过模板参数 指定容器中数据类型
V.push_back(1);
V.push_back(2);
V.push_back(3);
V.push_back(4);
//每一容器都有对应的 自己的 迭代器,用以 遍历 容器的元素
//V.begin(); // 返回迭代器 ,指向容器中的第一个数据
//V.end(); // 返回迭代器 ,指向最后一个元素的 下一个位置
//vector<int>::iterator; //得到 vector<int> 这种容器的迭代器 类型
vector<int>::iterator VBegin = V.begin(); // 指向容器 第一 位
vector<int>::iterator VEnd = V.end(); //指向 容器最后一位的后一位
// 第一种遍历方式
while (VBegin != VEnd)
{
cout << *VBegin << endl;
VBegin++;
}
//第二种遍历方式
for (vector<int>::iterator it = V.begin(); it != V.end(); it++)
{
cout << *it << endl;
}
//第三种方式
//由 STL 提供标准遍历算法, 头文件 algorithm
for_each(V.begin(), V.end(), Display);
}
// 二. vector 存放自定义数据类型
class cat
{
public:
cat(string nam, int ag)
{
name = nam;
age = ag;
}
string name;
int age;
};
void test2()
{
vector <cat> ccc;// 存对象
cat cat1("葬花", 78);
cat cat2("莲华", 45);
cat cat3("风花", 75);
cat cat4("雪月", 23);
ccc.push_back(cat1);
ccc.push_back(cat2);
ccc.push_back(cat3);
ccc.push_back(cat4);
for (vector<cat>::iterator it = ccc.begin(); it != ccc.end(); it++)
{
//对 it 解指针后直接得到 类
cout << "Name:" << (*it).name << " Age:" << (*it).age << endl;
}
vector <cat*> cc;// 存对象 指针
cat ccat1("葬花", 78);
cat ccat2("莲华", 45);
cat ccat3("风花", 75);
cat ccat4("雪月", 23);
cc.push_back(&ccat1);
cc.push_back(&ccat2);
cc.push_back(&ccat3);
cc.push_back(&ccat4);
for (vector<cat*>::iterator it = cc.begin(); it != cc.end(); it++)
{
//对 it 解指针后得到 类的指针
cat* cat5 = (*it);
cout << "Name:" << cat5->name << " Age:" << cat5->age << endl;
}
}
// 三. vector 容器的嵌套容器
void test3()
{
vector<vector<int>> v;//创建存放的数据为 容器 的 容器 对象.
vector<int> v1;//创建 存放int 类型数据的容器对象
vector<int> v2;
vector<int> v3;
vector<int> v4;
for (int i = 0; i < 4; i++)
{
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 ir = (*it).begin(); ir != (*it).end(); ir++)
{
cout << *ir << " ";
}
cout << endl;
}
}
// 四. vector 赋值
//函数原型:
//vector& operator=(const vector& vec);//重载等号操作符
//assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
//assign(n, elem); //将n个 elem 拷贝赋值给本身。
void Printf_v(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test4()
{
vector<int> v1;
v1.assign(10,5);//将10个5赋值给v1
Printf_v(v1);
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
Printf_v(v1);
vector<int> v2;
v2 = v1;
Printf_v(v2);
vector<int> v3;
v3.assign(v1.begin(),v1.end());// [beg, end)区间中的数据拷贝赋值给本身。
Printf_v(v3);
}
// 五. vector 容量和大小
//函数原型:
//
//empty(); //判断容器是否为空
//
//capacity(); //容器的容量
//
//size(); //返回容器中元素的个数
//
//resize(int num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。
//
// //如果容器变短,则末尾超出容器长度的元素被删除。
//
//resize(int num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
// //如果容器变短,则末尾超出容器长度的元素被删除
void test5()
{
vector<int> v1;
v1.clear();
cout << "v1.empty() = " << v1.empty() << endl;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
Printf_v(v1);
cout << v1.empty() << endl;
if (v1.empty())
{
cout << "空" << endl;
}
else
{
cout << "非空 = " << v1.empty() << endl;
cout << "v1 容量 = " << v1.capacity() << endl;
cout << "v1 大小 = " << v1.size() << endl;
}
v1.resize(15,10);
//resize 重新指定大小 ,若指定的更大,用10填充新位置,可以利用重载版本替换默认填充
Printf_v(v1);
v1.resize(5);
//resize 重新指定大小 ,若指定的更小,超出部分元素被删除
Printf_v(v1);
}
// 六. vector 插入 与 删除
//push_back(ele); //尾部插入元素ele
//pop_back(); //删除最后一个元素
//insert(const_iterator pos, ele); //迭代器指向位置pos插入元素ele
//insert(const_iterator pos, int count, ele);//迭代器指向位置pos插入count个元素ele
//erase(const_iterator pos); //删除迭代器指向的元素
//erase(const_iterator start, const_iterator end);//删除迭代器从start到end之间的元素
//clear(); //删除容器中所有元素
void test6()
{
vector<int> v1;
//尾插
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
Printf_v(v1);
//尾删
v1.pop_back();
Printf_v(v1);
//插入
v1.insert(v1.begin(), 100);
Printf_v(v1);
v1.insert(v1.begin(), 2, 1000);
Printf_v(v1);
//删除
v1.erase(v1.begin());
Printf_v(v1);
//清空
v1.erase(v1.begin(), v1.end());
v1.clear();
Printf_v(v1);
cout << "Clear" << endl;
}
// 七. vector 数据存取 , 互换 , 空间预留
//at(int idx); //返回索引idx所指的数据
//operator[]; //返回索引idx所指的数据
//front(); //返回容器中第一个数据元素
//back(); //返回容器中最后一个数据元素
//除了用迭代器获取vector容器中元素,[]和at也可以
//front返回容器第一个元素
//back返回容器最后一个元素
//swap(vec); // 将vec与本身的元素互换
//总结:swap可以使两个容器互换,可以达到实用的收缩内存效果
void test7()
{
vector<int> v1;
vector<int> v2;
for (int i = 0; i < 10; i++)//赋值
{
v1.push_back(i);
}
for (int i = 0; i < v1.size(); i++)//输出
{
cout << v1[i] << " ";
}
cout << endl;
for (int i = 0; i < v1.size(); i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
// 互 换
cout << "v1的第一个元素为: " << v1.front() << endl;// front : 前面
cout << "v1的最后一个元素为: " << v1.back() << endl;
for (int i = 0; i < 10; i++)//赋值
{
v2.push_back(i*10);
}
cout << "v1 v2" << endl;
for (int i = 0; i < 10; i++)//输出
{
cout << v1[i] << " ";
}
cout << endl;
for (int i = 0; i < 10; i++)//输出
{
cout << v2[i] << " ";
}
cout << endl;
v2.swap(v1);// 互 换
cout << "v1 v2 交换后" << endl;
for (int i = 0; i < 10; i++)//输出
{
cout << v1[i] << " ";
}
cout << endl;
for (int i = 0; i < 10; i++)//输出
{
cout << v2[i] << " " ;
}
cout << endl;
for (int i = 0; i < 100000; i++)
{
v1.push_back(i);
}
cout << "v的容量为:" << v1.capacity() << endl;
cout << "v的大小为:" << v1.size() << endl;
v1.resize(3);
cout << "v的容量为:" << v1.capacity() << endl;
cout << "v的大小为:" << v1.size() << endl;
vector<int>(v1).swap(v1); //匿名对象 //收缩内存
cout << "v的容量为:" << v1.capacity() << endl;
cout << "v的大小为:" << v1.size() << endl;
// 空间预留
vector<int> v3;
v3.reserve(10000); // 预留空间 reserve : 保留
int num = 0;
int* P = NULL;
for (int i = 0;i < 10000;i++)
{
v3.push_back(i);
if (P !=& v3[0])
{
P = &v3[0];
num++;
}
}
cout << "num:" << num << endl;
}
int main()
{
test1();
cout << "-------------------------------------------------------------------" << endl;
test2();
cout << "-------------------------------------------------------------------" << endl;
test3();
cout << "-------------------------------------------------------------------" << endl;
test4();
cout << "-------------------------------------------------------------------" << endl;
test5();
cout << "-------------------------------------------------------------------" << endl;
test6();
cout << "-------------------------------------------------------------------" << endl;
test7();
cout << "-------------------------------------------------------------------" << endl;
system("pause");
return 0;
}
2.The result of the run
:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FopgAU5x-1622442213858)(MarkDownPicture/vector1.png)]