C++数据结构

2 STL初识

2.1 STL诞生

在这里插入图片描述

2.2 STL基本概念

在这里插入图片描述

2.3 STL六大组件

分别为:容器、算法、迭代器、仿函数、适配器、空间配置器

在这里插入图片描述

2.4 STL种容器、算法、迭代器

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.5 容器算法迭代器初始

STL最常用的容器为Vector

2.5.1 vector存放内置数据模型

容器: vector
算法: for_each
迭代器: vector::iterator

#include<iostream>
#include<vector>
#include<algorithm> //标准算法头文件
using namespace std;
//vector容器存放内置数据类型
void myPrint(int val) {
	cout << val << endl;
}

//vector 容器存放内置数据模型
void test01() {
	//创建一个vector容器,数组
	vector<int> v;
	//向容器中插入数据
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	通过迭代器访问容器中的数据
	//vector<int>::iterator itBegan = v.begin();//起始迭代器,指向容器中第一个元素
	//vector<int>::iterator itEnd = v.end(); //结束迭代器 指向容器中最后一个元素的下一个位置
	第一种遍历方式
	//while (itBegan != itEnd) {
	//	cout << *itBegan << endl;
	//	itBegan++;

	//第二种遍历方式
	/*for(vector<int>::iterator it=v.begin();it!=v.end();it++){
		cout << *it << endl;
	}*/

	//第三种遍历方式 利用STL提供遍历算法
	for_each(v.begin(), v.end(), myPrint);
}
		

int main() {
	test01();
	system("pause");
	return 0;
}

2.5.2 Vector存放自定义数据类型

#include<iostream>
#include<vector>
#include<algorithm> //标准算法头文件
using namespace std;
//vercor容器存放自定义数据类型
class Person {
public:
	Person(string name, int age) {
		this->m_Nmae = name;
		this->m_Age = age;
	}
	string m_Nmae;
	int m_Age;
};
void test01() {
	vector<Person> v;
	Person p1("aaa", 10);
	Person p2("aa1a", 20);
	Person p3("a1aa", 30);
	Person p4("aa5a", 40);
	//向容器中添加数据
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	//遍历容器中的数据
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
		//cout << (*it).m_Age << (*it).m_Nmae << endl;
		cout << it->m_Age << it->m_Nmae << endl;


	}

}
//存放自定义数据类型 指针
void test02(){
	vector<Person*> v;
	Person p1("aaa", 10);
	Person p2("aa1a", 20);
	Person p3("a1aa", 30);
	Person p4("aa5a", 40);
	//向容器中添加数据
	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);
	v.push_back(&p4);
	//遍历容器中的数据
	for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++) {
		//cout << (*it).m_Age << (*it).m_Nmae << endl;
		cout << (*it)->m_Age << (*it)->m_Nmae << endl;


	}
}

int main() {
	test02();
	system("pause");
	return 0;
}

2.5.3 Vector容器嵌套容器

#include<iostream>
#include<vector>
#include<algorithm> //标准算法头文件
using namespace std;
//容器嵌套容器
class Person {
public:
	Person(string name, int age) {
		this->m_Nmae = name;
		this->m_Age = age;
	}
	string m_Nmae;
	int m_Age;
};
void test01() {
	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;
	}

}



int main() {
	test01();
	system("pause");
	return 0;
}

3 STL常用容器

3.1 string 容器

3.1.1string基本概念

  • string是C++风格的字符串,而string本质上是一个类
    在这里插入图片描述

3.1.2 string构造函数

在这里插入图片描述

#include<iostream>
#include<vector>
#include<algorithm> //标准算法头文件
using namespace std;
//string的构造函数
void test01() {
	string s1;//默认构造
	const char *str = "hello world";
	string  s2(str);
	cout << s2 << endl;
	string s3(s2);
	cout << s3 << endl;
	string s4(10, 'a');
	cout << s4 << endl;
}


int main() {
	test01();
	system("pause");
	return 0;
}

3.1.3 string赋值操作

功能描述:给string字符串进行赋值
在这里插入图片描述

#include<iostream>
#include<vector>
#include<algorithm> //标准算法头文件
using namespace std;
//string赋值操作
void test01() {
	string str1;
	str1 = "hello world";
	cout << str1 << endl;
	string str2;
	str2 = str1;
	cout << str2 << endl;
	string str3;
	str3 = 'a';
	cout << str3 << endl;

	string str4;
	str4.assign("heelo c++");
	cout << str4 << endl;

	string str5;
	str5.assign("hello c++", 5);
	cout << str5 <<endl;

	string str6;
	str6.assign(str5);
	cout << str6 << endl;

	string str7;
	str7.assign(10, 'w');
	cout << str7 << endl;
};


int main() {
	test01();
	system("pause");
	return 0;
}

3.1.4 string字符串拼接

实现在字符串末尾拼接字符串
在这里插入图片描述

3.1.5 string查找和替换

查找:查找指定字符串是否存在
替换:在指定位置替换字符串
在这里插入图片描述

#include<iostream>
#include<vector>
#include<algorithm> //标准算法头文件
using namespace std;
//字符串查找和替换
//1.查找
void test01() {
	string str1 = "abcdefg";
	int pos=str1.find("de");
	//pos=-1 查不到else 查到
	cout << pos << endl;
	//rfind 和find区别
	//rfind从右往左,find 从左往右
	int pos1 = str1.rfind("de");
	cout << pos1 << endl;
};
//2.替换
void test02() {
	string str1 = "abcdefg";
	str1.replace(1, 3, "1111");
	cout << str1 << endl;
}
int main() {
	test02();
	system("pause");
	return 0;
}

3.1.6 string字符串比较

字符串之间的比较
比较方式:

  • 字符串比较是按字符的ASCII码进行对比
    在这里插入图片描述
#include<iostream>
#include<vector>
#include<algorithm> //标准算法头文件
using namespace std;
//字符串比较

void test01() {
	string str1 = "aello";
	string str2 = "hello";
	if (str1.compare(str2) == 0) {
		cout << "==" << endl;
	}
	else if (str1.compare(str2) > 0) {
		cout << ">" << endl;
	}
	else {
		cout << "<" << endl;
	}
};
int main() {
	test01();
	system("pause");
	return 0;
}

3.1.7 string字符存取

在这里插入图片描述

#include<iostream>
#include<vector>
#include<algorithm> //标准算法头文件
using namespace std;
//string 字符存取

void test01() {
	string str1 = "aello";
	//cout << str1 << endl;
	//1.通过[]访问单个字符
	for (int i = 0; i < str1.size(); i++) {
		cout << str1[i] << endl;
	}
	//2.通过at方式访问单个字符
	for (int i = 0; i < str1.size(); i++) {
		cout << str1.at(i) << endl;
	}
	//修改单个字符
	str1[0] = 'x';
	cout << str1 << endl;
	str1.at(1) = 'x';
	cout << str1 << endl;
};


int main() {
	test01();
	system("pause");
	return 0;
}

3.1.8 string插入和删除

在这里插入图片描述

#include<iostream>
#include<vector>
#include<algorithm> //标准算法头文件
using namespace std;
//string 字符插入和删除

void test01() {
	string str = "hello";
	//插入
	str.insert(1, "111");
	cout << str << endl;
	//删除
	str.erase(1, 3);
	cout << str << endl;
};

int main() {
	test01();
	system("pause");
	return 0;
}

3.1.9 string子串

在这里插入图片描述

#include<iostream>
#include<vector>
#include<algorithm> //标准算法头文件
using namespace std;
//string 子串

void test01() {
	string str = "abcdef";
	string subStr = str.substr(1, 3);
	cout << subStr << endl;
};
//实用操作
void test02() {
	string email = "zhangsan@sina.com";
	//从邮件地址中 获取用户名信息
	int pos = email.find("@");
	string usname = email.substr(0, pos);
	cout << usname << endl;
}

int main() {
	test02();
	system("pause");
	return 0;
}

3.2 vector容器

3.2.1 vector基本概念

功能:

  • vector数据结构和数组非常相似,也称为单端数组

vector与普通数据区别:

  • 不同之处在于数组是静态空间,而vector可以动态扩展

动态扩展:

  • 并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间
  • vector容器的迭代器是支持随机访问的迭代器

3.2.2 vector 构造函数

创建vector容器
在这里插入图片描述

#include<iostream>
#include<vector>
using namespace std;
//vector容器构造
void printVector(vector<int> v1) {
	for (vector<int>::iterator v = v1.begin(); v != v1.end(); v++) {
		cout << (*v);
	}
}
void test01() {
	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方式构造
	vector<int> v3(10, 100);
	printVector(v3);
	//拷贝构造
	vector<int>v4(v3);
	printVector(v4);
};
int main() {
	test01();
	system("pause");
	return 0;
}

3.2.3 vector赋值操作

给vector容器进行赋值
在这里插入图片描述

#include<iostream>
#include<vector>
using namespace std;
//vector赋值
void printVector(vector<int> v1) {
	for (vector<int>::iterator v = v1.begin(); v != v1.end(); v++) {
		cout << (*v)<<" ";
	}
}
void test01() {
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}
	printVector(v1);
	//赋值 operator=
	vector<int> v2 = v1;
	printVector(v2);
	//assign
	vector<int> v3;
	v3.assign(v1.begin(), v1.end());
	printVector(v3);
	// n个element方式赋值
	vector<int> v4;
	v4.assign(10, 100);
	printVector(v4);
};



int main() {
	test01();
	system("pause");
	return 0;
}

3.2.4 vector容量和大小

对vector容器的容量和大小操作

在这里插入图片描述

#include<iostream>
#include<vector>
using namespace std;
//vector容量和大小
void printVector(vector<int> v1) {
	for (vector<int>::iterator v = v1.begin(); v != v1.end(); v++) {
		cout << (*v)<<" ";
	}
	cout << endl;
}
void test01() {
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}
	printVector(v1);

	if (v1.empty())//为真,为空
	{
		cout << "null" << endl;
	}
	else {
		cout << "not null" << endl;
		cout << v1.capacity() << endl;
		cout << v1.size() << endl;

	}
	//重新指定大小
	v1.resize(15);//利用重载版本,可以指定默认填充值,参数2
	printVector(v1); //如果重新指定比原来常,用0填充新位置

	v1.resize(5);
	printVector(v1); //如果重新指定的比原来短了,超出部分会删除掉
};



int main() {
	test01();
	system("pause");
	return 0;
}

3.2.5 vector插入和删除

对vector容器进行插入和删除操作

在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include<vector>
using namespace std;
//vector插入和删除操作
void printVector(vector<int> v1) {
	for (vector<int>::iterator v = v1.begin(); v != v1.end(); v++) {
		cout << (*v)<<" ";
	}
	cout << endl;
}
void test01() {
	vector<int> v1;
	//尾插
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	//遍历
	printVector(v1);

	//尾删
	v1.pop_back();
	printVector(v1);
	//插入 第一个参数是迭代器
	v1.insert(v1.begin(), 100);
	printVector(v1);
	v1.insert(v1.begin(), 2,101);
	printVector(v1);

	//删除 参数也是迭代器
	v1.erase(v1.begin());
	printVector(v1);

	//清空
	//v1.erase(v1.begin(),v1.end());
	v1.clear();
	printVector(v1);




};



int main() {
	test01();
	system("pause");
	return 0;
}

3.2.6 vector数据存取

对vector中数据进行存取
在这里插入图片描述

#include<iostream>
#include<vector>
using namespace std;
//vector数据存取
void printVector(vector<int> v1) {
	for (vector<int>::iterator v = v1.begin(); v != v1.end(); v++) {
		cout << (*v)<<" ";
	}
	cout << endl;
}
void test01() {
	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];
	}
	cout << endl;
	//利用AT方式访问元素
	for (int i = 0; i < v1.size(); i++) {
		cout << v1.at(i);
	}
	cout << endl;

	//获取第一个元素
	cout << v1.front() << endl;
	//获取最后一个元素
	cout << v1.back() << endl;




};

int main() {
	test01();
	system("pause");
	return 0;
}

3.2.7 vector互换容器

实现两个容器内元素进行互换

函数原型:

  • swap(vec);//将vec与本身的元素互换
#include<iostream>
#include<vector>
using namespace std;
//vector互换容器


void printVector(vector<int> v1) {
	for (vector<int>::iterator v = v1.begin(); v != v1.end(); v++) {
		cout << (*v)<<" ";
	}
	cout << endl;
}
//1、基本使用
void test01() {
	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);
};
//2.实际用途
//利用swap可以收缩内存空间
void test02() {
	vector<int> v;
	for (int i = 0; i < 100000; i++) {
		v.push_back(i);
	}
	cout << v.capacity() << endl;
	cout << v.size() << endl;
	v.resize(3);
	cout << v.capacity() << endl;
	cout << v.size() << endl;
	//巧用swap收缩内存
	vector<int>(v).swap(v);  //vector<int>(v) 匿名对象
	cout << v.capacity() << endl;
	cout << v.size() << endl;
}



int main() {
	test02();
	system("pause");
	return 0;
}

在这里插入图片描述

3.2.8 vector预留空间

减少vector在动态扩展容量时的扩展次数
在这里插入图片描述

3.3 deque容器

3.3.1 deque容器基本概念

双端数组,可以对头端进行插入删除操作

在这里插入图片描述
在这里插入图片描述

3.3.2 deque构造函数

deque容器构造

在这里插入图片描述

#include<iostream>
#include<deque>
using namespace std;
//deque 构造函数
void printDeque (const deque<int>& d){
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		//*it = 100;//容器中数据不可以修改
		cout << *it;
	}
	cout << endl;
}
void test01() {
	deque<int> d1;
	for (int i = 0; i < 10; i++) {
		d1.push_back(i);
	}
	printDeque(d1);
	deque<int> d2(d1.begin(), d1.end());
	printDeque(d2);

	deque<int> d3(10, 100);
	printDeque(d3);
	 
	deque<int> d4(d3);
	printDeque(d4);

};

int main() {
	test01();
	system("pause");
	return 0;
}

在这里插入图片描述

3.3.3deque赋值操作

在这里插入图片描述

#include<iostream>
#include<deque>
using namespace std;
//deque 容器赋值操作
void printDeque (const deque<int>& d){
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		//*it = 100;//容器中数据不可以修改
		cout << *it;
	}
	cout << endl;
}
void test01() {
	deque<int> d1;
	for (int i = 0; i < 10; i++) {
		d1.push_back(i);
	}
	//等号赋值 operator=赋值
	deque<int> d2;
	d2 = d1;
	printDeque(d2);
	//assign赋值
	deque<int> d3;
	d3.assign(d1.begin(), d1.end());
	printDeque(d3);

	deque<int> d4;
	d4.assign(10, 100);
	printDeque(d4);

};
int main() {
	test01();
	system("pause");
	return 0;
}

在这里插入图片描述

3.3.4 deque大小操作

在这里插入图片描述

#include<iostream>
#include<deque>
using namespace std;
//deque 大小操作
void printDeque (const deque<int>& d){
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		//*it = 100;//容器中数据不可以修改
		cout << *it;
	}
	cout << endl;
}
void test01() {
	deque<int> d1;
	for (int i = 0; i < 10; i++) {
		d1.push_back(i);
	}
	printDeque(d1);
	if (d1.empty()) {
		cout << "null" << endl;
	}
	else {
		cout << "is not null" << endl;
		cout << d1.size() << endl;
		//deque没有容量概念
	}
	//重新指定大小
	d1.resize(15);
	printDeque(d1);
	

};

int main() {
	test01();
	system("pause");
	return 0;
}

在这里插入图片描述

3.3.5 deque插入和删除

在这里插入图片描述

#include<iostream>
#include<deque>
using namespace std;
//deque 插入和删除
void printDeque (const deque<int>& d){
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		//*it = 100;//容器中数据不可以修改
		cout << *it;
	}
	cout << endl;
}
void test01() {
	deque<int> d1;
	//尾插
	d1.push_back(10);
	d1.push_back(20);
	//头插
	d1.push_front(100);
	d1.push_front(200);
	printDeque(d1);
	//尾删
	d1.pop_back();
	printDeque(d1);
	//头删
	d1.pop_front();
	printDeque(d1);
};
void test02() {
	deque<int>d1;
	d1.push_back(10);
	d1.push_back(20);
	d1.push_front(100);
	d1.push_front(200);
	printDeque(d1);

	//insert 插入
	d1.insert(d1.begin(), 1000);
	printDeque(d1);
	d1.insert(d1.begin(), 2,10001);
	printDeque(d1);

	//按照区间进行插入
	deque<int>d2;
	d2.push_back(1);
	d2.push_back(2);
	d2.push_back(3);

	d1.insert(d1.begin(), d2.begin(), d2.end());
	printDeque(d1);
}
void test03() {
	deque<int> d1;
	d1.push_back(10);
	d1.push_back(20);
	d1.push_front(100);
	d1.push_front(200);

	//删除
	deque<int>::iterator it = d1.begin();
	it++;
	d1.erase(it);
	printDeque(d1);
	//按区间方式删除
	d1.erase(d1.begin(), d1.end());
	printDeque(d1);
}
int main() {
	test03();
	system("pause");
	return 0;
}

在这里插入图片描述

3.3.6 deque数据存取

在这里插入图片描述

#include<iostream>
#include<deque>
using namespace std;
//deque 容器存储
void printDeque (const deque<int>& d){
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		//*it = 100;//容器中数据不可以修改
		cout << *it;
	}
	cout << endl;
}
void test01() {
	deque<int> d1;
	//尾插
	d1.push_back(10);
	d1.push_back(20);
	d1.push_back(30);
	//头插
	d1.push_front(100);
	d1.push_front(200);
	d1.push_front(300);
	//通过[]方式访问元素
	for (int i = 0; i < d1.size(); i++) {
		cout << d1[i] << " ";
	}
	cout << endl;
	//通过at方式访问元素
	for (int i = 0; i < d1.size(); i++) {
		cout << d1.at(i) << " ";
	}
	cout << endl;
	cout << d1.front() << endl;
	cout << d1.back() << endl;
};

int main() {
	test01();
	system("pause");
	return 0;
}

在这里插入图片描述

3.3.7 deque排序

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;
//deque 容器存储
void printDeque (const deque<int>& d){
	for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
		//*it = 100;//容器中数据不可以修改
		cout << *it;
	}
	cout << endl;
}
void test01() {
	deque<int> d1;
	d1.push_back(10);
	d1.push_back(20);
	d1.push_back(30);
	d1.push_front(100);
	d1.push_front(200);
	d1.push_front(300);
	//排序 排序默认规则 从小到大 升序
	//对于支持随机访问的迭代器的容器,都可以用sort算法直接对其进行排序
	//vector容器也可以用sort排序
	sort(d1.begin(),d1.end());
	printDeque(d1);
};

int main() {
	test01();
	system("pause");
	return 0;
}

3.5 Stack容器

3.5.1 stack基本概念

在这里插入图片描述
在这里插入图片描述

3.5.2 stack常用接口

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
using namespace std;
//stack 栈容器

void test01() {
	//先进后出
	stack<int> s1;
	//入栈
	s1.push(10);
	s1.push(20);
	s1.push(30);
	s1.push(40);

	//只要栈不为空,查看栈顶,执行出栈操作
	while (!s1.empty()) {
		//查看栈顶元素
		cout << s1.top() << endl;
		//出栈
		s1.pop();
	}
	cout << s1.size() << endl;

};
int main() {
	test01();
	system("pause");
	return 0;
}

3.6 queue容器

3.6.1queue基本概念

在这里插入图片描述
在这里插入图片描述

3.6.2 queue 常用接口

在这里插入图片描述

3.7 list容器

3.7.1 list基本概念

优点:可以对任意位置进行快速插入和删除元素
缺点:容器遍历速度,没有数组快;占用空间比数组大
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.7.2 list构造函数

创建list容器
在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
using namespace std;
void printList(const list<int>& L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << (*it) ;
	}
	cout << endl;
}
//list容器构造函数

void test01() {
	//创建list容器
	list<int>L1;
	//添加数据
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	printList(L1);
	//区间方式构造
	list<int>L2(L1.begin(),L1.end());
	printList(L2);
	//拷贝构造
	list<int>L3(L2);
	printList(L3);
	//n个elem
	list<int>L4(10, 100);
	printList(L4);
};

int main() {
	test01();
	system("pause");
	return 0;
}

总结:list构造方式同其他几个STL常用容器,熟练掌握即可

3.7.3 list赋值和交换

给list容器进行赋值,以及交换list容器
在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
using namespace std;
void printList(const list<int>& L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << (*it) ;
	}
	cout << endl;
}
//list容器赋值和交换

void test01() {
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	list<int>L2;
	L2 = L1; //operator=赋值
	printList(L2);
	list<int>L3;
	L3.assign(L2.begin(), L2.end());
	printList(L3);

	list<int>L4;
	L4.assign(10, 100);
	printList(L4);
};
//交换
void test02() {
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	list<int>L2;
	L2.assign(10, 100);
	L1.swap(L2);
	printList(L1);
}

int main() {
	test02();
	system("pause");
	return 0;
}

3.7.4 list大小操作

对list容器的大小进行操作
在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
using namespace std;
void printList(const list<int>& L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << (*it) ;
	}
	cout << endl;
}
//list容器大小操作


void test01() {
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	printList(L1);
	//判断容器是否为空
	if (L1.empty() ){
		cout << "null" << endl;
	}
	else {
		cout << "is not null" << endl;
		cout << L1.size() << endl;
	}
	//重新指定大小
	L1.resize(10);
	printList(L1);
	L1.resize(2);
	printList(L1);
};


int main() {
	test01();
	system("pause");
	return 0;
}

3.7.5 list插入和删除

对list容器进行数据插入和删除
在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
using namespace std;
void printList(const list<int>& L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << (*it) ;
	}
	cout << endl;
}
//list容器插入和删除


void test01() {
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_front(100);
	L1.push_front(200);
	L1.push_front(300);
	printList(L1);
	//尾删
	L1.pop_back();
	printList(L1);
	//头删
	L1.pop_front();
	printList(L1);
	//insert插入
	list<int>::iterator it = L1.begin();
	L1.insert(++it, 1000);
	printList(L1);
	//删除
	it = L1.begin();
	L1.erase(it);
	printList(L1);

	//移除
	L1.push_back(10000);
	L1.remove(10000);
	printList(L1);
};


int main() {
	test01();
	system("pause");
	return 0;
}

3.7.6 list数据获取

对list容器中数据进行存取
在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
using namespace std;
void printList(const list<int>& L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << (*it) ;
	}
	cout << endl;
}
//list容器数据存取
void test01() {
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_front(100);
	L1.push_front(200);
	L1.push_front(300);
	//list本质链表,不是用连续线性空间存储数据,迭代器也是不支持随机访问
	cout << L1.front() << endl;
	cout << L1.back() << endl;

	
};


int main() {
	test01();
	system("pause");
	return 0;
} 

3.7.7 list反转和排序

将容器中元素反转,以及将容器中的数据进行排序
在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
using namespace std;
void printList(const list<int>& L) {
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << (*it) ;
	}
	cout << endl;
}
//list容器反转和排序
bool myCompart(int v1, int v2) {
	return v1 > v2;
}

void test01() {
	list<int>L1;
	L1.push_back(20);
	L1.push_back(10);
	L1.push_back(30);
	L1.push_front(100);
	L1.push_front(200);
	L1.push_front(300);
	L1.reverse();
	printList(L1);
	//所有不支持随机访问迭代器的容器,不可以用标准算法
	// 不支持随机访问迭代器的容器,内部会提供对应一些算法
	//sort(L1.begin(),L1.end())
	L1.sort(myCompart);
	printList(L1);
	

	
};



int main() {
	test01();
	system("pause");
	return 0;
}

3.8 set/multiset容器

3.8.1 set基本概念

所有元素都会在插入时自动被排序

  • 本质:set/multiset属于关联式容器,底层结构是用二叉树实现

  • set与multiset区别:
    (1)set不允许容器中有重复的元素
    (2)multiset允许容器中有重复的元素

3.8.2 set构造和赋值

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
using namespace std;
//set容器构造和赋值
void printSet(set<int>& s) {
	for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

}

void test01() {
	set<int> s1;
	//插入数据 只有insert方式
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);
	s1.insert(20);
	s1.insert(30);
	// set :所有元素插入时自动排序
	//set不允许插入重复值
	printSet(s1);

	//拷贝构造
	set<int> s2(s1);
	printSet(s2);
	

	
};
int main() {
	test01();
	system("pause");
	return 0;
}
3.8.3 set大小和交换

统计set容器大小以及交换set容器

在这里插入图片描述

3.8.4 set插入和删除

在这里插入图片描述

3.8.5 set查找与统计

在这里插入图片描述

3.8.6 set和multiset区别

在这里插入图片描述

3.8.7 pair对组的创建

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.8.8 set容器排序

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
using namespace std;
//set容器排序
void printSet(set<int>& s) {
	for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

}
class MyCompare {
public:
	bool operator()(int v1,int v2) const{
		return v1 > v2;
	}
};
void test01() {
	set<int> s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	s1.insert(50);
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
	//指定规则从大到小
	set<int,MyCompare> s2;
	s2.insert(10);
	s2.insert(20);
	s2.insert(30);
	s2.insert(40);
	s2.insert(50);
	for (set<int,MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

};

int main() {
	test01();
	system("pause");
	return 0;
}

对自定义数据类型

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
using namespace std;
//set容器排序 存放自定义数据类型
class Person {
public:
	Person(string name, int age) {
		this->m_name = name;
		this->m_Age = age;
	}
	string m_name;
	int m_Age;
};

class MyCompare {
public:
	bool operator()(const Person p1,const Person p2) const{
		return p1.m_Age > p2.m_Age;
	}
};
void test01() {
	//自定义数据类型都会指定排序规则
	set<Person,MyCompare> s1;
	Person p1("刘备", 21);
	Person p2("张飞", 24);
	Person p3("刘分", 22);
	Person p4("曹操", 20);
	s1.insert(p1);
	s1.insert(p2);
	s1.insert(p3);
	s1.insert(p4);
	for (set<Person,MyCompare>::iterator it = s1.begin(); it != s1.end(); it++) {
		cout << it->m_name <<" ";
	}
	cout << endl;
};

int main() {
	test01();
	system("pause");
	return 0;
}

3.9 map/multimap容器

3.9.1 map基本概念

在这里插入图片描述

3.9.2 map构造和赋值

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
//set容器 构造和赋值
void printMap(map<int,int> &m) {
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
		cout << (*it).first << it->second << " ";
	}
	cout << endl;
}


void test01() {
	map<int, int> m;

	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(4, 40));
	m.insert(pair<int, int>(3, 30));
	m.insert(pair<int, int>(2, 20));
	printMap(m);
	//拷贝构造
	map<int, int> m2(m);
	printMap(m2);
};

int main() {
	test01();
	system("pause");
	return 0;
}
3.9.3 map大小和交换

在这里插入图片描述

3.9.4 map插入和删除

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
//map 容器插入和删除
void printMap(map<int,int> &m) {
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
		cout << (*it).first << it->second << " ";
	}
	cout << endl;
}


void test01() {
	map<int, int> m;
	//插入
	//第一种
	m.insert(pair<int, int>(1, 10));
	//第二种
	m.insert(make_pair(2, 20));
	//第三种
	m.insert(map<int, int>::value_type(3, 30));
	//第四种 不建议插入 可以用key访问到value
	m[4] = 40;
	printMap(m);

	//删除
	m.erase(m.begin());
	printMap(m);
	m.erase(3); //按照key删除
	printMap(m);
	//清空
	m.erase(m.begin(), m.end());
	m.clear();
};

int main() {
	test01();
	system("pause");
	return 0;
}

3.9.5 map查找和统计

在这里插入图片描述

3.9.6 map容器排序

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
//map 容器排序
void printMap(map<int,int> &m) {
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
		cout << (*it).first << it->second << " ";
	}
	cout << endl;
}

class MyCompare {
public:
	bool operator()(int v1, int v2)const {
		//降序
		return v1 > v2;
	}
};
void test01() {
	map<int, int,MyCompare> m;
	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));
	m.insert(make_pair(4, 40));
	m.insert(make_pair(5, 50));
	for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
		cout << (*it).first << it->second << " ";
	}
	cout << endl;

};

int main() {
	test01();
	system("pause");
	return 0;
}

4 STL-函数对象

4.1 函数对象

4.1.1 函数对象概念

概念:

  • 重载函数调用操作符的类,其对象常称为函数对象
  • 函数对象实用重载的()时,行为类似函数调用,也叫仿函数

本质:
函数对象是一个类,不是一个对象

4.1.2 函数对象使用

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
//函数对象(仿函数)
class Myadd {
public:
	int operator()(int v1, int v2) {
		return v1 + v2;
	}
};


//1.函数对象再使用时,可以像普通函数那样调用,可以有参数,可以有返回值
void test01() {
	Myadd myadd;
	cout << myadd(10, 10) << endl;

};
//2.函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint {
public:
	MyPrint() {
		this->count = 0;
	}
	void operator()(string test) {
		cout << test << endl;
		this->count++;
	}
	int count;
};
void test02() {
	MyPrint myprint;
	myprint("hello world");
	myprint("hello world");
	myprint("hello world");
	myprint("hello world");
	cout << myprint.count << endl;
}
//3、函数对象可以作为参数传递
void doPrint(MyPrint& mp, string test) {
	mp(test);
}
void test03() {
	MyPrint myprint;
	doPrint(myprint, "hello");
}

int main() {
	test03();
	system("pause");
	return 0;
}

4.2 谓词

4.2.1 谓词概念

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
using namespace std;
//仿函数 返回值类型是bool数值类型,称为谓词
//一元谓词
class GreaterFive {
public:
	bool operator()(int val) {
		return val > 5;
	}
};

void test01() {
	vector<int> v;
	for (int i=0; i < 10; i++) {
		v.push_back(i);
	}
	//查找容器中有没有大于5的数字
	//GreaterFive()匿名函数对象
	vector<int>::iterator  it= find_if(v.begin(), v.end(), GreaterFive());
	cout << *it << endl;
}	

int main() {
	test01();
	system("pause");
	return 0;
}

二元谓词

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

//二元谓词

class Mycompare {
public:
	bool operator()(int val1,int val2) {
		return val1 > val2;
	}
};

void test01() {
	vector<int> v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(20);
	v.push_back(30);
	v.push_back(50);
	sort(v.begin(), v.end());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
	//使用函数对象 改变算法策略,变为排序规则从大到小
	sort(v.begin(), v.end(),Mycompare());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
	
}	

int main() {
	test01();
	system("pause");
	return 0;
}

4.3 内建函数对象

4.3.1 内建函数对象意义

在这里插入图片描述

4.3.2 算法仿函数

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//内建函数对象
//negate 一元仿函数 取反仿函数



void test01() {
   negate<int> a;
   cout << a(50) << endl;
   
}	
void test02() {
   plus<int>p;
   cout << p(10, 10) << endl;
}

int main() {
   test02();
   system("pause");
   return 0;
}

4.3.3 关系仿函数

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//内建函数对象 关系仿函数
//大于 greater
class Mycompare
{
public:
   bool operator()(int v1,int v2) {
   	return v1 > v2;
   }
};


void test01() {
   vector<int> v;
   v.push_back(10);
   v.push_back(30);
   v.push_back(40);
   v.push_back(20);
   v.push_back(50);
   for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
   	cout << *it << endl;
   }
   //降序
   //greater<int>() 内建函数对象
   sort(v.begin(), v.end(),greater<int>());
   for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
   	cout << *it << endl;
   }
}	


int main() {
   test01();
   system("pause");
   return 0;
}

4.3.4 逻辑仿函数

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//内建函数对象 逻辑仿函数
//逻辑非 logical_not

void test01() {
	vector<bool> v;
	v.push_back(true);
	v.push_back(true);
	v.push_back(false);
	v.push_back(false);
	v.push_back(false);
	for (vector<bool>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
	//利用逻辑非 将容器v搬运到容器v2中,并执行取反操作
	vector<bool> v2;
	v2.resize(v.size());
	transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
	for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

}	


int main() {
	test01();
	system("pause");
	return 0;
}

5 STL常用算法

在这里插入图片描述

5.1 常用遍历算法

在这里插入图片描述

5.1.1 for_each

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用遍历算法 for_each
//普通函数
void print01(int val) {
	cout << val << " ";
}
//仿函数
class print02 {
public:
	void operator()(int val) {
		cout << val << " ";
	}
};
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), print01);
	cout << endl;
	for_each(v.begin(), v.end(), print02());
	cout << endl;
}	


int main() {
	test01();
	system("pause");
	return 0;
}

在这里插入图片描述

5.1.2 transform

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用遍历算法 transform
class dransform {
public:
	int operator()(int v){
		return v;
	}
};
class Myprint {
public:
	void operator()(int val) {
		cout << val << " ";
	}
};

void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
	}
	vector<int> target; //目标容器 需要提前开辟空间
	target.resize(v.size());
	transform(v.begin(), v.end(), target.begin(), dransform());
	for_each(target.begin(), target.end(), Myprint());
	
}	


int main() {
	test01();
	system("pause");
	return 0;
}

5.2 常用查找算法

在这里插入图片描述

5.2.1 find

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用查找算法
//find

//查找 内置数据类型

void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
	}
	//查找 是否 有5
	vector<int>::iterator it=find(v.begin(), v.end(), 5);
	if (it == v.end()) {
		cout << "没有找到"<<endl;
	}
	else {
		cout << "找到:" << *it << endl;
	}
	
}	
class Person {
public:
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
	//重载== 底层find知道如何对比Person数据类型
	bool operator==(const Person& p) {
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
			return true;
		}
		else {
			return false;
		}
	}
	string m_Name;
	int m_Age;

};
//查找 自定义数据类型
void test02() {
	vector<Person>v;
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	vector<Person>::iterator it=find(v.begin(), v.end(), p2);
	if (it == v.end()) {
		cout << "no" << endl;
	}
	else {
		cout << "yes" << it->m_Name << endl;
	}
}

int main() {
	test02();
	system("pause");
	return 0;
}

5.2.2 find_if

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用查找算法
//find_if

//查找 内置数据类型
class Grater {
public:
	bool operator()(int val) {
		return val > 5;
	}
};
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
	}
	//查找 是否 有5
	vector<int>::iterator it=find_if(v.begin(), v.end(), Grater());
	if (it == v.end()) {
		cout << "没有找到"<<endl;
	}
	else {
		cout << "找到:" << *it << endl;
	}
	
}	
class Person {
public:
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;

};
class Greater20 {
public:
	bool operator()(Person& p) {
		return p.m_Age > 20;
	}
};
//查找 自定义数据类型
void test02() {
	vector<Person>v;
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	vector<Person>::iterator it=find_if(v.begin(), v.end(), Greater20());
	if (it == v.end()) {
		cout << "no" << endl;
	}
	else {
		cout << "yes" << it->m_Name << endl;
	}
}

int main() {
	test02();
	system("pause");
	return 0;
}

5.2.3 adjacent_find

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用查找算法
//adjacent_find

void test01() {
	vector<int> v;
	v.push_back(0);
	v.push_back(3);
	v.push_back(2);
	v.push_back(0);
	//查找 是否 有5
	vector<int>::iterator it=adjacent_find(v.begin(), v.end());
	if (it == v.end()) {
		cout << "没有找到"<<endl;
	}
	else {
		cout << "找到:" << *it << endl;
	}
	
}	


int main() {
	test01();
	system("pause");
	return 0;
}

5.2.4 binary_search

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用查找算法
//binary_search

void test01() {
	vector<int> v;
	v.push_back(0);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	//查找 是否 有5
	bool it=binary_search(v.begin(), v.end(),2);
	if (it) {
		cout << "有找到"<<endl;
	}
	else {
		cout << "null找到:" << endl;
	}
	
}	


int main() {
	test01();
	system("pause");
	return 0;
}

5.2.5 count

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用查找算法
//count
//1.统计内置数据类型
void test01() {
	vector<int> v;
	v.push_back(0);
	v.push_back(2);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	//查找 是否 有5
	int it=count(v.begin(), v.end(),2);
	cout << it << endl;
	
}	
//2.统计自定义数据类型
class Person {
public:
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}
	bool operator==(const Person &p1) {
		if (this->m_Age == p1.m_Age) {
			return true;
		}
		else {
			return false;
		}
	}
	string m_Name;
	int m_Age;
};
void test02() {
	vector<Person> v;
	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("赵云", 30);
	Person p5("曹操", 40);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	int num = count(v.begin(), v.end(), p1);
	cout << num << endl;
}

int main() {
	test02();
	system("pause");
	return 0;
}

5.2.6 count_if

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用查找算法
//count_if
//1.统计内置数据类型
class Greater {
public:
	bool operator()(int val) {
		return val >= 2;
	}
};
void test01() {
	vector<int> v;
	v.push_back(0);
	v.push_back(2);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	//查找 是否 有5
	int it=count_if(v.begin(), v.end(),Greater());
	cout << it << endl;
	
}	
//2.统计自定义数据类型


class Person {
public:
	Person(string name, int age) {
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;
};
class FFFF {
public:
	bool operator()(const Person& p1) {
		return p1.m_Age > 35;
	}
};
void test02() {
	vector<Person> v;
	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("赵云", 30);
	Person p5("曹操", 40);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	int num = count_if(v.begin(), v.end(), FFFF());
	cout << num << endl;
}

int main() {
	test02();
	system("pause");
	return 0;
}

5.3 常用排序算法

在这里插入图片描述

5.3.1 sort

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用排序算法
//sort

void myPrint(int val) {
	cout << val << endl;
}
void test01() {
	vector<int> v;
	v.push_back(0);
	v.push_back(3);
	v.push_back(5);
	v.push_back(3);
	v.push_back(4);
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint);
	//改变为降序
	sort(v.begin(), v.end(), greater<int>());
	for_each(v.begin(), v.end(), myPrint);
	
}	


int main() {
	test01();
	system("pause");
	return 0;
}

5.3.2 random_shuffle

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用排序算法
//random_shuffle

void myPrint(int val) {
	cout << val << endl;
}
void test01() {
	vector<int> v;
	v.push_back(0);
	v.push_back(3);
	v.push_back(5);
	v.push_back(3);
	v.push_back(4);
	random_shuffle(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint);
	
	
}	


int main() {
	srand((unsigned int)time(NULL)); //随机种子
	test01();
	system("pause");
	return 0;
}

5.3.3 merge

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用排序算法
//merge

void myPrint(int val) {
	cout << val << endl;
}
void test01() {
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
		v2.push_back(i+1);

	}
	vector<int> target(v1.size()+v2.size());
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), target.begin());

	for_each(target.begin(), target.end(), myPrint);
	
}	


int main() {
	test01();
	system("pause");
	return 0;
}

5.3.4 reverse

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用排序算法
//reverse

void myPrint(int val) {
	cout << val << " ";
}
void test01() {
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}
	for_each(v1.begin(), v1.end(), myPrint);
	reverse(v1.begin(), v1.end());
	for_each(v1.begin(), v1.end(), myPrint);
	
}	
int main() {
	test01();
	system("pause");
	return 0;
}

5.4 常用拷贝和替换算法

在这里插入图片描述

5.4.1 copy

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用拷贝和替换算法
//copy

void myPrint(int val) {
	cout << val << " ";
}
void test01() {
	vector<int> v1;
	vector<int> v2; 

	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}
	v2.resize(v1.size());
	copy(v1.begin(), v1.end(), v2.begin());
	for_each(v2.begin(), v2.end(), myPrint);
	
}	


int main() {
	test01();
	system("pause");
	return 0;
}

5.4.2 replace

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用拷贝和替换算法
//replace

void myPrint(int val) {
	cout << val << " ";
}
void test01() {
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;
	//替换
	replace(v1.begin(), v1.end(), 9, 100);
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;

	
}	


int main() {
	test01();
	system("pause");
	return 0;
}

5.4.3 replace_if

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用拷贝和替换算法
//replace_if
class Greator{
public:
	bool operator()(int val) {
		return val > 6;
	}
};
void myPrint(int val) {
	cout << val << " ";
}
void test01() {
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;
	//替换
	replace_if(v1.begin(), v1.end(), Greator(), 100);
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;

	
}	


int main() {
	test01();
	system("pause");
	return 0;
}

5.4.4 swap

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
using namespace std;

//常用拷贝和替换算法
//swap
void myPrint(int val) {
	cout << val << " ";
}
void test01() {
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
		v2.push_back(i + 100);
	}
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;
	//替换
	swap(v1, v2);
	for_each(v1.begin(), v1.end(), myPrint);
	cout << endl;

	
}	


int main() {
	test01();
	system("pause");
	return 0;
}

5.5 常用算术生成算法

在这里插入图片描述

5.5.1 accumulate

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
#include<numeric>
using namespace std;


//accumulate
void myPrint(int val) {
	cout << val << " ";
}
void test01() {
	vector<int> v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}
	//参数3为起始值
	int total=accumulate(v1.begin(), v1.end(), 0);
	cout<<total<<endl;
	

	
}	
int main() {
	test01();
	system("pause");
	return 0;
}

5.5.2 fill

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
#include<numeric>
using namespace std;


//fill
void myPrint(int val) {
	cout << val << " ";
}
void test01() {
	vector<int> v1;
	v1.resize(10);
	fill(v1.begin(), v1.end(), 100);
	for_each(v1.begin(), v1.end(), myPrint);
	
	

	
}	


int main() {
	test01();
	system("pause");
	return 0;
}

5.6 常用集合算法

在这里插入图片描述

5.6.1 set_intersection

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
#include<numeric>
using namespace std;


//set_intersection
void myPrint(int val) {
	cout << val << " ";
}
void test01() {
	vector<int> v1;
	vector<int> v2;
	vector<int> target;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
		v2.push_back(i + 5);
	}
	target.resize(min(v1.size(), v2.size()));
	set_intersection(v1.begin(),v1.end(), v2.begin(),v2.end() ,target.begin());
	for_each(target.begin(), target.end(), myPrint);
	

	
}	


int main() {
	test01();
	system("pause");
	return 0;
}

5.6.2 set_union

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
#include<numeric>
using namespace std;


//set_union
void myPrint(int val) {
	cout << val << " ";
}
void test01() {
	vector<int> v1;
	vector<int> v2;
	vector<int> target;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
		v2.push_back(i + 5);
	}
	target.resize(v1.size()+ v2.size());
	vector<int>::iterator it=set_union(v1.begin(),v1.end(), v2.begin(),v2.end() ,target.begin());
	for_each(target.begin(), it, myPrint);
	

	
}	


int main() {
	test01();
	system("pause");
	return 0;
}

5.6.3 set_difference

在这里插入图片描述

#include<iostream>
#include<deque>
#include<algorithm>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件
#include<numeric>
using namespace std;


//set_difference
void myPrint(int val) {
	cout << val << " ";
}
void test01() {
	vector<int> v1;
	vector<int> v2;
	vector<int> target;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
		v2.push_back(i + 5);
	}
	target.resize(v1.size()+ v2.size());
	vector<int>::iterator it=set_difference(v1.begin(),v1.end(), v2.begin(),v2.end() ,target.begin());
	for_each(target.begin(), it, myPrint);
	

	
}	


int main() {
	test01();
	system("pause");
	return 0;
}
  • 2
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值