C++STL迭代器

本文介绍了C++STL中的迭代器,包括正向、反向、常正向和常反向迭代器的分类及功能,以及辅助函数如advance、distance和iter_swap。此外,还探讨了Lambda表达式、仿函数、函数适配器和函数包装器在C++编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

迭代器

1.迭代器:类中类,通过运算符的重载,用类中类的对象遍历容器

2.迭代器分类:

(1)正向迭代器:iterator(begin(); end();)

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

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

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

3.按功能分类:

(1)正向迭代器

(2)双向迭代器

(3)随机访问迭代器

4.迭代器辅助函数

(1)移动:advance(iterator iter,n);

(2)间距:distance(iterator begin(),iterator end());

(3)交换:iter_swap(iterator first,iterator end()-1);

5.流型迭代器

(1)输出流:

ostream_iterator<_Ty> iter(ostream& out);

ostream_iterator<_Ty> iter(ostream& out,char* str);

(2)输入流:

istream_iterator<_Ty> iter;//构造无参对象,是一个错误流

istream_iterator<_Ty> iter(istream& in);

#include <iostream>
#include <vector>
using namespace std;
int main()
{
	vector<int> vecdata = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	//正向
	vector<int>::iterator iter;
	for (iter = vecdata.begin(); iter != vecdata.end(); iter++)
	{
		cout << *iter << "\t";
	}
	//反向
	vector<int>::reverse_iterator riter = vecdata.rbegin();
	for (; riter != vecdata.rend(); riter++)
	{
		cout << *riter << "\t";
	}
	//常正向
	vector<int>::const_iterator citer = vecdata.cbegin();
	for (; citer != vecdata.cend(); riter++)
	{
		cout << *citer << "\t";
	}
	//常反向
	vector<int>::const_reverse_iterator criter = vecdata.crbegin();
	for (; criter != vecdata.crend(); riter++)
	{
		cout << *criter << "\t";
	}

	vector<int> vec = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	auto iter = vec.begin();
    //移动
	advance(iter, 3);
	cout << *iter << endl;//4
    //间距
	cout << distance(vec.begin(), vec.end()) << endl;//10
    //交换
	iter_swap(vec.begin(), vec.end()-1);
	for (auto v : vec)
	{
		cout << v << "\t";
	}
	return 0;
}

Lambda表达式

1.Lambda表达式就是返回函数指针的表达式

//[捕获方式](函数参数)mutable exception->函数返回值类型{函数体}
int Max(int a,int b)
{
   return a>b?a:b;
}
//等效
int(*pmax)(int,int)=[](int a,int b)mutable noexcept->int{return a>b?a:b;}
//省略
auto ppmax=[](int a,int b){return a>b?a:b;}
//捕获
/*
[]  不捕获
[=]  值的方式捕获
[&]  引用的方式捕获
[this]  this指针方式捕获
[=,&x]  x用引用的方式捕获,其余用值的方式捕获
*/
#include <iostream>
#include <string>
using namespace std;
void print(int(*p)(int ,int ),int a,int b)
{
	cout << p(a, b) << endl;
}
int main()
{
	int(*pmax)(int, int) = nullptr;
	pmax = [](int a, int b)mutable ->int{return a > b ? a : b; };
	auto ppmax = [](int a, int b){return a > b ? a : b; };
	cout << ppmax(3, 4) << endl;
	cout << [](int a, int b){return a > b ? a : b; }(4, 5) << endl;
	print([](int a, int b){return a > b ? a : b; }, 1, 2);
	print([](int a, int b){return a +b; }, 1, 2);
	print([](int a, int b){return a*b; }, 1, 2);
	//捕获方式
	//1.用值的捕获,在lambda中不能把值当作左值使用,函数调用不会因为值的改变而改变
	//2.用引用的方式捕获,在lambda中能把值当作左值使用,函数调用会因为值的改变而改变
	int data = 111111;
	auto pprint = [=]{cout << data << endl; };
	auto pprint = [&]{cout << data << endl; };
	return 0;
}
//3.用this指针的方式捕获
class boy
{
public:
	void print()
	{
		[this]{cout << name << "\t" << age << endl; };
	}
protected:
	string name="abc";
	int age=11;
};

仿函数

1.类模仿函数行为,实质无名对象调用函数过程

2.一般做排序准则,或算法的计算准则

3.标准库的仿函数

(1)算术类

(2)关系类

(3)逻辑类

(4)选择,正同,投射

#include <iostream>
#include <functional>
#include <string>
using namespace std;
class Sum
{
public:
	int operator()(int a, int b)const
	{
		return a + b;
	}
protected:
};
int main()
{
	//重载()的调用
	Sum s;
	cout << "显示" << s.operator()(1, 2) << endl;
	cout << "隐式" << s(1, 2) << endl;
	cout << Sum()(1, 2) << endl;//用(){}标识
	//算术
	cout << plus<int>()(1, 2) << endl;
	//关系
	cout <<boolalpha<< equal_to<int>()(1, 2) << endl;
	//逻辑
	cout <<boolalpha<< logical_and<int>()(1, 0) << endl;//逻辑与
	return 0;
}

函数适配器

1.函数适配器:用来绑定函数调用时候的参数,让函数适合其他调用法

#include <iostream>
#include <string>
#include <functional>
#include <vector>
#include <algorithm>
using namespace std;
int Max(int a, int b)
{
	return a > b ? a : b;
}
class Test
{
public:
	void print(int a, int b, int c)
	{
		cout << a << " " << b << " " << c << endl;
	}
protected:
};
void printdata(int a, Test test, string str)
{
	cout << "非正常调用" << endl;
}
int main()
{
	//基本用法
	auto pmax = bind(Max, std::placeholders::_1, 100);//绑定第二个函数为100
	cout << pmax(1,2) << endl;//100
	vector<int> vec = { 11, 22, 33, 44, 55, 66, 77, 88, 99 };
	//条件统计
	cout << count_if(vec.begin(), vec.end(),bind(greater<int>(),std::placeholders::_1,60) ) << endl;//4
	cout<<count_if(vec.begin(), vec.end(), [](int a){return a > 60; })<<endl;//4
	//类中函数
	Test test;
	auto testfunc = bind(&Test::print, &test, std::placeholders::_1, std::placeholders::_2, 99);
	testfunc(1,2);
	//非正常调用
	auto testf = bind(printdata, std::placeholders::_3, std::placeholders::_1, std::placeholders::_2);//实参去对应形参的占位符的数字位
	testf( test, "love",1);
	return 0;
}

函数包装器

1.把函数指针包装成一个对象,通过这个对象调用函数。

2.函数包装器:function<函数返回值类型(参数类型)>

#include <iostream>
#include <functional>
#include <string>
using namespace std;
int Max(int a, int b)
{
	cout << "包装普通函数" << endl;
	return a > b ? a : b;
}
class cl
{
public:
	void print1()
	{
		cout << "包装成员函数" << endl;
	}
	static void print()
	{
		cout << "包装静态成员函数" << endl;
	}
protected:
};
class funcclass
{
public:
	void operator()(string str)
	{
		cout << str << endl;
	}
};
void printdata(int a, funcclass test, string str)
{
	cout << "bind和function" << endl;
}
int main()
{
	function<int(int, int)> funcmax(Max);
	function<int(int, int)> funcmax1=Max;
	cout << funcmax(1, 2) << endl;
	//静态成员函数
	function<void()> func(cl::print);
	//成员函数
	cl a;
	function<void()> func1(bind(&cl::print1, &a));
	//仿函数
	funcclass test;
	function<void(string)> funtest = test;
	funtest("仿函数");
	//bind和function
	function<void(int, funcclass, string)> bp = bind(printdata, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
	bp(1, test, "bind");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值