#include<vector>
#include<string>
#include<iostream>
#include<algorithm>//标准算法头文件
using namespace std;
void myPrint(int val) {
cout << val << endl;
}
//test01: 存放内置数据类新
//test02: 存放自定义数据类型
//test03: 存放自定义数据类型的指针
//test04: 容器嵌套容器:内置数据类型
//test05: 容器嵌套容器:自定义数据类型
//vector 存放内置数据类型
void test01() {
vector<int>v;
//插入数据
v.push_back(1);
v.push_back(2);
v.push_back(3);
#if 0
//迭代器访问数据
vector<int>::iterator itBegin = v.begin();//起始迭代器: 指向容器中第一个元素的位置
vector<int>::iterator itEnd = v.end();//结束迭代器: 指向容器中最后一个元素的下一个位置
//遍历方式
//idea 1: while loop
//缺点:需要定义起始迭代器和结束迭代器
while (itBegin != itEnd) {
//迭代器当作指针使用
cout << *itBegin << endl;
itBegin++;
}
#endif
#if 0
//idea 2: for loop
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << endl;
}
#endif
//idea 3: STL 算法
for_each(v.begin(), v.end(), myPrint);//内置定义了解引用
}
//vector 存放自定义数据类型
class Person {
string name;
int age;
public:
Person(string name, int age) {
this->name = name;
this->age = age;
}
string getName() { return this->name; }
int getAge() { return this->age; }
};
#if 0
void test02() {
vector<Person>v;
Person p1("刘备", 20);
Person p2("关羽", 23);
Person p3("张飞", 24);
//向容器中添加数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
//遍历容器中的数据
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
cout << "name: " << (*it).getName() <<" " << "age: " << (*it).getAge() << endl;
//这里可以解引用 点出来, 也可以通过指针的箭头
}
}
#endif
#if 0
//vector 存放自定义数据类型的指针
void test03() {
vector<Person*>v;
Person p1("刘备", 20);
Person p2("关羽", 23);
Person p3("张飞", 24);
//向容器中添加数据(常用的尾插法)
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
//遍历容器
for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++) {
cout << "name: " << (*it)->getName() << " " << "age: " << (*it)->getAge() << endl;
}
}
#endif
#if 0
//容器嵌套容器:内置数据类型
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++) {
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)----容器
for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) {
cout << *vit << " ";
}
cout << endl;
}
}
#endif
#if 0
//容器嵌套容器:自定义类型
void test05() {
vector<vector<Person*>>v;
//创建小容器
vector<Person*>v1;
vector<Person*>v2;
//向小容器中添加数据
Person p1("刘备", 20);
Person p2("关羽", 23);
Person p3("张飞", 24);
Person p4("孔明", 18);
v1.push_back(&p1);
v1.push_back(&p2);
v2.push_back(&p3);
v2.push_back(&p4);
//向大容器中添加数据
v.push_back(v1);
v.push_back(v2);
//通过大容器遍历数据
for (vector<vector<Person*>>::iterator it = v.begin(); it != v.end(); it++) {
for (vector<Person*>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) {
cout << "name: " << (*vit)->getName() << " " << "age: " << (*vit)->getAge() << " ";
}
cout << endl;
}
}
#endif
//-----------------------------------------------------------------------------------------------------------------
// VECTOR CONSTRUCTION
//vector数组是单端数组
//动态扩展:将原数据拷贝到新空间,释放原空间
//vector容器的迭代器是支持随机访问的迭代器
void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << (*it) << " ";
}
cout << endl;
}
//ONE: vector的构造函数
#if 0
void test06() {
//1: 默认构造:无参构造
vector<int>v1;
for (int i = 0; i < 4; i++) {
v1.push_back(i);
}
cout << "默认构造:" << endl;
printVector(v1);
//2: 通过区间构造
vector<int>v2(v1.begin(), v1.end());
cout << "区间构造:" << endl;
printVector(v2);
//3: n个element方式构造
vector<int>v3(10, 100);
cout << "n个element:" << endl;
printVector(v3);
//4: 拷贝构造 (常用)
vector<int>v4(v3);
cout << "拷贝构造:" << endl;
printVector(v4);
}
#endif
#if 0
//vector的赋值操作
void test07() {
//首先构造vector容器
vector<int>v1;
for (int i = 0; i < 4; i++) {
v1.push_back(i);
}
cout << "v1: " << endl;
printVector(v1);
//赋值
//idea1:operator= (需要重载等号运算符)
vector<int>v2;
v2 = v1;
cout << "v2: " << endl;
cout << "overload operator=: " << endl;
printVector(v2);
//idea2: 利用assign进行区间赋值 [v1.begin(), v1.end())
vector<int>v3;
v3.assign(v1.begin(), v1.end());
cout << "assign domain: " << endl;
printVector(v3);
//idea3: assign n个element
vector<int>v4;
v4.assign(10, 100);
cout << "assgin n times element: " << endl;
printVector(v4);
}
#endif
#if 0
//THREE: vector 容量和大小操作
void test08() {
//首先构造vector容器
vector<int>v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
cout << "v1: " << endl;
printVector(v1);
//判断容器是否为空
if (v1.empty()) {
cout << "empty" << endl;
}
else
{
cout << "not empty" << endl;
cout << "capacity: " << v1.capacity() << endl;
cout << "size: " << v1.size() << endl;
}
//重新指定大小, 默认用0填充新的位置
v1.resize(15);//利用重载的版本可以指定默认填充数值,参数2
cout << "v1 print after resize: " << endl;
printVector(v1);
cout << "v1 print with shorter size: " << endl;
v1.resize(5);//超出的部分会被删掉
printVector(v1);
}
#endif
#if 0
//FOUR: vector的插入和删除
void test09() {
//首先构造vector容器
vector<int>v1;
//尾插法
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
cout << "尾插法: " << endl;
printVector(v1);
cout << "尾删法:" << endl;
v1.pop_back();
printVector(v1);
//向迭代器位置插入
v1.insert(v1.begin(), 100);
v1.insert(v1.end(), 200);
cout << "迭代器位置插入:" << endl;
printVector(v1);
//迭代器位置插入n个数据
v1.insert(v1.begin(), 2, 1000);
v1.insert(v1.end(), 2, 2000);
cout << "迭代器位置插入n个数据:" << endl;
printVector(v1);
//删除 参数也是迭代器 删除迭代器位置的数据
cout << "迭代器位置删除数据:" << endl;
v1.erase(v1.begin());
printVector(v1);
//清空
v1.clear();//等同于 v1.erase(v1.begin(),v1.end());
}
#endif
#if 0
//FIVE: 数据存取
void test10() {
//首先构造vector容器
vector<int>v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
cout << "v1: " << endl;
//利用[]的方式访问数组中的元素
cout << "利用[]的方式访问数组中的元素" << endl;
for (int i = 0; i < v1.size(); i++) {
cout << v1[i] << " ";
}
cout << endl;
//利用at方式访问元素
cout << "利用at方式访问元素" << endl;
for (int i = 0; i < v1.size(); i++) {
cout << v1.at(i) << " ";
}
cout << endl;
//获取第一个元素
cout << "第一个元素:" << v1.front() << endl;
//获取最后一个元素
cout << "最后一个元素:" << v1.back() << endl;
}
#endif
#if 0
void test11() {
//SIX: vector 互换容器:实现两个容器内元素互换
//1.基本使用
//首先构造vector容器
cout << "交换前:" << endl;
vector<int>v1;
for (int i = 0; i < 10; i++) {
v1.push_back(i);
}
cout << "v1: " << endl;
printVector(v1);
vector<int>v2;
for (int i = 9; i >= 0; i--) {
v2.push_back(i);
}
cout << "v2: " << endl;
printVector(v2);
cout << "交换后:" << endl;
v1.swap(v2);
cout << "v1:" << endl;
printVector(v1);
cout << "v2: " << endl;
printVector(v2);
}
//2.实际用途
//巧用swap可以收缩内存空间
void test12() {
vector<int>v;
for (int i = 0; i < 100000; i++) {
v.push_back(i);
}
cout << "v的容量:" << v.capacity() << endl;
cout << "v的大小: " << v.size() << endl;
v.resize(3);//重新指定大小
cout << "After resize: " << endl;
cout << "v的容量:" << v.capacity() << endl;
cout << "v的大小: " << v.size() << endl;
//利用swap收缩内存
vector<int>(v).swap(v);//看不懂这一步
cout << "After swap funciton: " << endl;
cout << "v的容量:" << v.capacity() << endl;
cout << "v的大小: " << v.size() << endl;
}
#endif
#if 1
//SEVEN: vector预留空间
void test13() {
//减少vector在动态扩展时的扩展次数: vector在存储数据时空间不够的话会自动扩展空间
vector<int>v;
int num = 0;//统计动态开辟空间次数
int* p = nullptr;
cout << "提前预留空间:" << endl;
v.reserve(100000);
for (int i = 0; i < 100000; i++) {
v.push_back(i);
//每次重新开辟内存的首地址不同
if (p != &v[0]) {
p = &v[0];
num++;
}
}
cout << "num: " << num << endl;
}
#endif
int main() {
test13();
system("pause");
return 0;
}