1.迭代器:类中类
正向迭代器
只能正向不能反向
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
vector<int>::iterator it;//正向迭代器
for (it = v.begin(); it != v.end();it++)
{
cout << *it << " ";
}
}
常正向迭代器
常量
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
vector<int>::const_iterator it;//常正向迭代器
for (it = v.cbegin(); it != v.cend();it++)
{
cout << *it << " ";
}
}
反向迭代器
只能反向遍历
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
vector<int>::reverse_iterator it;//反向迭代器
for (it = v.rbegin(); it != v.rend();it++)
{
cout << *it << " ";
}
}
常反向迭代器
常量
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
vector<int>::const_reverse_iterator it;//常反向迭代器
for (it = v.crbegin(); it != v.crend();it++)
{
cout << *it << " ";
}
}
随机访问迭代器(访问任意一个元素)array vector deque......
双向迭代器(可以++或--)list map set......
无迭代器 queue priority_queue queue
迭代器中的辅助函数
-
移动:advance(iterator iter,n);
-
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { vector<int> v; v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); vector<int>::iterator it;//正向迭代器 it = v.begin(); advance(it, 2);//把迭代器it移动2个位置 for (; it != v.end();it++) { cout << *it << " "; } }
-
间距:distance(iterator begin,iterator end);
-
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { vector<int> v; v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); vector<int>::iterator it; cout<<distance(v.begin(), v.end());//测距 /* for (it = v.begin(); it != v.end();it++) { cout << *it << " "; }*/ }
-
交换:iter_swap(iterato first,iterator end);
-
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { vector<int> v; vector<int> v1; v.push_back(10); v.push_back(20); v.push_back(30); v.push_back(40); v1.push_back(20); v1.push_back(210); v1.push_back(40); v1.push_back(10); vector<int>::iterator it1=v1.begin(); vector<int>::iterator it = v.begin(); iter_swap(it,it1); for (it = v.begin(); it != v.end();it++) { cout << *it << " "; } }
注:begin() 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表达式
-
返回值:函数指针
-
完整格式:[捕获方式](函数参数)mutable exception->函数返回值类型{函数体;}
-
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { auto p=[=](int a)mutable noexcept->void {cout << a; }; p(1); }
-
缩写格式:[](){},没有参数时可以写成[]{}
-
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { auto p=[=](int a) {cout << a; }; p(1); }
捕获方式:
-
=赋值:不改变实参
-
&引用:改变实参
-
this
-
[]不捕获
-
[=,&x] x用&,其他用=
-
......
-
3.仿函数:重载()
-
1.自己写仿函数
-
1.隐式调用如下
-
#include<iostream> #include<vector> using namespace std; class pt { public: void operator()() { cout << "仿函数"; } }; int main() { pt p; p(); }
2.显式调用如下
-
#include<iostream> #include<vector> using namespace std; class pt { public: void operator()() { cout << "仿函数"; } }; int main() { pt p; p.operator()(); }
3.直接用类名调用如下
-
#include<iostream> #include<vector> using namespace std; class pt { public: void operator()() { cout << "仿函数"; } }; int main() { pt{}(); }
这个大括号是为了与匿名对象分开,可以改成小括号,如下
-
#include<iostream> #include<vector> using namespace std; class pt { public: void operator()() { cout << "仿函数"; } }; int main() { pt()(); }
标准库中的仿函数
-
头文件:#include<functional>
-
内容:
-
plus(加法运算)
-
#include<iostream> #include<functional> #include<vector> using namespace std; int main() { cout<<plus<int>{}(1, 2); }
-
关系
-
#include<iostream> #include<functional> #include<vector> using namespace std; int main() { cout<< equal_to<int>{}(1, 2); }
逻辑&
-
#include<iostream> #include<functional> #include<vector> using namespace std; int main() { cout<< logical_and<int>{}(1, 2); }
......(其他省略)
-
4.函数适配器
-
函数名:bind
-
#include <functional> using namespace std; void pt(int a, int b, int c) { cout << a << " " << b << " " << c << endl; } int main() { auto p=bind(pt,std::placeholders::_1, std::placeholders::_2,10); p(10, 20); }
注意:
-
bind的返回值是个函数指针
-
std::placeholders::_1指的是原来的第1个参数放在此处
-
5.函数包装器
-
function<函数返回值类型(参数类型)>
-
#include<iostream> #include<vector> #include <algorithm> #include <functional> using namespace std; void pt(int a, int b, int c) { cout << a << " " << b << " " << c << endl; } int main() { function<void(int, int, int)> f1 = pt; f1(10, 20, 30); }
f1是包装器的名字,等号右边是函数名
-
函数包装器可以配合函数适配器使用
-