algorithm,numeric常用算法函数

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

常用遍历算法:
for_each(iterator beg, iterator end, _func);						//遍历容器
transform(iterator beg1, iterator end1, iterator beg2, _func);		//搬运容器到另一个容器
常用的查找算法:
find((iterator beg, iterator end, val)			//查找元素
find_if	(iterator beg, iterator end, _Pred)		//按条件查找
abjacent_find (iterator beg, iterator end)		//查找相邻重复元素
bool binary_search (iterator beg, iterator end, value)	//二分查找法
//查找指定元素,查到返回true 否则返回false.		在无序序列中不可用
count(iterator beg, iterator end, value)		//统计元素个数
count_if (iterator beg, iterator end, _Pred)	//按条件统计元素个数

常用的排序算法:
sort (iterator beg, iterator end, _Pred);		//按照_Pred规则排序,默认升序
random_shuffle (iterator beg, iterator end);	//指定范围内的元素,随机调整次序
merge (iterator beg1, iterator end1, iterator beg2, iterator end2, iterator vTarget.beg);
//将两个指定范围内的容器元素合并到目标容器中
//两个容器必须是有序的,且顺序一致
//需要提前分配目标容器的内存
reverse (iterator beg, iterator end);			//将指定范围内的元素反转

常用的拷贝和替换算法
copy (iterator beg1, iterator end1, iterator beg2);
//将指定容器范围内的元素复制到另一容器中
//需要提前开辟接收容器的内存
replace (iterator beg, iterator end, oldvalue, newvalue);
//将容器中指定范围内的旧元素替换为新元素
replace_if (iterator beg, iterator end, _Pred, newvalue);
//将指定范围内满足条件的元素,替换为newvalue
swap (container c1, container c2);
//互换两个容器的元素	同类型容器

常用的算术生成算法
#include <numeric>
accumulate (iterator beg, iterator end, value);		//累加操作
//value 起始累加值
fill (iterator beg, iterator end, value);
//向指定范围的容器中填充指定元素

常用的集合算法
//以下算法需要提前开辟目标容器的空间
//两个集合必须是有序序列
set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator vTarget.beg);
//交集
//返回目标容器的最后一个元素的迭代器地址
set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator vTarget.beg);
//并集
set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator vTarget.beg);
//求范围1中元素对范围2中元素的差集
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
//内建函数对象头文件
#include <functional>
#include <numeric>
//打印函数
void myPrint(int val)
{
	cout << val << endl;
}
//打印仿函数
class MyPrint
{
public:
	void operator()(int val) {
		cout << val << endl;
	}
};
//返回原值函数
class Transform
{
public:
	int operator()(int val) {
		return val;
	}
};
//自定义从大到小排序函数
bool  Mycompare(int a, int b)
{
	return a > b;
}
//自定义小于3的返回真仿函数
class Lower3
{
public:
	bool operator()(int val) {
		return val < 3;
	}
};

for_each和transform

void test1()
{
	//for_each
	vector<int> v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(50);
	v.push_back(20);
	for_each(v.begin(), v.end(), myPrint);
	cout << "************************************" << endl;
	for_each(v.begin(), v.end(), MyPrint());
	//transform
	vector<int> v2;
	v2.push_back(100);
	v2.push_back(500);
	//目标容器要提前开辟空间
	v2.resize(v.size() + v2.size());
	//会覆盖掉,目标容器起始位置之后的内容
	transform(v.begin(), v.end(), v2.begin() + 2, Transform());
	cout << "*********************************" << endl;
	for_each(v.begin(), v.end(), MyPrint());
	cout << "*********************************" << endl;
	for_each(v2.begin(), v2.end(), myPrint);
}

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

find\find_if\binary_search\count

void test2()
{
	vector<int> v;
	//find
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
	}
	//查找容器中是否有5这个元素
	vector<int> :: iterator it = find(v.begin(), v.end(), 5);
	vector<int> :: iterator it = find_if(v.begin(), v.end(), GreaterFive());
	//注意容器必须是有序序列
	bool ret = binary_search(v.begin(), v.end(), 9);
	int num = count(v.begin(), v.end(), 3);
	cout << "3的个数为: " << num << endl;
	int num2 = count_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end()) {
		cout << "没有找到" << endl;
	}
	else {
		cout << *it << endl;
	}
	//自定义数据类型
	vector<Person> v2;
	Person p1("zhangsan", 20);
	Person p2("lisi", 23);
	Person p3("wangwu", 10);
	Person p4("liuer", 18);
	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	//需要重载 == 
	vector<Person> ::iterator it2 = find(v2.begin(), v2.end(), p2);
	vector<Person> ::iterator it3 = find_if(v2.begin(), v2.end(), Greater20());
	int num = count (v.begin(), v.end(), p2);
	int num3 = count_if(v2.begin(), v2.end(), Greater20());
	cout << "大于20岁的人数为: " << num3 << endl;
	if (it2 == v2.end()) {
		cout << "没有找到" << endl;
	}
	else {
		cout << "存在" << endl;
	}
	if (it3 == v2.end()) {
		cout << "没找到" << endl;
	}
	else {
		cout << "姓名为: " << it3->m_Name << "  年龄为 : " << it3->m_Age << endl;
	}
}

adjacent_find

void test3()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(1);
	v.push_back(3);
	v.push_back(4);
	v.push_back(3);
	v.push_back(3);
	vector<int> ::iterator pos = adjacent_find(v.begin(), v.end());
	if (pos == v.end()) {
		cout << "没有找到" << endl;
	}
	else {
		cout << "存在相邻重复元素为: " << *pos << endl;
	}
}
void test4()
{
	vector<int> v;
	vector<int> v2;
	for (int i = 0; i < 10; i++) {
		v.push_back(i);
		v2.push_back(i - 1);
	}
	for_each(v.begin(), v.end(), Myprint());
	cout << endl;
	random_shuffle(v.begin(), v.end());
	for_each(v.begin(), v.end(), Myprint());
	cout << endl;
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), Myprint());
	cout << endl;
	sort(v.begin(), v.end(), Mycompare);
	for_each(v.begin(), v.end(), Myprint());
	cout << endl;
	//开辟空间
	vector<int> v3;
	v3.resize(v.size() + v2.size());
	//v和v2顺序不一致导致崩了,调整一下v1排序
	sort(v.begin(), v.end());
	merge(v.begin(), v.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), v3.end(), Myprint());
	cout << endl;
	reverse(v.begin(), v.end());
	for_each(v.begin(), v.end(), Myprint());
	cout << endl;
	//copy,merge需要开辟足够空间
	v2.resize(v2.size() + v.size());
	copy(v.begin(), v.end(), v2.begin() + 3);
	for_each(v2.begin(), v2.end(), Myprint());
	cout << endl;
	replace(v2.begin(), v2.end(), 5, 888888);
	for_each(v2.begin(), v2.end(), Myprint());
	cout << endl;
	replace_if(v2.begin(), v2.end(), Lower3(), 9999);
	for_each(v2.begin(), v2.end(), Myprint());
	cout << endl;
	swap(v, v2);
	for_each(v2.begin(), v2.end(), Myprint());
	cout << endl;
	int sum = accumulate(v2.begin(), v2.end(), 1000);
	cout << sum << endl;
	fill(v.begin() + 3, v.end(), 6);
	for_each(v.begin(), v.end(), Myprint());
	cout << endl;
	vector<int> v4;
	//开辟适合的空间
	v4.resize(min(v.size(), v2.size()));
	//必须是有序的
	sort(v.begin(), v.end());
	sort(v2.begin(), v2.end());
	set_intersection(v.begin(), v.end(), v2.begin(), v2.end(), v4.begin());
	for_each(v4.begin(), v4.end(), Myprint());
	cout << endl;
	v4.resize(v.size() + v2.size());
	set_union(v.begin(), v.end(), v2.begin(), v2.end(), v4.begin());
	for_each(v4.begin(), v4.end(), Myprint());
	cout << endl;
	set_difference(v.begin(), v.end(), v2.begin(), v2.end(), v4.begin());
	for_each(v4.begin(), v4.end(), Myprint());
	cout << endl;
}
int main()
{
	test1();
	test2();
	test3();
	test4();
	system("pause");
	return 0;
}

黑马程序员课程学习笔记

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值