C++STL迭代器

1.迭代器 的认知

迭代器: 就是一个类中类,通过运算符重载通过类中类的对象去遍历容器

迭代器的分类 : 正向迭代器: iterator                     begin();             end();

                           反向迭代器: reverse_iterator       rbegin();            rend();

                           常正向迭代器: const_iterator       cbegin();           cend();

                           常反向迭代器:const_reverse_iterator       crbegin();        crend();

按功能分:正向:只能++

                  双向:能++能--

                  随机访问迭代器:访问哪里都可以

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

int main()
{
	vector<int>data = { 1,2,3,6,5,4 };
	vector<int>::iterator iter;
	for (iter = data.begin(); iter != data.end(); iter++)
	{
		cout << *iter << endl;//对象模仿指针*iter取数据
	}//正向迭代器
	auto riter = data.rbegin();
	for (; riter != data.rend(); riter++)
	{
		cout << *riter << endl;
	}//反向迭代器
	vector<int>::const_iterator c_iter;
	for (c_iter = data.begin(); c_iter != data.end(); c_iter++)
	{
		cout << *c_iter << endl;//对象模仿指针*iter取数据
	}//常正向迭代器
    return 0;
}

 容器中迭代器的分类

容器名迭代器类型
array随机访问
vector随机访问
deque随机访问
stack/queue/priority_queue(特定顺序存取不支持
list双向
set/multiset双向
map/multimap双向

迭代器辅助函数:

     advance(iterator iter,n);//移动

     distance(iterator begin,iterator end);//两个迭代器间的间距

     iter swap(iterato first,iterator end);//交换

拓展内容:特殊迭代器 流型迭代器-->一般用在辅助打印

输出流型

 ostream_iterator<_Ty> iter(ostream& out);

 ostream_iterator<_Ty> iter(ostream& out,char* str);  输出流型迭代做赋值运算,意味着就是打印数据到屏幕上

 输入流型

 istream_iterator<_Ty> iter; //构造无参对象,是一个错误流 end_of_ostream                 istream_iterator<_Ty> iter(istream& in); + *iter 等效cin>>操作

2.  Lambda表达式

         Lambda: 就是一个返回函数指针的表达式,它定义和返回值函数指针在一起的

         Lambda表达式的组成部分

         c++ // [捕获方式](函数参数)mutable exception->函数返回值类型{函数体;}

int Max(int a,int b)

{ return a>b?a:b; }

void print()

{ //Max的Lambda表达式写法 int(*pMax)(int,int)=[](int a,int b)mutable noexcept->int{ return a>b?a:b;};

/省略写法: 中间描述此都可以省略 auto ppMax=[](int a,int b){reutrn a>b?a:b;};

//捕获方式-->函数使用外部的变量的方式

[=] //值的方式捕获

[&] //引用的方式捕获

[this]//this指针方式捕获

[] //不捕获任何变量

[=,&x];//x用引用方式捕获,其他变量用值的方式捕获

#include <vector>
using namespace std;
int main()
{
	int(*pMax)(int, int) = nullptr;
	//Lambad表达式
	pMax = [](int a, int b)mutable noexcept->int {return a > b ? a : b; };
	cout << pMax(8, 9) << endl;//结果为9
	return 0;
}

3 仿函数

类模仿函数的行为,实质是无名对象调用重载的函数

仿函数做排序准则,或算法的计算准则

关键点:重载()

标准库中的仿函数

#include <iostream>
#include <vector>
#include <string>
#include <functional>//仿函数的头文件
#include <map>
using namespace std;

class Sum
{
public:
	int operator()(int a, int b)const
	{
		return a + b;
	}
};
int main()
{
	Sum s;
	cout << "显示调用" << s.operator()(8, 8) << endl;//显示调用成员函数
	cout << "隐式调用" << s(9, 9) << endl;
	cout << "无名对象调用" << Sum()(8, 9) << endl;
	cout << "无名对象调用" << Sum{}(8, 9) << endl;
	//标准库中的仿函数
	//算数
	cout << plus<int>{}(9, 9) << endl;//9+9=18
	//关系
	cout << equal_to<int>{}(8, 9) << endl;
	map<int, less<int>> map1;
	map<int, greater<int>> map2;
	//逻辑
	cout << logical_and<int>{}(5, 4);
	return 0;
}

4    函数适配器

用来绑定函数调用时的参数,让其适应其他调用的用法

int Max(int a,int b)
{
	return a < b ? a : b;
}
void print(int(*pMax)(int,int), int a,int b)
{
	cout << pMax(a, b) << endl;
}

int main()
{
	cout << Max(8, 9) << endl;
	//基本用法
	auto pMax = bind(Max, std::placeholders::_1, 100);//把第二个参数置为100
	cout << pMax(99) << endl;//99和100比
	return 0;
}

5   函数包装器

函数包装器是什么?就是把函数指针包装成一个对象。通过这个对象调用函数 

一旦函数指针被函数包装器包装了,那这个包装器对象可以直接替

#include <iostream>
#include <string>
#include <functional>
using namespace std;
int Max(int a, int b) 
{
	cout << "包装普通函数:" << endl;
	return a > b ? a : b;
}
class MM 
{
public:
	void print(int a)    //void print(int a, MM* mm);
	{
		cout << "包装成员函数" << endl;
	}
	static void printStatic() 
	{
		cout << "包装静态的函数" << endl;
	}
protected:
};
void testMMFunc() 
{
	MM mm;
	function<void(int)> func(bind(&MM::print,&mm,std::placeholders::_1));
	func(12);
}
class Test 
{
public:
	void operator()(string str) 
	{
		cout << str << endl;
	}
};
void printData(int a, MM mm, string str) 
{
	cout << "bind和function" << endl;
}
void TestFuncBind() 
{
	function<void(string, int, MM)> pf = bind(printData,
		std::placeholders::_2, std::placeholders::_3, std::placeholders::_1 );
	pf("string", 1, MM());
	//3  1  2
}

int main() 
{
	function<int(int, int)> funcMax(Max);
	cout << funcMax(1, 3) << endl;

	function<int(int, int)> funcMax2=Max;
	function<void()> funcS(MM::printStatic);
	funcS();

	//仿函数包装
	Test test;
	function<void(string)> func = test;
	func("包装仿函数");
	TestFuncBind();
	//包装成员函数
	testMMFunc();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值