C++ 提高编程 下

set/multiset 容器

set 基本概念

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

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

set 和 multiset 区别

  • set 不属于容器中有重复的元素
  • multiset 允许容器中重复的元素

set 构造和赋值

功能描述:创建 set 容器以及赋值

构造:

  • set<T> st; //默认构造函数
  • set(const set &t); //拷贝构造函数

赋值:

  • set& operator=(const set &st);//重载等号操作符

示例

#include<iostream>
#include <set>
using namespace std;

void printSet(set<int>& s) {
    
    
	for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
}

void test01() {
    
    
	set<int> s;

	s.insert(10);
	s.insert(40);
	s.insert(20);
	s.insert(30);

	printSet(s);

	set<int> s1(s);
	printSet(s1);

	set<int> s2 = s1;
	printSet(s2);
}

int main() {
    
    
	test01();

	return 0;
}

set 大小和交换

功能描述:统计 set 容器大小以及交换 set 容器

函数原型

  • size(); //返回容器中元素的数目
  • empty();//判断容器是否为空
  • swap(st);//交换两个集合容器

示例

#include<iostream>
#include <set>
using namespace std;

void printSet(set<int>& s) {
    
    
	for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
}

void test01() {
    
    
	set<int> s;

	s.insert(1);
	s.insert(2);
	s.insert(3);
	s.insert(4);

	if (s.empty()) {
    
    
		cout << "empty" << endl;
	}
	else {
    
    
		printSet(s);
		cout << s.size() << endl;
	}
}

void test02(){
    
    
	set<int> s;

	s.insert(1);
	s.insert(2);
	s.insert(3);
	s.insert(4);

	set<int> s1;
	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	printSet(s);
	printSet(s1);

	s.swap(s1);

	printSet(s);
	printSet(s1);
}

int main() {
    
    
	test01();
	test02();

	return 0;
}

set 插入和删除

功能描述:set 容器进行插入数据和删除数据

函数原型

  • insert(elem); //在容器中插入元素
  • clear(); //清除所有元素
  • erase(pos); //删除 pos 迭代器所指的元素,返回下一个元素的迭代器
  • erase(beg,end); //删除区间 [beg,end) 的所有元素,返回下一个元素的迭代器
  • erase(elem); //删除容器中值为elem的元素

实例

#include<iostream>
#include <set>
using namespace std;

void printSet(const set<int> &s) {
    
    
	for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) {
    
    
		cout << *it << "\t";
	}
	cout << endl;
}

void test01() {
    
    
	set<int> s1;

	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	printSet(s1);

	s1.erase(s1.begin());
	printSet(s1);

	s1.erase(30);
	printSet(s1);

	s1.clear();
}

int main() {
    
    
	test01();

	return 0;
}

set 查找和统计

功能描述:对 set 容器进行查找数据以及统计数据
函数原型

  • find(key); //查找 key 是否存在,返回该键的元素的迭代器;若不存在,返回 set.end()
  • count(key); //统计 key 的元素个数

实例

#include<iostream>
#include <set>
using namespace std;


void test01() {
    
    
	set<int> s1;

	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);

	set<int>::iterator pos = s1.find(30);

	if (pos != s1.end()) {
    
    
		cout << "found" << endl;
	}
	else {
    
    
		cout << "not found" << endl;
	}
}

void test02() {
    
    
	set<int> s1;

	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	//对于 set 而言,返回的要么是 0 要么是 1
	int num = s1.count(30);
	cout << num << endl;
}

int main() {
    
    
	test01();
	test02();

	return 0;
}

set 和 multiset 的区别

区别

  • set 不可以重复插入重复的数据,而 multiset 可以
  • set 插入数据的同时会返回插入结果,表示插入是否成功
  • multiset 不会检测数据,因此可以插入重复数据

实例

#include<iostream>
#include <set>
using namespace std;


void test01() {
    
    
	set<int> s1;
	pair<set<int>::iterator,bool> ret = s1.insert(10);

	if (ret.second) {
    
    
		cout << "succeed" << endl;
	}
	else {
    
    
		cout << "default" << endl;
	}
	ret = s1.insert(10);
	if (ret.second) {
    
    
		cout << "succeed" << endl;
	}
	else {
    
    
		cout << "default" << endl;
	}

	multiset<int> m1;
	m1.insert(10);
	m1.insert(10);

	for (multiset<int>::iterator it = m1.begin(); it != m1.end(); it++) {
    
    
		cout << *it << "\t";
	}

	cout << endl;
}

int main() {
    
    
	test01();

	return 0;
}

pair 对组创建

功能描述

  • 成对出现的数据,利用对组可以返回两个数据

两种创建方式

  • pair<type,type> p (value1,value2);
  • pair<type,type> p = make_pair(value1,value2);

实例

#include <iostream>
using namespace std;

void test01() {
    
    
	pair<string, int>p("Tom", 19);

	cout << p.first << endl;
	cout << p.second << endl;
}

void test02() {
    
    
	pair<string, int> p = make_pair("Tom", 15);
	cout << p.first << endl;
	cout << p.second << endl;
}

int main() {
    
    
	test01();
	test02();

	return 0;
}

set 容器排序

主要技术点

  • 利用仿函数,可以改变排序规则

示例1: set 存放内置数据类型

#include <iostream>
#include <set>
using namespace std;

class MyCompare {
    
    
public:
    //set内部是以const的方式调用的比较器,必须声明为const
    bool operator()(int v1,int v2) const {
    
    
        return v1 > v2;
    }
};

void test01() {
    
    
    set<int> s1;
    
    s1.insert(10);
    s1.insert(50);
    s1.insert(30);
    s1.insert(40);
    s1.insert(20);

    for (set<int>::iterator it = s1.begin();it != s1.end();it++) {
    
    
        cout << *it << " ";
    }

    cout << endl;

    //改变排序规则改为从大到小
    set<int, MyCompare> s2;

    s2.insert(10);
    s2.insert(50);
    s2.insert(30);
    s2.insert(40);
    s2.insert(20);

    for (set<int>::iterator it = s2.begin();it != s2.end();it++) {
    
    
        cout << *it << " ";
    }

    cout << endl;
}


int main()
{
    
    
    test01();
    return 0;
}

示例2:set 存放自定义数据类型

#include <iostream>
#include <set>
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;
};

class PersonComPare {
    
    
public:
	bool operator()(Person p1, Person p2) const {
    
    
		return p1.m_Age > p2.m_Age;
	}
};

void test01() {
    
    
	//自定义数据类型都会指定排序规则
	set<Person,PersonComPare> s1;

	Person p1("123", 1);
	Person p2("543", 3);
	Person p3("987", 5);
	Person p4("236", 7);

	s1.insert(p1);
	s1.insert(p2);
	s1.insert(p3);
	s1.insert(p4);

	for (set<Person> ::iterator it = s1.begin();it != s1.end();it++) {
    
    
		cout << it->m_Name << "\t" << it->m_Age << endl;
	}
}

int main() {
    
    
	test01();

	return 0;
}

map/multimap 容器

map 基本概念

简介

  • map中所有元素都是 pair
  • pair中第一个元素为 key(键值),起到索引作用,第二个元素为 value(实值)
  • 所有元素都会根据元素的键值自动排序

本质

  • map/multimap 属于 关联式容器,底层结构是用二叉树实现。

优点

  • 可以根据 key 值快速查找 value 值

map 和 multimap 区别

  • map 不允许容器中有重复 key 值元素
  • multimap 允许容器中有重复 key 值元素

map 构造和赋值

功能描述

  • 对map容器进行构造和赋值操作

函数原型
构造

  • map<T1,T2> mp; //map 默认构造函数;
  • map(const map &mp); // 拷贝构造函数

赋值

  • map& operator = (const map &mp); // 重载等号操作符

示例

#include <iostream>
#include <map>
using namespace std;

void printMap(map<int, int>& m) {
    
    
	for (map<int, int>::iterator it = m.begin();it != m.end();it++) {
    
    
		cout << (*it).first << " " << (*it).second << endl;
	}
}

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

	mp.insert(pair<int, int>(1, 10));
	mp.insert(pair<int, int>(3, 30));
	mp.insert(pair<int, int>(2, 20));
	mp.insert(pair<int, int>(4, 40));

	printMap(mp);

	map<int, int> m2(mp);

	printMap(m2);

	map<int, int>m3;
	m3 = m2;

	printMap(m3);
}

int main() {
    
    
	test01();

	return 0;
}

map 大小和交换

功能描述

  • 统计 map 容器大小以及交换 map 容器

函数原型:

  • size(); //返回容器中元素的数目
  • empty(); //判断容器是否为空
  • swap(st);//交换两个集合容器

示例

#include <iostream>
#include<map>
using namespace std;

void printMap(map<int, int>& m) {
    
    
	for (map<int, int>::iterator it = m.begin();it != m.end();it++) {
    
    
		cout << (*it).first << " ";
	}
	cout << endl;
}

void test01() {
    
    
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

	if (m.empty()) {
    
    
		cout << "empty" << endl;
	}
	else {
    
    
		cout << "not empty" << endl;
		cout << m.size() << endl;
	}
}

void test02() {
    
    
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

	printMap(m);

	map<int, int>m1;
	m1.insert(pair<int, int>(7, 70));
	m1.insert(pair<int, int>(8, 80));
	m1.insert(pair<int, int>(9, 90));

	printMap(m1);

	m.swap(m1);

	printMap(m);
	printMap(m1);
}

int main() {
    
    
	test01();
	test02();

	return 0;
}

map 插入和删除

功能描述

  • map 容器进行插入数据和删除数据

函数原型

  • instert(elem);//在容器中插入元素
  • clear();//清除所有元素
  • erase(pos);//删除 pos 迭代器所指的元素,返回下一个元素的迭代器
  • erase(beg,end);//删除区间 [beg,end) 的所有元素,返回下一个元素的迭代器
  • erase(key);//删除容器中值为 key 的元素

示例

#include <iostream>
#include<map>
using namespace std;

void printMap(map<int, int>& m) {
    
    
	for (map<int, int>::iterator it = m.begin();it != m.end();it++) {
    
    
		cout << (*it).first << 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);
	printMap(m);

	m.erase(m.begin(), m.end());
	printMap(m);

	m.clear();
}

int main() {
    
    
	test01();

	return 0;
}

map 查找和统计

功能描述

  • 对 map 容器进行查找数据和统计数据

函数原型

  • find(key);//查找 key 是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
  • found(key);//统计 key 的元素个数

示例

#include <iostream>
#include<map>
using namespace std;

void test01() {
    
    
	map<int, int>m;
	m.insert(make_pair(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(make_pair(3, 30));

	map<int, int>::iterator pos = m.find(3);

	if (pos != m.end()) {
    
    
		cout << (*pos).first << " " << pos->second << endl;
	}
	else {
    
    
		cout << "not found" << endl;
	}

	int num = m.count(3);
	cout << num << endl;
}

int main() {
    
    
	test01();

	return 0;
}

map 容器排序

主要技术点

  • 利用仿函数可以改变排序规则

示例

#include <iostream>
#include<map>
using namespace std;

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>::iterator it = m.begin();it != m.end();it++) {
    
    
		cout << (*it).first << endl;
	}
}

int main() {
    
    
	test01();

	return 0;
}

案例——员工分组

案例描述

  • 10 名员工
  • 员工信息有:姓名、工资组成;部门分为:策划、美术、研发
  • 随机给 10 名员工分配部门和工资
  • 通过 multimap 进行信息的插入 key(部门编号) value(员工)
  • 分部门显示员工信息

实现步骤

  1. 创建 10 名员工,放到 vector 中
  2. 便利 vector 容器,取出每个员工,进行随机分组
  3. 分组后,将员工部门编号作为 key,具体员工作为 value,放入到 multimap 容器中
  4. 分部门显示员工信息

示例

#include<iostream>
#include <vector>
#include <map>
using namespace std;


class Worker {
    
    
public:
	string m_Name;
	int m_Salary;
};

void createWorker(vector<Worker>& v) {
    
    
	string nameSeed = "ABCDEFGHIJ";
	for (int i = 0;i < 10;i++) {
    
    
		Worker worker;
		worker.m_Name = "员工";
		worker.m_Name += nameSeed[i];

		worker.m_Salary = rand() % 10000 + 10000;
		v.push_back(worker);
	}
}

void setGroup(vector<Worker>& v, multimap<int,Worker>& m) {
    
    
	for (vector<Worker>::iterator it = v.begin();it != v.end();it++) {
    
    
		int depId = rand() % 3;

		m.insert(make_pair(depId, *it));
	}
}

void showWorkerByGroup(multimap<int, Worker>&m) {
    
    
	cout << "A部门" << endl;

	multimap<int, Worker>::iterator pos = m.find(0);
	int count = m.count(0);
	int index = 0;
	for (;pos != m.end() && index < count;pos++,index++) {
    
    
		cout << pos->second.m_Name << " " << pos->second.m_Salary << endl;
	}

	cout << "B部门" << endl;

	pos = m.find(1);
	count = m.count(1);
	index = 0;
	for (;pos != m.end() && index < count;pos++, index++) {
    
    
		cout << pos->second.m_Name << " " << pos->second.m_Salary << endl;
	}

	cout << "C部门" << endl;

	pos = m.find(2);
	count = m.count(2);
	index = 0;
	for (;pos != m.end() && index < count;pos++, index++) {
    
    
		cout << pos->second.m_Name << " " << pos->second.m_Salary << endl;
	}
}

int main() {
    
    
	vector<Worker>vWorker;
	createWorker(vWorker);

	multimap<int, Worker>mWorker;
	setGroup(vWorker, mWorker);

	showWorkerByGroup(mWorker);
}

函数对象

函数对象

函数对象概念

概念

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

本质

  • 函数对象(仿函数)是一个,不是一个函数

函数对象使用

特点

  • 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态
  • 函数对象可以作为参数传递

示例

#include <iostream>
using namespace std;

class MyAdd {
    
    
public:
	int operator()(int v1, int v2) {
    
    
		return v1 + v2;
	}
};

void test01() {
    
    
	MyAdd myAdd;
	cout << myAdd(1, 2) << endl;
}

class MyPrint {
    
    
public:
	void operator()(string test) {
    
    
		cout << test << endl;
		this->count++;
	}
	int count = 0;
};

void test02() {
    
    
	MyPrint myPrint;
	myPrint("11111");
	myPrint("11111");
	myPrint("11111");
	myPrint("11111");
	myPrint("11111");

	cout << myPrint.count << endl;
}

void doPrint(MyPrint& mp, string test) {
    
    
	mp(test);
}

void test03() {
    
    
	MyPrint mp;
	doPrint(mp, "12345");
}

int main() {
    
    
	test01();
	test02();
	test03();

	return 0;
}

谓词

谓词概念

概念

  • 返回 bool 类型的仿函数成为谓词
  • 如果 operator() 接受一个参数,那么叫做一元谓词
  • 如果 operator() 接收两个参数,那么叫做二元谓词

一元谓词

示例

#include <iostream>
#include <vector>
using namespace std;

class GreaterFive {
    
    
public:
	bool operator()(int val) const {
    
    
		return val > 5;
	}
};

void test01() {
    
    
	vector<int> v;
	for (int i = 0;i < 10;i++) {
    
    
		v.push_back(i);
	}

	vector<int> :: iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end()) {
    
    
		cout << "cannot Find" << endl;
	}
	else {
    
    
		cout << *it << endl;
	}
}

int main() {
    
    
	test01();

	return 0;
}

二元谓词

示例

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class MyCompare {
    
    
public:
	bool operator()(int a,int b) const {
    
    
		return a > b;
	}
};

void test01() {
    
    
	vector<int> v;
	
	v.push_back(10);
	v.push_back(40);
	v.push_back(50);
	v.push_back(30);
	v.push_back(20);

	sort(v.begin(), v.end(),MyCompare());

	for (vector<int>::iterator it = v.begin();it != v.end();it++) {
    
    
		cout << *it << " ";
	}
}

int main() {
    
    
	test01();

	return 0;
}

内建函数对象

内建函数对象意义

概念

  • STL 内建了一些函数对象

分类

  • 算数仿函数
  • 关系仿函数
  • 逻辑仿函数

用法

  • 这些仿函数所产生的对象,用法和一般函数完全相同
  • 使用内建函数对象,需要引入头文件#include<functional>

算数仿函数

功能描述

  • 实现四则运算
  • 其中 negate 是一元运算,其他都是二元运算

仿函数原型

  • template<class T> T plus<T>//加法仿函数
  • template<class T> T minus<T>//减法仿函数
  • template<class T> T multiplies<T>//乘法仿函数
  • template<class T> T divides<T>//除法仿函数
  • template<class T> T modulus<T>//取模仿函数
  • template<class T> T negate<T>//取反仿函数

示例

#include <iostream>
#include <functional>
using namespace std;

void test01() {
    
    
	negate<int>n;
	cout << n(50) << endl;
}

void test02() {
    
    
	plus<int>p;
	cout << p(1, 2) << endl;
}

int main() {
    
    
	test01();
	test02();

	return 0;
}

关系仿函数

功能描述

  • 实现关系对比

仿函数原型

  • template<class T> bool equal_to<T>//等于
  • template<class T> bool not_equal_to<T>//不等于
  • template<class T> bool greater<T>//大于
  • template<class T> bool greater_equal<T>//大于等于
  • templata<class T> bool less<T>//小于
  • templata<class T> bool less_equal<T>//小于等于

示例

#include<iostream>
#include <vector>
#include <algorithm>
using namespace std;

void test01() {
    
    
	vector<int>v;

	v.push_back(10);
	v.push_back(40);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);

	for (vector<int>::iterator it = v.begin();it != v.end();it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
	sort(v.begin(), v.end(),greater<int>());
	for (vector<int>::iterator it = v.begin();it != v.end();it++) {
    
    
		cout << *it << " ";
	}
}

int main() {
    
    
	test01();

	return 0;
}

逻辑仿函数

功能描述

  • 实现逻辑运算

函数原型

  • templatate<class T> bool logical_and<T>//逻辑与
  • template<class T> bool logical_or<T>//逻辑或
  • template<class T> bool logical_not<T>//逻辑非

示例

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

void test01() {
    
    
	vector<bool>v;

	v.push_back(true);
	v.push_back(false);
	v.push_back(true);
	v.push_back(false);
	v.push_back(true);

	for (vector<bool>::iterator it = v.begin();it != v.end();it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;

	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 << " ";
	}
}

int main() {
    
    
	test01();

	return 0;
}

STL 常用算法

常用遍历算法

算法简介

  • for_each//遍历容器
  • transform//搬运容器到另一个容器中

for_each

功能描述

  • 实现遍历容器

函数原型

  • for_each(iterator beg,iterator end,_func);//遍历算法 遍历容器元素,beg 开始迭代器,end 结束迭代器,_func 函数或者函数对象

示例

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void print(int val) {
    
    
	cout << val << " ";
}

class Print01 {
    
    
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(), print);

	cout << endl;

	for_each(v.begin(), v.end(), Print01());
}

int main() {
    
    
	test01();

	return 0;
}

transform

功能描述

  • 搬运容器到另一个容器中

函数原型

  • transform(iterator beg1,iterator end1,iterator beg2,_func);//beg1 原容器开始迭代器,end1 原容器结束迭代器,beg2 目标容器开始迭代器,_func 函数或者函数对象

示例

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Transform {
    
    
public:
	int operator()(int val) {
    
    
		return val;
	}
};

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> v1;
	v1.resize(v.size());
	transform(v.begin(), v.end(), v1.begin(), Transform());

	for_each(v1.begin(), v1.end(), MyPrint());
}

int main() {
    
    
	test01();

	return 0;
}

查找常用算法

find

功能描述

  • 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器 end()

函数原型

  • find(iterator beg,iterator end,value);//按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置,beg开始迭代器,end 结束迭代器,value 查找的元素

示例

#include <iostream>
#include <vector>
using namespace std;

void test01() {
    
    
	vector<int>v;
	for (int i = 0;i < 10;i++) {
    
    
		v.push_back(i);
	}
	vector<int>::iterator it = find(v.begin(), v.end(), 5);
	if (it != v.end()) {
    
    
		cout << *it << endl;
	}
}

class Person {
    
    
public:
	Person(string name, int age) {
    
    
		this->m_Name = name;
		this->m_Age = age;
	}
public:
	bool operator==(const Person& p) {
    
    
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
    
    
			return true;
		}
		return false;
	}

	string m_Name;
	int m_Age;
};

void test02() {
    
    
	vector<Person>v;
	Person p1("a", 10);
	Person p2("b", 20);
	Person p3("c", 30);
	Person p4("d", 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 << (*it).m_Name << " " << (*it).m_Age << endl;
	}
}

int main() {
    
    
	test01();
	test02();

	return 0;
}

find_if

功能描述

  • 按条件查找

函数原型

  • find_if(iterator beg,iterator end,_Pred);//按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置,beg 开始迭代器,end 结束迭代器,_Pred 函数或者谓词(返回bool 类型的仿函数)

示例

#include <iostream>
#include <vector>
using namespace std;

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);
	}
	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it != v.end()) {
    
    
		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 PersonGreaterTwenty {
    
    
public:
	bool operator()(Person& p)const {
    
    
		return p.m_Age >= 20;
	}
};

void test02() {
    
    
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);

	vector<Person>v;
	
	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(), PersonGreaterTwenty());
	if (it != v.end()) {
    
    
		cout << (*it).m_Name << " " << (*it).m_Age;
	}
}

int main() {
    
    
	test01();
	test02();

	return 0;
}

adjacent_find

功能描述

  • 查找相邻重复元素

函数原型

  • adjacent_find(iterator beg,iterator end);//查找相邻重复元素,返回相邻元素的第一个位置的迭代器,beg 开始迭代器,end 结束迭代器

示例

#include<iostream>
#include <vector>
#include <algorithm>
using namespace std;

void test01() {
    
    
	vector<int>v;

	v.push_back(1);
	v.push_back(1);
	v.push_back(0);
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(1);

	vector<int>::iterator it = adjacent_find(v.begin(), v.end());
	if (it != v.end()) {
    
    
		cout << *it << endl;
	}
}

int main() {
    
    
	test01();

	return 0;
}

binary_search

功能描述

  • 查找指定元素是否存在

函数原型

  • bool binary_search(iterator beg,iterator end,value);//查找指定的元素,查到返回 true,否则 false,注意:在无序序列中不可用,beg 开始迭代器,end 结束迭代器,value 查找的元素

示例

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

void test01() {
    
    
	vector<int>v;
	for (int i = 0;i < 10;i++) {
    
    
		v.push_back(i);
	}
	bool res = binary_search(v.begin(), v.end(), 8);
	if (res) {
    
    
		cout << "found" << endl;
	}
}

int main() {
    
    
	test01();

	return 0;
}

count

功能描述

  • 统计元素个数

函数原型

  • count(iterator beg,iterator end,value);//统计元素出现次数,beg 开始迭代器,end 结束迭代器,value 统计的元素

示例

#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;

void test01() {
    
    
	vector<int>v;

	v.push_back(1);
	v.push_back(11);
	v.push_back(123);
	v.push_back(145);
	v.push_back(1);
	v.push_back(1);
	v.push_back(51);
	v.push_back(1);

	int res = count(v.begin(), v.end(), 1);
	cout << res << endl;
}

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

	bool operator==(const Person& p) {
    
    
		if (this->m_Age == p.m_Age) {
    
    
			return true;
		}
		return false;
	}
	
	string m_Name;
	int m_Age;
};

void test02() {
    
    
	Person p1("aaa", 1);
	Person p2("aba", 13);
	Person p3("ada", 15);
	Person p4("axa", 1);
	Person p5("afa", 1);

	vector<Person>v;
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	int res = count(v.begin(), v.end(), p1);
	cout << res << endl;
}

int main() {
    
    
	test01();
	test02();

	return 0;
}

count_if

功能描述

  • 按条件统计元素个数

函数原型

  • count_if(iterator beg,iterator end,_Pred);//按条件统计元素出现次数,beg 开始迭代器,end 结束迭代器,_Pred谓词

示例

#include <iostream>
#include<algorithm>
#include <vector>
using namespace std;

class GreaterTwenty {
    
    
public:
	bool operator()(int val) {
    
    
		return val > 20;
	}
};

void test01() {
    
    
	vector<int>v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(20);
	v.push_back(10);

	int res = count_if(v.begin(), v.end(), GreaterTwenty());
	cout << res << endl;
}

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

	string m_Name;
	int m_Age;
};

class AgeGreateTwenty {
    
    
public:
	bool operator()(Person& p)const {
    
    
		return p.m_Age >= 2;
	}
};

void test02() {
    
    
	Person p1("a", 1);
	Person p2("b", 2);
	Person p3("c", 3);
	Person p4("d", 2);
	Person p5("e", 1);

	vector<Person>v;

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	int res = count_if(v.begin(), v.end(), AgeGreateTwenty());
	cout << res << endl;
}

int main() {
    
    
	test01();
	test02();

	return 0;
}

常用排序算法

sort

功能描述

  • 对容器内元素进行排序

函数原型

  • sort(iterator beg,iterator end,_Pred);//按值查找元素,找回返回指定位置迭代器,找不到返回结束迭代器位置,beg 开始迭代器,end 结束迭代器,_Pred 谓词

示例

#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;

void myPrint(vector<int>& v) {
    
    
	for (vector<int>::iterator it = v.begin();it != v.end();it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
}

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

void test01() {
    
    
	vector<int>v;

	v.push_back(1);
	v.push_back(5);
	v.push_back(2);
	v.push_back(4);
	v.push_back(3);

	sort(v.begin(), v.end(), Greater());
	myPrint(v);
}

int main() {
    
    
	test01();

	return 0;
}

random_shuffle

功能描述

  • 洗牌 指定范围内的元素随机调整次序

函数原型

  • random_shuffle(iterator beg,iterator end);//指定范围内的元素随机调整次序,beg 开始迭代器,end 结束迭代器

示例

#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;

void myPrint(int val) {
    
    
	cout << val << " ";
}

void test01() {
    
    
	srand((unsigned int)time(NULL));
	vector<int>v;
	for (int i = 0;i < 10;i++) {
    
    
		v.push_back(i);
	}
	random_shuffle(v.begin(), v.end());
	for_each(v.begin(), v.end(), myPrint);
}

int main() {
    
    
	test01();

	return 0;
}

merge

功能描述

  • 两个容器元素合并,存储到另一个容器中

函数原型

  • merge(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);//容器元素合并,并存储到另一容器中,注意:两个容器必须是有序的,beg1 容器 1 开始迭代器,end1 容器 1 结束迭代器,beg2 容器 2 开始迭代器,end2 容器 2 结束迭代器,dest 目标容器开始迭代器

示例

#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;

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>v3;
	v3.resize(v1.size() + v2.size());
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());

	for (vector<int>::iterator it = v3.begin();it != v3.end();it++) {
    
    
		cout << *it << " ";
	}
}

int main() {
    
    
	test01();

	return 0;
}

reverse

功能描述

  • 将容器内元素进行反转

函数原型

  • reverse(iterator beg,iterator end);//反转指定范围的元素,beg 开始迭代器,end 结束迭代器

示例

#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;

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);
	}

	reverse(v.begin(), v.end());
	for_each(v.begin(), v.end(), MyPrint());
}

int main() {
    
    
	test01();

	return 0;
}

常用拷贝和替换算法

copy

功能描述

  • 容器内指定范围的元素拷贝到另一容器中

函数原型

  • copy(iterator beg,iterator end,iterator dest);//按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置,beg 开始迭代器,end 结束迭代器,dest 目标起始迭代器

示例

#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;

class MyPrint {
    
    
public:
	void operator()(int val) {
    
    
		cout << val << " ";
	}
};

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

int main() {
    
    
	test01();

	return 0;
}

replace

功能描述

  • 将容器内指定范围的旧元素修改为新元素

函数原型

  • replace(iterator beg,iterator end,oldvalue,newvalue);//将区间内旧元素替换成新元素,beg 开始迭代器,end 结束迭代器,oldvalue 旧元素,newvalue 新元素

示例

#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;

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);
	}

	replace(v.begin(), v.end(), 3, -1);
	for_each(v.begin(), v.end(), MyPrint());
}

int main() {
    
    
	test01();

	return 0;
}

replace_if

功能描述

  • 将区间内蛮族条件的元素,替换成指定元素

函数原型

  • replace_if(iterator beg,iterator end,_pred,newvalue);//按条件替换元素,满足条件的替换成指定元素,beg 开始迭代器,end 结束迭代器,_pred 谓词,newvalue 替换的新元素

示例

#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;

class GreaterThirty {
    
    
public:
	bool operator()(int val) {
    
    
		return val > 30;
	}
};

class MyPrint {
    
    
public:
	void operator()(int val) {
    
    
		cout << val << " ";
	}
};

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);

	replace_if(v.begin(), v.end(), GreaterThirty(), -1);
	
	for_each(v.begin(), v.end(), MyPrint());
}

int main() {
    
    
	test01();

	return 0;
}

swap

功能描述

  • 互换两个容器的元素

函数原型

  • swap(container c1,container c2);//互换两个容器的元素,c1 容器 1,c2 容器 2

示例

#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;

class MyPrint {
    
    
public:
	void operator()(int val) {
    
    
		cout << val << " ";
	}
};

void test01() {
    
    
	vector<int>v1;
	vector<int>v2;
	for (int i = 0;i < 10;i++) {
    
    
		v1.push_back(i);
	}
	for (int i = 9;i > 0;i--) {
    
    
		v2.push_back(i);
	}
	
	swap(v1, v2);

	for_each(v1.begin(), v1.end(), MyPrint());
	cout << endl;
	for_each(v2.begin(), v2.end(), MyPrint());
}

int main() {
    
    
	test01();

	return 0;
}

常用算术生成算法

accumulate

功能描述

  • 计算区间内容器元素累计总和

函数原型

  • accumulate(iterator beg,iterator,end,value);//计算容器元素累计总和,beg 开始迭代器,end 结束迭代器,value 起始值

示例

#include<iostream>
#include<numeric>
#include <vector>
using namespace std;

void test01() {
    
    
	vector<int>v;

	for (int i = 0;i <= 100;i++) {
    
    
		v.push_back(i);
	}

	int res = accumulate(v.begin(), v.end(), 0);
	cout << res << endl;
}

int main() {
    
    
	test01();

	return 0;
}

fill

功能描述

  • 向容器中填充指定的元素

函数原型

  • fill(iterator beg,iterator end,value);//向容器中填充元素,beg 开始迭代器,end 结束迭代器,value 填充的值

示例

#include<iostream>
#include<numeric>
#include <vector>
#include<algorithm>
using namespace std;

class MyPrint {
    
    
public:
	void operator()(int val) {
    
    
		cout << val << endl;
	}
};

void test01() {
    
    
	vector<int>v;
	v.resize(10);

	fill(v.begin(), v.end(), 100);
	for_each(v.begin(), v.end(), MyPrint());
}

int main() {
    
    
	test01();

	return 0;
}

常用集合算法

set_intersection

功能描述

  • 求两个容器的交集

函数原型

  • set_intersection(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);//求两个集合的交集,注意:两个集合必须是有序序列,beg1 容器 1 开始迭代器,end1 容器 1 结束迭代器,beg2 容器 2 开始迭代器,end2 容器 2 结束迭代器,dest 目标容器开始迭代器

示例

#include<iostream>
#include <vector>
#include<algorithm>
using namespace std;

class MyPrint {
    
    
public:
	void operator()(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 + 5);
	}

	vector<int>v3;
	v3.resize(min(v1.size(), v2.size()));

	vector<int>::iterator it = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), it, MyPrint());
}

int main() {
    
    
	test01();

	return 0;
}

set_union

功能描述

  • 求两个集合的并集

函数原型

  • set_union(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);//求两个集合的并集,注意:两个集合必须是有序序列,beg1 容器 1 开始迭代器,end1 容器 1 结束迭代器,beg 2 容器 2 开始迭代器,end 2 容器 2 结束迭代器,dest 目标容器开始迭代器

示例

#include<iostream>
#include<algorithm>
#include <vector>
using namespace std;

class MyPrint {
    
    
public:
	void operator()(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 + 5);
	}

	vector<int>v3;
	v3.resize(v1.size()+v2.size());
	vector<int>::iterator it = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), it, MyPrint());
}

int main() {
    
    
	test01();

	return 0;
}

set_difference

功能描述

  • 求两个集合的差集

函数原型

  • set_difference(iterator beg1,iterator end1,iterator beg2,iterator end2,iterator dest);//求两个集合的差集,注意:两个集合必须是有序序列,beg1 容器 1 开始迭代器,end1 容器 1 结束迭代器,beg2 容器 2 开始迭代器,end2 容器 2 结束迭代器,dest 目标容器开始迭代器

示例

#include<iostream>
#include <vector>
#include<algorithm>
using namespace std;

class MyPrint {
    
    
public:
	void operator()(int val) {
    
    
		cout << val << " ";
	}
};

void test01() {
    
    
	vector<int>v1;
	vector<int>v2;
	vector<int>v3;
	
	for (int i = 0;i < 10;i++) {
    
    
		v1.push_back(i);
		v2.push_back(i + 5);
	}

	v3.resize(max(v1.size(),v2.size()));

	vector<int>::iterator it = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), it, MyPrint());
}

int main() {
    
    
	test01();

	return 0;
}