目录

4 STL-函数对象

237 函数对象

谓词

238 一元谓词

239 二元谓词

内建函数对象

240 算术仿函数

241 关系仿函数

242 逻辑仿函数

5 STL-常用算法

遍历算法

243 for_each

244 transform

查找算法

245 find

246 find_if

247 adjacent_find

248 binary_search

249 count

250 count_if

排序算法

251 sort 

252 random_shuffle

253 merge

254 reverse

拷贝和替换算法

255 copy

256 replace

257 replace_if

258 swap

算术生成算法

259 accumulate

260 fill

集合算法

261 set_intersection

262 set_union

263 set_difference


4 STL-函数对象

237 函数对象

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_c++

//可以有参数,可以有返回值
class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};

//可以有自己的状态
class MyPrint
{
public:
	MyPrint()
	{
		this->count = 0;
	}

	void operator()(string test)
	{
		cout << test << endl;
		this->count++;
	}
	int count;//可以记录调用次数
};

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

//函数对象可以作为参数传递
void test()
{
	MyPrint myprint;
	doPrint(myprint, "hellosfwe");
}

谓词

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_数据结构_02

238 一元谓词

//查找有没有大于5的元素

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 << "no" << endl;
	}
	else
	{
		cout << "yes:" << *it<<endl;
	}
}

239 二元谓词

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

void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(5);
	v.push_back(15);
	
	sort(v.begin(), v.end());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;//5 10 15

	//使用函数对象 改变排序策略 变为降序
	sort(v.begin(), v.end(), MyCompare());
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;//15 10 5
}

内建函数对象

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_i++_03

240 算术仿函数

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_仿函数_04

//negate 一元仿函数 取反
	negate<int>n;
	cout<<n(50)<<" "<<n(-1)<<endl;//-50 1
//plus 二元仿函数 加法
	plus<int>p;
	cout << p(1, 5) << endl;//6

241 关系仿函数

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_i++_05

//关系仿函数 大于 greater
	vector<int>v;
	v.push_back(10);
	v.push_back(5);
	v.push_back(15);
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;//10 5 15

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

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

242 逻辑仿函数

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_开发语言_06

vector<bool>v;
	v.push_back(true);
	v.push_back(false);
	for (vector<bool>::iterator it = v.begin(); it != v.end(); it++)
		{
			cout << *it << " ";
		}
	cout << endl;//1 0
	//将v搬运到容器v2中并执行取反操作
	vector<bool>v2;
	v2.resize(v.size());//开辟空间
	transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
	for (vector<bool>::iterator it2 = v2.begin(); it2 != v2.end(); it2++)
	{
		cout << *it2 << " ";
	}
	cout << endl;//0 1

5 STL-常用算法

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_数据结构_07

遍历算法

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_i++_08

243 for_each

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_i++_09

void myPrint(int val)
{
	cout << 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);
	}

	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

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

244 transform

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_数据结构_10

/*
transform算法 将指定容器区间元素搬运到另一容器中
注意 : transform 不会给目标容器分配内存,所以需要我们提前分配好内存
*/
class MyTransform
{
public:
	int operator()(int val)
	{
		return val+100;//搬运时加100
	}
};

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>vTarget;
	//重新指定 vTarget大小
	vTarget.resize(v1.size());

	transform(v1.begin(), v1.end(), vTarget.begin(), MyTransform());

	for_each(vTarget.begin(), vTarget.end(), MyPrint());
	//100 101 102 103 104 105 106 107 108 109
}

查找算法

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_c++_11

245 find

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_开发语言_12

//查找有没有5这个元素
	vector<int>::iterator it = find(v1.begin(), v1.end(), 5);
	if (it != v1.end())
	{
		cout << "找到了元素: " << *it << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}

 //查找自定义数据类型(需要重载==号)

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

	bool operator==(const Person & p)//重载==号,让底层find知道如何对比person数据类型
	{
		return this->m_Name == p.m_Name && this->m_Age == p.m_Age;
	}

	string m_Name;
	int m_Age;
};
void test02()
{
	vector<Person>v;
	Person p1("aaa", 10);
	Person p2("bbb", 40);
	Person p3("ccc", 20);
	Person p4("ddd", 30);

	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(), p3);
	if (it != v.end())
	{
		cout << "找到了元素---姓名:  " << (*it).m_Name  << " 年龄: "<< it->m_Age << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
}

246 find_if

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_开发语言_13

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

class MyComparePerson :public binary_function<Person*, Person*, bool>
{
public:
	bool operator()(Person* p1, Person* p2) const
	{
		return (p1->m_Name == p2->m_Name && p1->m_Age == p2->m_Age);
	}
};

void test01()
{
	vector<Person*>v;
	Person p1("aaa", 10);
	Person p2("bbb", 40);
	Person p3("ccc", 20);
	Person p4("ddd", 30);

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

	Person* p = new Person("bbb", 40);

	vector<Person*>::iterator it = find_if(v.begin(), v.end(), bind2nd(MyComparePerson(), p));
	if (it != v.end())
	{
		cout << "找到了数据 姓名: " << (*it)->m_Name << " 年龄: " << (*it)->m_Age << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
}

247 adjacent_find

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_仿函数_14

248 binary_search

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_c++_15

249 count

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_c++_16

250 count_if

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_c++_17

排序算法

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_开发语言_18

251 sort 

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_数据结构_19

//从大到小
	sort(v1.begin(), v1.end(), greater<int>());

252 random_shuffle

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_数据结构_20

void test01()
{
	srand((unsigned int)time(NULL));
	vector<int>v1;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	//random_shuffle算法 对指定范围内的元素随机调整次序
	random_shuffle(v1.begin(), v1.end());

	for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; });
	cout << endl;
}

253 merge

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_仿函数_21

//merge算法 容器元素合并,并存储到另一容器中
//注意 : 两个容器必须是有序的
void test01()
{
	vector<int>v1;
	vector<int>v2;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 3);
	}
	vector<int>vTarget;//目标容器
	vTarget.resize(v1.size() + v2.size());
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
	for_each(vTarget.begin(), vTarget.end(), [](int val) {cout << val << " "; });
	//0 1 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 11 12
}

254 reverse

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_i++_22

//reverse算法 反转指定范围的元素
void test04()
{
	vector<int>v1;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	v1.push_back(3);

	reverse(v1.begin(), v1.end());
	for_each(v1.begin(), v1.end(), [](int val) {cout << val << " "; });
	cout << endl;
	//3 9 8 7 6 5 4 3 2 1 0
}

拷贝和替换算法

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_数据结构_23

255 copy

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_开发语言_24

vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	vector<int>vTarget;
	vTarget.resize(v1.size());

	//copy算法 将容器内指定范围的元素拷贝到另一容器中
	copy(v1.begin(), v1.end(), vTarget.begin());

	for_each(vTarget.begin(), vTarget.end(), [](int val){ cout << val << " "; });
	cout << endl;
	//0 1 2 3 4 5 6 7 8 9

256 replace

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_数据结构_25

257 replace_if

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_数据结构_26

//replace算法 将容器内指定范围的旧元素修改为新元素
//replace_if算法 将容器内指定范围满足条件的元素替换为新元素

class MyCompare
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
void test01()
{
	vector<int>v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}

	replace(v1.begin(), v1.end(), 3, 100);//0 1 2 100 4 5 6 7 8 9

	copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	//按条件 进行替换   将所有大于5  替换为 3000
	replace_if(v1.begin(), v1.end(), MyCompare(), 3000);//0 1 2 3000 4 5 3000 3000 3000 3000

	copy(v1.begin(), v1.end(), ostream_iterator<int>(cout, " "));
}

258 swap

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_i++_27

两个容器需要同类型 

算术生成算法

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_开发语言_28

259 accumulate

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_仿函数_29

260 fill

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_数据结构_30

集合算法

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_仿函数_31

261 set_intersection

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_数据结构_32

262 set_union

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_开发语言_33

263 set_difference

黑马程序员android移动开发基础教材代码 黑马程序员免费教程_仿函数_34