cpp代码学习-常用算法

目录

常用遍历算法

for_each

transform

常用查找算法

常用查找算法

find_if

count_if

count

binary_search

adjacent_find

常用集合算法

set_union

set_intersection

set_intersecting2

set_difference

常用拷贝和构造算法

copy

replace

replace_if

swap

常用排序算法

sort

reverse

random_shuffle

merge

常用算术生成算法

accumulate

fill


常用遍历算法

for_each

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

/*
* 算法主要是由头文件<algorithm> <functional><numeric>组成。
* <algorithm> 是所有STL头文件中最大的一个,范围涉及到比较、交换、查找、遍历操作、复制、修改等等
* <numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数
* <functional>定义了一些模板类,用以声明函数对象
*/

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

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

class print
{
public:
	void operator()(int v) const
	{
		cout << v << " ";
	}
};

//普通函数
void print01(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(), print());
	cout << endl;
}

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

transform

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

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

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

class myPrint
{
public:
	void operator()(int v)
	{
		cout << v << " ";
	}
};

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>vTarget;
	vTarget.resize(v.size()); //目标容器提前开辟空间
	transform(v.begin(), v.end(), vTarget.begin(), Transform());
	for_each(vTarget.begin(), vTarget.end(), myPrint());
	cout << endl;
}

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

常用查找算法

常用查找算法

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

/*
* find				查找元素
* find_if			按条件查找元素
* adjacent_find		查找相邻重复元素
* binary_search		二分查找法
* count				统计元素个数
* count_if			按条件统计元素个数
*/

//find  查找指定元素  找到返回指定元素的迭代器, 找不到返回结束迭代器

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


//查找内置数据类型
void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator pos = find(v.begin(), v.end(),50);
	if (pos == v.end())
	{
		cout << "未找到!" << endl;
	}
	else
	{
		cout << "找到!"<<*pos << 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;
		}
	}

	string m_Name;
	int m_Age;
};

//class MyCompare
//{
//public:
//	bool operstor()(Person& p1, Person& p2)
//	{
//
//	}
//};


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 << "未找到!" << endl;
	}
	else
	{
		cout << "找到!" << endl << "name: " << it->m_Name << "  age:" << it->m_Age << endl;
	}
}

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

find_if

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

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

//第一种 查找内置数据类型



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

class GreaterTwenty
{
public:
	bool operator()(Person p) const
	{
		return p.m_Age > 20;
	}
};

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 << "未找到!" << endl;
	}
	else
	{
		cout << "找到大于5的数字为: " << *it << endl;
	}
}

void test02()
{
	vector<Person>v;
	Person p1("aHRY", 20);
	Person p2("bHRY", 30);
	Person p3("cHRY", 40);
	Person p4("dHRY", 50);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	//找年龄大于20的成员
	vector<Person>::iterator it = find_if(v.begin(), v.end(), GreaterTwenty());
	if (it == v.end())
	{
		cout << "未找到!" << endl;
	}
	else
	{
		cout << "找到年龄大于20的! name: "<<it->m_Name << endl;
	}
}

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

count_if

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

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

//内置数据类型
class GreaterFive
{
public:
	bool operator()(const int val)
	{
		return val > 5;
	}
};
void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	cout << "大于 5 的元素个数有 " << count_if(v.begin(), v.end(), GreaterFive()) << " 个" << 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()(const Person& p)
	{
		return p.m_Age > 20;
	}
};
void test02()
{
	vector<Person>v;
	Person p1("aHRY", 20);
	Person p2("bHRY", 30);
	Person p3("cHRY", 40);
	Person p4("dHRY", 50);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	cout << "年龄大于 20 的有 " << count_if(v.begin(), v.end(), Greater20()) <<" 个" << endl;
}

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

count

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

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

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	v.push_back(5);
	cout << "5共出现 " << count(v.begin(), v.end(), 5) << " 次" << endl;
}

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	bool operator==(const Person &p)//底层对比 加上const   一定要加
	{ 
		if (p.m_Name == this->m_Name && p.m_Age == this->m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	string m_Name;
	int m_Age;
};

void test02()
{
	vector<Person>v;
	Person p1("aHRY", 20);
	Person p2("bHRY", 30);
	Person p3("cHRY", 40);
	Person p4("dHRY", 50);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	cout << "aHRY共出现 " << count(v.begin(), v.end(), p1) << " 次" << endl;
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>

/*
* 二分查找
* 速度非常快  但是要注意在有序序列
* bool binary_search(iterator beg, iterator end, end);
* 查找指定元素 , 查到返回ture 否则返回false
* 注意: 在无序序列中不可用
* beg 开始迭代器
* end 结束迭代器
* value 查找的元素
*/

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	bool a = binary_search(v.begin(), v.end(), 5);
	if (a)
	{
		cout << "找到 5 !" << endl;
	}
	else
	{
		cout << "未找到 5 !" << endl;
	}
}

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

adjacent_find

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

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

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	v.push_back(9);
	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;
}

常用集合算法

set_union

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

/*
* set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
beg1 ——  容器 1 开始迭代器
end1 ——  容器 1  结束迭代器
beg2 —— 容器 2 开始迭代器
end2 —— 容器 2 结束迭代器
dest  —— 目标容器开始迭代器
*/

/*
* set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
* set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest, _Pred);
* 在对自定义数据类型进行操作时,要加上谓词进行排序
*/

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

ostream& operator<<(ostream& cout, const Person& p) {
	cout << p.m_Name << " " << p.m_Age << endl;
	return cout;
}

template<class T>
void printVector(const T& a)
{
	cout << a << " ";
}

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(v1.size() + v2.size());
	vector<int>::iterator end=set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), end, printVector<int>);
}

class Bijiao
{
public:
	bool operator()(const Person& p1, const Person& p2)
	{
		return (p1.m_Age < p2.m_Age);
	}
};

void test02()
{
	vector<Person>v1;
	vector<Person>v2;

	Person p1("A", 2);
	Person p2("B", 1);
	Person p3("C", 5);
	Person p4("D", 3);

	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);

	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);

	sort(v1.begin(), v1.end(), Bijiao());
	sort(v2.begin(), v2.end(), Bijiao());

	vector<Person>v3;
	v3.resize(v1.size() + v2.size());

	vector<Person>::iterator end = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin(), Bijiao());
	for_each(v3.begin(), end, printVector<Person>);
}

int main()
{
	test01();
	cout << endl<<" ";
	test02();
	system("pause");
	return 0;
}

set_intersection

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

/*
* 掌握常用的集合算法
* set_intersection   求两个容器的交集
* set_uniom			 求两个容器的并集
* set_difference	 求两个容器的差集
*/

/*
* set_intersection   求两个容器的交集 
* 容器必须是有序序列
* 返回的迭代器指向最后一个元素
*/

/*
* set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
* set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest, _Pred)
在对自定义数据类型进行操作时,要加上谓词进行排序
*/


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

ostream& operator<<(ostream& cout, const Person& p) 
{
	cout << p.m_Name << " " << p.m_Age << endl;
	return cout;
}

template<class T>
void PrintVector(const T& x)
{
	cout << x << " ";
}

//class Print
//{
//public:
//	void operator()(int a)
//	{
//		cout << a << " ";
//	}
//};

//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 itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
//
//	for_each(v3.begin(), itEnd, PrintVector<Person>);
//	cout << endl;
//}

class PersonSort
{
	bool operator()(const Person& p1, const Person& p2)
	{
		return (p1.m_Age < p2.m_Age);
	}
};

void test02()
{
	vector<Person>v1;
	vector<Person>v2;
	Person p1("A", 1);
	Person p2("B", 3);
	Person p3("C", 4);
	Person p4("B", 5);
	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	v1.push_back(p4);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);

	sort(v1.begin(), v1.end(), PersonSort());
	sort(v2.begin(), v2.end(), PersonSort());

	vector<Person>v3;

	// 计算大小目标容器需要的合适大小
	size_t v_size = (v1.size() > v2.size()) ? v2.size() : v1.size();

	v_size = min(v1.size(), v2.size()); // min 算法

	// 需要给目标容器开辟内存 v_size
	v3.resize(v_size);

	vector<Person>::iterator itEnd = set_intersection
	(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin(),PersonSort());
	//注意自定义数据类型要加上排序仿函数 !!!!!!!!!!

	for_each(v3.begin(), itEnd, PrintVector<Person>); // 遍历
	cout << endl;

}

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

set_intersecting2

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

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

ostream& operator<<(ostream& cout, const Person& p) {
    cout << p.m_Name << " " << p.m_Age << endl;
    return cout;
}

template<typename T>
void PrintVector(const T& x) {
    cout << x << " ";
}

// 内置数据类型交集
void test01() {
    vector<int>v1; // 有序
    v1.push_back(3);
    v1.push_back(4);
    v1.push_back(5);
    v1.push_back(6);
    vector<int>v2; // 有序
    v2.push_back(4);
    v2.push_back(5);
    v2.push_back(6);

    vector<int>v_target; // 目标容器

    // 计算大小目标容器需要的合适大小
    size_t v_size = (v1.size() > v2.size()) ? v2.size() : v1.size();

    // 或者用 min 算法
    v_size = min(v1.size(), v2.size());

    // 需要给目标容器开辟内存 v_size
    v_target.resize(v_size);  // 这个空间可能大了,默认值为 0 

    // 求集合交集到目标容器,返回的是最后一个交集的迭代器
    vector<int>::iterator EndIt = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v_target.begin());

    // 遍历的时候可以不用遍历到容器末尾,只需要遍历到交集的最后一个元素即可
    for_each(v_target.begin(), EndIt, PrintVector<int>); // 遍历
    cout << endl;
}

class Person_sort {
public:
    bool operator()(const Person& p1, const Person& p2) {
        return (p1.m_Age < p2.m_Age);
    }
};

// 自定义数据类型交集
void test02() {
    vector<Person>v1; // 有序
    vector<Person>v2; // 有序
    Person p1("A", 19);
    Person p2("B", 18);
    Person p3("C", 20);
    Person p4("D", 21);

    v1.push_back(p1);
    v1.push_back(p2);
    v1.push_back(p3);

    v2.push_back(p1);
    v2.push_back(p2);
    v2.push_back(p3);
    v2.push_back(p4);

    sort(v1.begin(), v1.end(), Person_sort());
    sort(v2.begin(), v2.end(), Person_sort());

    vector<Person>v_target;

    // 计算大小目标容器需要的合适大小
    size_t v_size = (v1.size() > v2.size()) ? v2.size() : v1.size();

    v_size = min(v1.size(), v2.size()); // min 算法

    // 需要给目标容器开辟内存 v_size
    v_target.resize(v_size);

    // 放上排序规则仿函数,返回最后一个交集的迭代器
    vector<Person>::iterator EndIt = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v_target.begin(), Person_sort());
    for_each(v_target.begin(), EndIt, PrintVector<Person>); // 遍历
    cout << endl;
}

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

set_difference

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


/*
* set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
* set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest, _Pred);
* 在对自定义数据类型进行操作时,要加上谓词进行排序
* 
* set集合操作的全部是有序序列
* 
* v1和v2的差集   v2和v1的差集时不一样的
*/


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

ostream& operator<<(ostream& cout, const Person& p)
{
    cout << p.m_Name << " " << p.m_Age << endl;
    return cout;
}

template<typename T>
void PrintVector(const T& x)
{
    cout << x << " ";
}

// 内置数据类型差集
void test01() {
    vector<int>v1; // 有序
    vector<int>v2; // 有序

    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(30);
    v1.push_back(40);

    v2.push_back(30);
    v2.push_back(40);
    v2.push_back(50);
    v2.push_back(60);

    cout << "v1 与 v2 的差集:";
    vector<int>v_target1; // 目标容器
    v_target1.resize(max(v1.size(), v2.size())); // 容器大小
    vector<int>::iterator EndIt1 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v_target1.begin());
    for_each(v_target1.begin(), EndIt1, PrintVector<int>);
    cout << endl;

    cout << "v1 与 v2 的差集:";
    vector<int>v_target2; // 目标容器
    v_target2.resize(max(v1.size(), v2.size())); // 容器大小
    vector<int>::iterator EndIt2 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v_target2.begin());
    for_each(v_target2.begin(), EndIt2, PrintVector<int>);
    cout << endl;
}

// 年龄排序仿函数
class Person_sort 
{
public:
    bool operator()(const Person& p1, const Person& p2) 
    {
        return (p1.m_Age < p2.m_Age); // m_Age 降序
    }
};

// 自定义数据类型差集
void test02() 
{
    vector<Person>v1; // 有序
    vector<Person>v2; // 有序

    Person p1("A", 22);
    Person p2("B", 21);
    Person p3("C", 20);
    Person p4("D", 19);
    Person p5("E", 18);

    v1.push_back(p1);
    v1.push_back(p2);
    v1.push_back(p3);

    v2.push_back(p3);
    v2.push_back(p4);
    v2.push_back(p5);

    sort(v1.begin(), v1.end(), Person_sort()); // 排序
    sort(v2.begin(), v2.end(), Person_sort()); // 排序

    cout << "v1 和 v2 的差集:" << endl<<" ";
    vector<Person>v_target1;
    v_target1.resize(max(v1.size(), v2.size()));
    vector<Person>::iterator EndIt1 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v_target1.begin(), Person_sort());
    for_each(v_target1.begin(), EndIt1, PrintVector<Person>);
    cout << endl;

    cout << "v2 与 v1 的差集:" << endl<<" ";
    vector<Person>v_target2;
    v_target2.resize(max(v1.size(), v2.size()));
    vector<Person>::iterator EndIt2 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v_target2.begin(), Person_sort());
    for_each(v_target2.begin(), EndIt2, PrintVector<Person>);
    cout << endl;
}

int main() 
{
    test01();
    cout << endl;
    test02();
    system("pause");
    return 0;
}

常用拷贝和构造算法

copy

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

/*
* 掌握常用的拷贝和替换算法
* copy			容器内指定范围的元素拷贝到另一容器中 
* replace		将容器内指定范围的旧元素修改为新元素
* replace_if	容器内指定范围满足条件的元素替换为新元素
* swap			互换两个容器的元素
*/

/*
* copy(iterator beg, iterator end, iterator dest);
* beg开始迭代器
* end结束迭代器
* dest目标起始迭代器
*/

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

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

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

replace

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

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

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

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
	replace(v.begin(), v.end(), 2, 2000);
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}

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

replace_if

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

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

class Pred
{
public:
	bool operator()(int a)
	{
		if (a % 3 == 0)
		{
			return true;
		}
		return false;
	}
};

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

void test01()
{
	vector<int>a;
	for (int i = 0; i < 10; i++)
	{
		a.push_back(i);
	}
	for_each(a.begin(), a.end(), MyPrint());
	replace_if(a.begin(), a.end(), Pred(), 100);
	cout << endl;
	for_each(a.begin(), a.end(), MyPrint());
	cout << endl;
}

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

swap

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

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

void Print(int a)
{
	cout << a << " ";
}

void test01()
{
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 1);
	}
	cout << " v1 :";
	for_each(v1.begin(), v1.end(), Print);
	cout << " v2 :";
	for_each(v2.begin(), v2.end(), Print);
	swap(v1, v2);  //v1.swap(v2)  效果相同
	cout << "交换后: " << endl;
	cout << " v1 :";
	for_each(v1.begin(), v1.end(), Print);
	cout << " v2 :";
	for_each(v2.begin(), v2.end(), Print);
	cout << endl;

}

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

常用排序算法

sort

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

/*
* sort    对容器内元素进行排序
* random_shuffle  洗牌 指定范围内的元素随机调整次序
* merge  容器元素合并 并存储到另一容器中
* reverse  反转指定范围的内容
*/

//对容器内元素进行排序

/*
* sort(iterator beg, iterator end, _Pred);
*/

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

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

void test01()
{
	vector<int>v;
	for (int i = 4; i < 10; i++)
	{
		v.push_back(i);
	}
	v.push_back(2);
	sort(v.begin(), v.end());
	Print1(v);
	sort(v.begin(), v.end(),greater<int>());//  greater<int>()  内建函数对象
	sort(v.begin(), v.end(),bijiao());
	Print1(v);
}

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

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

void test02()
{
	vector<Person>v;
	Person p1("HRY1", 10);
	Person p2("HRY2", 20);
	Person p3("HRY3", 15);
	Person p4("HRY4", 18);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	sort(v.begin(), v.end(), paixu1());
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "name: " << it->m_Name << "  age: " << it->m_Age << endl;
	}
	cout << endl;
}

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

reverse

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

/*
* reverse(iterator beg, iterator end);
* 反转指定范围的元素
*/

void Print(int a)
{
	cout << a << " ";
}

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), Print);
	reverse(v.begin(), v.end());
	cout << endl;
	for_each(v.begin(), v.end(), Print);
}

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

random_shuffle

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

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

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

void MyPrint1(int a)
{
	cout << a << " ";
}

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

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

merge

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

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

void Print(int a)
{
	cout << a << " ";
}

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 + 2);
	}
	v3.resize(v1.size()+v2.size());//注意重定义v3的大小
	for_each(v1.begin(), v1.end(), Print);
	cout << endl;
	for_each(v2.begin(), v2.end(), Print);
	cout << endl;
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), v3.end(), Print);
}

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

常用算术生成算法

accumulate

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

/*
* 掌握常用的算术生成算法
* 算术生成算法属于小型算法,使用时包含头文件#include<numeric>
* 
* 算法简介:
* accumulate  计算容器中元素累计总和
* fill		  向容器中插入数据
* 
* accumulate(iterator beg, iterator end, value);
* beg开始迭代器
* end结束迭代器
* value起始值
*/

void test01()
{
	vector<int>v1;
	int value1 = 0;
	for (int i = 1; i <= 10; i++)
	{
		v1.push_back(i);
		value1 = value1 + i;
	}
	cout << "value1 = " << value1 << endl;
	int value2 = accumulate(v1.begin(), v1.end(), 0);;
	cout << "value2 = " << value2 << endl;
}

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

fill

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

/*
* fill(iterator beg, iterator end, value);
* value 填充的值
*/

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

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	cout << "修改前 v.size() = : " << v.size() << endl;
	cout << "v = : ";
	for_each(v.begin(), v.end(), MyPrint());
	fill(v.begin(), v.end(), 99);
	cout << endl << "修改后 v.size() = : " << v.size() << endl;
	cout << "v = : ";
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值