C++day11STL基础知识

1.函数对象和谓词

class Show
{
public:
	Show()
	{
		count = 0;
	}
	void operator()(int a)
	{
		cout << a << " ";
		count++;
	}

	void show ()
	{
		cout << "元素个数为 : " << count << endl;
	}
private:
	int count;
};

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


template <typename T>
void printT(T a)
{
	cout << a << " ";
}

// 一元函数对象
void func1()
{
	Show s;  // s 是一个对象
	s(10);   // 可以当做函数来使用   =====> 函数对象  仿函数
	vector<int>  v;
	for (int i = 0; i < 10; i++)
		v.push_back(i+1);

	// 遍历算法
	// 第一个参数:要遍历的数据的起始位置
	// 第二个参数:要遍历的数据的结束位置
	// 第三个参数:函数对象,要对数据做些什么事情
	// 运行过程:从开始到结束 一个一个取出元素,给第三个参数调用
	for_each(v.begin(), v.end(), Show());    // 函数对象
	cout << endl;
	//for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	//	s(*it);


	for_each(v.begin(), v.end(), print);     // 普通函数
	cout << endl;

	for_each(v.begin(), v.end(), printT<int>);
	cout << endl;

	// 函数和函数对象有什么区别,函数对象可以携带一些其他的属性
	// 算法函数对象的传递是值传递,要想获取它的值,可以通过接收算法函数的返回值来获取
	Show s1;
	s1 = for_each(v.begin(), v.end(), s1);
	cout << endl;
	s1.show();
}

bool greater3(int a)
{
	return a > 3;
}

class Div
{
public:
	bool operator()(int a)
	{
		return (a%3==0);
	}
};

class Div3
{
public:
	void operator()(int a)
	{
		if (a%3==0)
			v.push_back(a);
	}

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

// 一元谓词
void func2()
{
	vector<int>  v;
	v.push_back(1);
	v.push_back(8);
	v.push_back(3);
	v.push_back(9);
	v.push_back(6);
	v.push_back(7);

	// 如果存在,返回找到的那个元素的迭代器
	// 如果不存在,返回 end()
	vector<int>::iterator it = find(v.begin(), v.end(), 6);
	if (it != v.end())
		cout << "找到元素: " << *it << endl;
	else
		cout << "没有找到元素" << endl;

	// 查找大于3的数
	// 有条件的查找、设置查找条件
	// 返回第一个符合要求的元素的迭代器
	it = find_if(v.begin(), v.end(), greater3);
	if (it != v.end())
		cout << "找到第一个大于3的元素: " << *it << endl;
	else
		cout << "没有找到元素" << endl;


	// 查找能被3整除的数
	it = find_if (v.begin(), v.end(), Div());
	if (it != v.end())
		cout << "找到第一个能被3整除的元素: " << *it << endl;
	else
		cout << "没有找到元素" << endl;

	// 找出所有能被3整除的元素
	Div3 v3 = for_each(v.begin(), v.end(), Div3());
	v3.show();
}


int add(int a, int b)
{
	return a+b;
}

// 二元函数对象
void func3()
{
	vector<int>  v1;
	v1.push_back(1);
	v1.push_back(8);
	v1.push_back(3);

	vector<int> v2;
	v2.push_back(9);
	v2.push_back(6);
	v2.push_back(7);

	vector<int> v3(3);
	// v3[i] = add(v1[i], v2[i])
	transform(v1.begin(), v1.end(), v2.begin(), v3.begin(), add);	
	for_each(v3.begin(), v3.end(), print);
	cout << endl;

}

bool mySort(int left, int right)
{
	// return (left < right);
	return (left > right);
}

// 二元谓词
void func4()
{
	vector<int>  v;
	v.push_back(1);
	v.push_back(8);
	v.push_back(3);
	v.push_back(9);
	v.push_back(6);
	v.push_back(7);

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

	sort(v.begin(), v.end(), mySort);
	for_each(v.begin(), v.end(), print);
	cout << endl;

	sort(v.begin(), v.end(), less<int>());
	for_each(v.begin(), v.end(), print);
	cout << endl;

	sort(v.begin(), v.end(), greater<int>());
	for_each(v.begin(), v.end(), print);
	cout << endl;
}

2.预定义函数对象和函数配适器

#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
#include <fstream>
#include <time.h>

using namespace std;


bool greater5(int a)
{
	return a > 5;
}

void func1()
{
	plus<int>  add; // 定义一个函数对象
	cout << add(1,2) << endl;

	greater<int>  g;
	cout << g(1, 2) << endl;

	less<int>  l;
	cout << l(1, 2) << endl;

	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(9);
	v.push_back(11);
	v.push_back(5);
	v.push_back(9);
	v.push_back(7);

	int num9 = count(v.begin(), v.end(), 9);
	cout << num9 << endl;

	// 统计大于5的个数
	int g5 = count_if(v.begin(), v.end(), greater5);
	cout << g5 << endl;


	// 函数适配器:将一个类型不匹配的函数对象通过某种方式 编程类型匹配的函数对象
	// 绑定器:通过将一个参数绑定二元函数对象的某一个参数上,让其变成一元函数对象
	//bind1st(op, value): op 是函数U对象,value是值,将 value 固定到 op 的第一个参数上
	//bind2nd(op, value): op 是函数U对象,value是值,将 value 固定到 op 的第二个参数上
	int g3 = count_if(v.begin(), v.end(), bind2nd(greater<int>(), 3));
	cout << g3<< endl;
}

// 取反器
void func2()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(9);
	v.push_back(11);
	v.push_back(5);
	v.push_back(9);
	v.push_back(7);

	// 能被2整除的元素个数
	int div2 = count_if(v.begin(), v.end(), not1(bind2nd(modulus<int>(), 2)));
	cout << "能被2 整除的元素个数: " << div2 << endl;
}

void func3()
{
	vector<string> v;
	ifstream fin("aa.txt");
	char str[100];
	while (fin.getline(str, 100))
	{
		v.push_back(str);
	}

	srand((unsigned int)time(NULL));

	// 随机排序
	random_shuffle(v.begin(), v.end());

	for (unsigned int i = 0; i < v.size(); i++)
	{
		if (i % 4 == 0)
			printf("\n");
		printf ("%-10s", v[i].c_str());
	}

	printf("\n");
}


int main()
{
	// func1();
	// func2();
	func3();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值