Vector容器
数据结构和数组非常相似,也成为单端数组, 支持随机访问
不同之处:数组是静态空间,而vector可以动态扩展
动态扩展:不是在原有空间之后接新空间,而是找到更大的内存空间,然后将元数据拷贝到新空间
释放原空间
示例代码
#include<algorithm> //标准算法头文件
#include<vector>
using namespace std;
void myPrint(int val)
{
cout << val << endl;
}
void test01()
{
//创建一个vector容器
vector<int> v;
//像容器中插入数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
//通过迭代器访问容器中的数据
vector<int>::iterator itBegin = v.begin(); //起始迭代器,指向容器中第一个元素
vector<int>::iterator itEnd = v.end(); //结束迭代器,指向容器中最后一个元素的下一个位置
//第一种遍历方式
cout << "我是第一种遍历方式" << endl;
while (itBegin != itEnd)
{
cout << *itBegin << endl;
itBegin++;
}
//第二种遍历方式
cout << "我是第二种遍历方式" << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << endl;
}
//第三种遍历方式,利用STL提供的遍历算法
cout << "我是第三种遍历方式" << endl;
for_each(v.begin(), v.end(), myPrint);
}
int main()
{
test01();
std::cout << "Hello World!\n";
}
插入数据
#include<algorithm> //标准算法头文件
#include <iostream>
#include<vector>
#include<string>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test201()
{
vector<Person>v;
//向容器中添加数据
Person pl("aaa", 10);
Person p2("bbb", 10);
Person p3("ccc", 10);
Person p4("ddd", 10);
Person p5("eee", 10);
v.push_back(pl);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//遍历容器中的数据
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
std::cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << endl;
std::cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
}
}
//存放自定义数据类型 指针
void test202()
{
vector<Person*>v;
//向容器中添加数据
Person pl("aaa", 10);
Person p2("bbb", 10);
Person p3("ccc", 10);
Person p4("ddd", 10);
Person p5("eee", 10);
v.push_back(&pl);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
//遍历容器中的数据
for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++)
{
std::cout << "姓名:" << (*it)->m_Name << " 年龄:" << (*it)->m_Age << endl;
}
}
int main()
{
test201();
test202();
std::cout << "Hello World!!!!\n";
return 0;
}
容器嵌套
#include<algorithm> //标准算法头文件
#include <iostream>
#include<vector>
#include<string>
using namespace std;
//容器嵌套容器
void test301()
{
vector< vector<int> > v;
vector<int> v1;
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++)
{
//(*it)----容器vector<int>
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++)
{
cout << *vit << " ";
}
cout << endl;
}
}
int main()
{
test301();
system("pause");
return 0;
}
vector容器的构造函数
#include<algorithm> //标准算法头文件
#include <iostream>
#include<vector>
#include<string>
using namespace std;
/*函数原型:
vector<T> v; //采用模板实现类实现
vector(v.begin(), v.end) ; //将v[begin(), end()]区间中的元素拷贝给本身
vector(n, elem); //构造函数将n个elem拷贝给本身
vector(const vector &vec); //拷贝构造函数
*/
void printVector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector容器构造
void test401()
{
vector<int>v1; //默认构造,无参构造
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
/*printVector(v1);*/
//通过区间方式进行构造
vector<int>v2(v1.begin(), v1.end());
/*printVector(v2);*/
//n个elem方式构造10个100
vector<int>v3(10, 100);
/*printVector(v3);*/
//拷贝构造
vector<int>v4(v3);
/*printVector(v4);*/
}
int main()
{
test401();
system("pause");
}
赋值操作
#include<algorithm> //标准算法头文件
#include <iostream>
#include<vector>
#include<string>
using namespace std;
/*
函数原型:
vector& operator=const vector &vec); 重载等号操作符
assign(beg, end); 将[beg,end]区间中的数据拷贝赋值给本身
assign(n, elem); 将n个elem拷贝赋值给本身
*/
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test501()
{
vector<int>v1; //默认构造,无参构造
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
//赋值 opetator=
vector <int>v2;
v2 = v1;
printVector(v2);
//assign
vector<int>v3;
v3.assign(v1.begin(), v1.end());
printVector(v3);
//n个elem
vector<int>v4;
v4.assign(10, 120);
printVector(v4);
}
int main()
{
test501();
system("pause");
return 0;
}
vector容器容量
#include<algorithm> //标准算法头文件
#include <iostream>
#include<vector>
#include<string>
using namespace std;
/*
函数原型:
empty(); 判断容器是否为空
capacity() 容器的容量
size() 返回容器中元素的个数
resize(int num) 重新指定容器长度未num,若容器变长则以默认值填充新位置
如果容器变短,则末尾超出容器长度的元素被删除
resize(int num, elem) 重新指定容器的长度为num,若容器变长,则以elem值填充新位置
如果容器变短,则末尾超出容器长度的元素被删除
*/
void printVector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector容器的存量和大小操作
void test601()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
if (v1.empty()) //为真 代表容器为空
{
cout << "v1为空" << endl;
}
else
{
cout << "v1不为空" << endl;
cout << "v1的容量为:" << v1.capacity() << endl;
cout << "v1的大小为:" << v1.size() << endl;
}
//重新指定大小
v1.resize(15);
printVector(v1);
v1.resize(18, 5);
printVector(v1);
}
int main()
{
test601();
system("pause");
return 0;
}
删除和插入元素
#include<algorithm> //标准算法头文件
#include <iostream>
#include<vector>
#include<string>
using namespace std;
/*
函数原型:
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 printVector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test701()
{
vector<int>v1;
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
v1.push_back(60);
//遍历
printVector(v1);
//尾删
v1.pop_back();
printVector(v1);
//插入
v1.insert(v1.begin(), 100);
printVector(v1);
v1.insert(v1.begin(), 2, 1000);
printVector(v1);
//删除 参数也是迭代器
v1.erase(v1.begin());
printVector(v1);
v1.erase(v1.begin(), v1.end());
v1.clear();
printVector(v1);
}
int main()
{
test701();
system("pause");
return 0;
}
vector容器数据存取
#include<algorithm> //标准算法头文件
#include <iostream>
#include<vector>
#include<string>
using namespace std;
/*
函数原型:
at(int idx) 返回所有idx所指的数据
operator[] 返回所有idx所指的数据
front() 返回容器中第一个数据元素
back() 返回容器中最后一个数据元素
*/
void test801()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
//利用[]方式访问数组
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
//利用at方式访问数组
for (int i = 0; i < v1.size(); i++)
{
cout << v1.at(i) << " ";
}
//获取第一个元素
cout << "第一个元素为:" << v1.front() << endl;
//获取最后一个元素
cout << "最后一个元素为:" << v1.back() << endl;
}
int main()
{
test801();
system("pause");
return 0;
}
容器互换
通过容器呼唤可以实现内存收缩
#include<algorithm> //标准算法头文件
#include <iostream>
#include<vector>
#include<string>
using namespace std;
//容器互换
//swap(vec) 将vec与本身的元素互换
//void printVector(vector<int>& v)
//{
// for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
// {
// cout << *it << " ";
// }
// cout << endl;
//}
void test901()
{
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);*/
v1.swap(v2);
/*printVector(v1);
printVector(v2);*/
}
//利用swap收缩内存空间
void test902()
{
vector<int>v1;
for (int i = 10000; i > 0; i--)
{
v1.push_back(i);
}
cout << "v1的容量为:" << v1.capacity() << endl;
cout << "v1的大小为:" << v1.size() << endl;
v1.resize(3);
cout << "v1的容量为:" << v1.capacity() << endl;
cout << "v1的大小为:" << v1.size() << endl;
vector<int>(v1).swap(v1); //vector<int>(v1) 匿名对象
cout << "v1的容量为:" << v1.capacity() << endl;
cout << "v1的大小为:" << v1.size() << endl;
}
int main()
{
test902();
system("pause");
return 0;
}
预留空间
#include<algorithm> //标准算法头文件
#include <iostream>
#include<vector>
#include<string>
using namespace std;
//vector预留空间
//减少vector在动态扩展容量时的扩展次数
//reserve(int len); 容器预留len个元素长度,预留位置不初始化,元素不可访问
void test1001()
{
vector<int>v;
//利用reserve预留空间
v.reserve(100000);
int num = 0;
int* p = NULL;
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
if (p != &v[0])
{
p = &v[0];
num++;
}
}
cout << "num:" << num << endl;
}
int main()
{
test1001();
system("pause");
return 0;
}