第十周 C++11新特性和C++高级主题
1.C++11新特性(一)
2.C++11新特性(二)
3.强制类型转换
4.异常处理
2.C++11新特性(二)
无序容器(哈希表)
用法和功能和map是一样的,区别是哈希表的时间效率更高。map的插入和查询时间复杂度是log(N),而哈希表插入和查询的时间复杂度几乎是常数。代价是内存空间需要更大。
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<string,int> turingWinner; //图灵奖获奖名单
turingWinner.insert(make_pair("Dijkstra",1972));
turingWinner.insert(make_pair("Scott",1976));
turingWinner.insert(make_pair("Wilkes",1967));
turingWinner.insert(make_pair("Hamming",1968));
turingWinner["Ritchie"] = 1983;//如果有元素的key是Ritchie,就修改他的第二个变量,如果没有,就插入这样的一个新的元素。
string name;
cin >> name; //输入姓名
unordered_map<string,int>::iterator p = turingWinner.find(name);
//据姓名查获奖时间
if( p != turingWinner.end())
cout << p->second;
else
cout << "Not Found" << endl;
return 0;
}
正则表达式
正则表达式就是一个字符串,这个字符串描述了一种模式。比如"b.?p.k":字符’b’开头,后面的’.‘可以是一个任意字符,’?‘表明前面的’.‘字符是0次或者1次,也就是’b’和’p’之间可以有一个任意字符也可以没有字符,接下来的’.‘也是任意字符,’'表明’p’和结尾的’k’字符之间’p’可以重复0次或任意多次。细节其余规则还有很多需要自己去了解。
#include <iostream>
#include <regex> //使用正则表达式须包含此文件
using namespace std;
int main()
{
regex reg("b.?p.*k");
cout << regex_match("bopggk",reg) <<endl; //输出 1, 表示匹配成功
cout << regex_match("boopgggk",reg) <<endl; //输出 0, 匹配失败
cout << regex_match("b pk",reg) <<endl ; //输出 1, 表示匹配成功
regex reg2("\\d{3}([a-zA-Z]+).(\\d{2}|N/A)\\s\\1");
string correct="123Hello N/A Hello";
string incorrect="123Hello 12 hello";
cout << regex_match(correct,reg2) <<endl; //输出 1,匹配成功
cout << regex_match(incorrect,reg2) << endl; //输出 0, 失败
}
Lambda表达式
只使用一次的函数对象,能否不要专门为其编写一个类?
只调用一次的简单函数,能否在调用时才写出其函数体?
Lambda表达式本质上就是函数,形式:
[外部变量访问方式说明符](参数表) ->返回值类型
{
语句组 //和函数是一样的
}
[] 不使用任何外部变量
[=] 以传值的形式使用所有外部变量,不允许改变外部变量的值
[&] 以引用形式使用所有外部变量,允许修改变量的值
[x, &y] x 以传值形式使用,y 以引用形式使用
[=,&x,&y] x,y 以引用形式使用,其余变量以传值形式使用
[&,x,y] x,y 以传值的形式使用,其余变量以引用形式使用
“->返回值类型”也可以没有, 没有则编译器自动根据return语句判断返回值类型。
int main()
{
int x = 100,y=200,z=300;
cout << [ ](double a,double b) { return a + b; } (1.2,2.5)<< endl;
auto ff = [=,&y,&z](int n) {
cout <<x << endl;
y++; z++;
return n*n;
};//这里只是定义,还没有调用,ff(n)才会将n传进去并调用
cout << ff(15) << endl;
cout << y << "," << z << endl;
}
输出:
3.7
100
225
201,301
int a[4] = { 4,2,11,33};
sort(a,a+4,[ ](int x,int y)->bool { return x%10< y%10; });
for_each(a,a+4,[ ](int x) {cout << x << " " ;} ) ;
输出:
11 2 33 4
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> a { 1,2,3,4};
int total = 0;
for_each(a.begin(),a.end(),[&](int & x){total += x; x*=2;});
cout << total << endl; //输出 10
for_each(a.begin(),a.end(),[ ](int x){ cout << x << " ";});
return 0;
}
程序输出结果:
10
2 4 6 8
Lambda表达式还可以实现递归的功能。
实现递归求斐波那契数列第n项:
function<int(int)> fib = [&fib](int n)
{
return n <= 2 ? 1 : fib(n-1) + fib(n-2);
};
cout << fib(5) << endl; //输出5
function<int(int)> 表示返回值为 int, 有一个int参数的函数
多线程
#include <iostream>
#include <thread>
using namespace std;
struct MyThread {
void operator () () {
while(true)
cout << "IN MYTHREAD\n";
}
};
void my_thread(int x)
{
while(x)
cout << "in my_thread\n";
}
int main()
{
MyThread x; // 对x 的要求:可复制
thread th(x);// 创建线程并执行
thread th1(my_thread, 100);
while(true)
cout << "in main\n";
return 0;
}
输出:
in my_thread
in my_thread
IN MYTHREAD
IN MYTHREAD
in main
in my_thread
IN MYTHREAD
IN MYTHREAD
in main
in main
in my_thread
IN MYTHREAD
……