文章目录
一、lambda表达式
1. lambda引入
lambda表达式是C++11最重要也最常用的一个特性之一。其实在c#3.5中就引入了lambda,Java则至今还没引入,要等到Java 8中才有lambda表达式。
lambda来源于函数式编程的概念,也是现代编程语言的一个特点。C++11这次终于把lambda加进来了。
2.lambda表达式优点
lambcla表达式有如下优点:
-
声明式编程风格︰就地匿名定义目标函数或函数对象,不需要额外写一个命名函数或者函数对象。以更直接的方式去写程序,好的可读性和可维护性。
-
简洁︰不需要额外再写一个函数或者函数对象,避免了代码膨胀和功能分散,让开发者更加集中精力在手边的问题,同时也获取了更高的生产率。
-
在需要的时间和地点实现功能闭包,使程序更灵活。下面,先从lambda表达式的基本功能开始介绍它。
3.lambda表达式的概念和基本用法
lambda表达式定义了一个匿名函数,并且可以捕获一定范围内的变量。lambda表达式的语法形式可简单归纳如下:
[capture ] ( params ) opt -> ret { body; };
其中: capture是捕获列表;
params是参数表;
opt是函数选项;
ret是返回值类型;
body是函数体。
[capture](paarams)opt->ret
{
body;
};
其中: capture是捕获列表; params是参数表; opt是函数选项; ret是返回值类型; body是函数体。
因此,一个完整的lambda表达式看起来像这样:
auto f=[](int a) -> int
{
return a+1;
}
std::cout<<f(1)<<std::endl;
4. 仿函数
template<class _Arg,class _Result>
struct my_unary_function
{
typedef _Arg argument_type;
typedef _Result result_type;
};
template<class _Arg1,class _Arg2,class _Result>
struct my_binary_function
{
typedef _Arg1 first_argument_type;
typedef _Arg2 second_argument_type;
typedef _Result result_type;
};
template<class _Tp>
struct my_plus :public my_binary_function<_Tp, _Tp, _Tp>
{
_Tp operator()(const _Tp& _x, const _Tp& _y) const
{
return _x + _y;
}
};
int main()
{
my_plus<int> myi;
my_plus<double> myd;
int x = myi(12, 23);//仿函数
double dx = myd(12.23, 34.45);
}
5.类型萃取
template<class _Arg, class _Result>
struct my_unary_function
{
typedef _Arg argument_type;
typedef _Result result_type;
};
template<class _Arg1, class _Arg2, class _Result>
struct my_binary_function
{
typedef _Arg1 first_argument_type;
typedef _Arg2 second_argument_type;
typedef _Result result_type;
};
template<class _Tp>
struct my_plus :public my_binary_function<_Tp, _Tp, _Tp>
{
_Tp operator()(const _Tp& _x, const _Tp& _y) const
{
return _x + _y;
}
};
template<class _Fn>
void fun(_Fn& x)
{
cout << typeid(_Fn).name() << endl;
typename _Fn::first_argument_type first_type;
cout << typeid(first_type).name() << endl;
cout << typeid(_Fn::first_argument_type).name() << endl;
}
int main()
{
my_plus<int> myi;
my_plus<double> myd;
fun(myi);
fun(myd);
}
运行结果
6.排序
从小到大排序
int main()
{
list<int> ilist = { 1,4,2,0,9,5,6,7,3,2,8,10 };
ilist.sort();
for (auto& x : ilist)
{
cout << x << endl;
}
}
运行结果
从大到小排序
int main()
{
list<int> ilist = { 1,4,2,0,9,5,6,7,3,2,8,10 };
ilist.sort(greater<int>());
for (auto& x : ilist)
{
cout << x << endl;
}
}
运行结果
7.lmabda表达式捕获
lambda表达式可以通过捕获列表捕获一定范围内的变量
- [ ]不捕获任何变量。
- [&]捕获外部作用域中所有变量,并作为引用在函数体中使用(按引用捕获)。
- [=]捕获外部作用域中所有变量,并作为副本在函数体中使用(按值捕获)。
- [=,&foo]按值捕获外部作用域中所有变量,并按引用捕获foo变量。
- [bar]按值捕获bar变量,同时不捕获其他变量。
- [this]捕获当前类中的this指针,让lambda表达式拥有和当前类成员函数同样的访问权限。如果已经使用了&或者=,就默认添加此选项。捕获this的目的是可以在lamda中使用当前类的成员函数和成员变量。
int main()
{
int x = 0;
auto f1 = [&](int a)->int
{
x += 10;//定义时候进行捕获
cout << x << endl;
return x;
};
auto f2 = [=](int a)->int
{
return x;
};
x = 100;
cout << f1(0) << endl;
cout << f2(0) << endl;
return 0;
}
8.lambda表达式推导
//lambda表达式推导
int main()
{
auto f1 = [](int a) {return a; };
auto f2 = [](int a)->std::initializer_list<int> {return { a,1 }; };//列表方案推导
auto f3 = []() {return 1; };
}
二、map
1.map分类
- 唯一性map
- 多重map
底层实现是红黑树,无序map底层是哈希表结构。
2.成员类型
成员类型 | 定义 |
---|---|
key_type | key |
mapped_type | T |
value_type | std::pair<const key,T> |
size_type | 无符号整数类型(通常是std::size_t) |
difference_type | 有符号整数类型(通常是std::ptrdiff_t) |
key_compare | Compare |
allocator_type | Allocator |
reference | Allocator::reference(C++11前) value_type&(C++11起) |
const_reference | Allocator::const_reference(C++11前) const value_type&(C++11起) |
pointer | Allocator::pointer(C++11前) std::allocator_traits::pointer(C++11起) |
const_pointer | Allocator::const_pointer(C++11前) std::allocator_traits::const_pointer(C++11起) |
iterator | 遗留双向迭代器 |
const_iterator | 常双向迭代器 |
reverse_iterator | std::reverse_iterator |
const_reverse_iterator | std::reverse_iterator<const_iterator> |
node_type(c++17起) | 表示容器结点地结点把柄特化 |
3. 元素访问
at | 访问指定的元素,同时进行越界检查 |
---|---|
operator[] | 访问或插入指定的元素 |
4. 代码示例
#include<iostream>
#include<string> // C++字符串类型库
#include<string.h> //C字符函数库
#include<cstring> // C字符函数库
#include<vector>
#include<initializer_list>
#include<iomanip>
#include<limits.h>
#include<list>
#include<map>
#include<deque>
#include<queue>
using namespace std;
int main()
{
string stra[] = { "map","deque","queue","vector","list","map","vector","map" };
std::map<string, int> simap;
for (auto& x : stra)
{
simap[x] += 1;
}
std::map<string, int>::iterator it = simap.begin();
for (; it != simap.end(); ++ it)
{
cout << it->first << " ==> " << it->second << endl;
}
return 0;
}
5.typedef和using关键字的区别
template<class _key,class _val>
class map
{
typedef pair<const _key, _val>value_type;
using value_type = pair<const _key, _Ty>;//类型重命名,与上一行等价
typedef int* PINT;
using PINT = int*;//与上一行等价
};
6. 无序map和普通map
#include<iostream>
#include<string> // C++字符串类型库
#include<string.h> //C字符函数库
#include<cstring> // C字符函数库
#include<vector>
#include<initializer_list>
#include<iomanip>
#include<limits.h>
#include<list>
#include<map>
#include<deque>
#include<queue>
#include<unordered_map>
using namespace std;
int main()
{
string stra[] = { "map","deque","queue","vector","list","map","vector","map" };
std::map<string, int> simap;
std::unordered_map<string, int> unmap;//无序map
for (auto& x : stra)
{
simap[x] += 1;
unmap[x] += 1;
}
std::map<string, int>::iterator it = simap.begin();
for (; it != simap.end(); ++ it)
{
cout << it->first << " ==> " << it->second << endl;
}
std::unordered_map<string, int>::iterator uit = unmap.begin();
for (; uit != unmap.end(); ++uit)
{
cout << uit->first << " " << uit->second << endl;
}
return 0;
}
7. map的几种插入方式
int main()
{
typedef std::map<int, string> ANmap;
ANmap age_namemap;//类型重命名
int age;
string name;
while (cin >> age >> name, age >= 16 && name != "end")//年龄不允许重复
{
//age_namemap[age] = name;//第一种插入方式
age_namemap.insert(std::map<int, string>::value_type(age, name));//第二种插入方式:insert函数插入
age_namemap.insert(pair<const int, string>(age, name));//第三种插入方式
age_namemap.insert(ANmap::value_type(age, name));
}
for (auto& x : age_namemap)
{
cout << x.first << " --> " << x.second << endl;
}
return 0;
}
运行结果
8. 多重map和普通map
8.1 会产生覆盖
int main()
{
//多重map
std::multimap<int, string> amap;//允许关键码重复
std::map<int, string> bmap;//不允许关键码重复
bmap[16] = "a";
bmap[17] = "b";
bmap[16] = "c";
for (auto& x : bmap)
{
cout << x.first << " " << x.second << endl;
}
return 0;
}
运行结果
8.2 不会产生覆盖
insert返回键值对(bool为真,插入成功,bool值为假,插入失败)
std::pair<iterator,bool> insert(const value_type& value);
int main()
{
//多重map
std::multimap<int, string> amap;//允许关键码重复
std::map<int, string> bmap;//不允许关键码重复
bmap.insert(map<int, string>::value_type(16, "a"));
bmap.insert(map<int, string>::value_type(17, "b"));
bmap.insert(map<int, string>::value_type(16, "c"));
for (auto& x : bmap)
{
cout << x.first << " " << x.second << endl;
}
return 0;
}
运行结果
9. 查找
count | 返回匹配特定的元素数量 |
---|---|
find | 寻找带有特定键的元素 |
contains | 检查容器是否含有特定键的元素 |
equal_range | 返回匹配特定键的元素范围 |
lower_bound | 返回指向首个不小于给定键的元素的迭代器 |
upper_bound | 返回指向首个大于给定键的元素的迭代器 |
10.set函数
检查是否存在重复元素,但是不能够记录元素个数
代码示例
#include<iostream>
#include<set>
using namespace std;
int main()
{
std::set<int> iset;
int x;
while (cin >> x, x != -1)
{
if (iset.count(x))
{
cout << " 存在 " << x << endl;
}
else
{
iset.insert(x);
}
}
return 0;
}
运行结果
用map记录元素出现的次数
#include<set>
using namespace std;
int main()
{
map<int, int> iimap;
for (int i = 0; i < 10000; ++i)
{
int x = rand() % 10000;
iimap[x] += 1;
}
return 0;
}