程序设计与算法(三)第十周 c++新特性和c++高级主题 (2)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/abc15766228491/article/details/82971406

无序容器(哈希表)

//哈希表插入和查询的时间复杂度几乎是常数
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
    unordered_map<string, int> turingWinner;
    turingWinner.insert(make_pair("Dijsktra", 1972));
    turingWinner.insert(make_pair("Scott", 1976));
    turingWinner.insert(make_pair("Wilkes", 1967));
    turingWinner.insert(make_pair("Hamming", 1968));
    turingWinner["Richie"]=1983;
    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;
}

正则表达式

//正则表达式
#include <iostream>
#include <regex>
using namespace std;
int main()
{
    // b开头跟0或1个字符再跟p再跟0个或多个相同字符再跟k
    regex reg("b.?p.*k");
    cout<<regex_match("bopggk",reg)<<endl;
    cout<<regex_match("boopggk", reg)<<endl;
    cout<<regex_match("b pk", reg)<<endl;
    // \\d是\d代表0-9{3}代表出现前面的东西3次,()代表项,[a-zA-Z]+代表字母出现一次或者若干次
    // 接上面。。。 .代表有任意一个字符,|代表或,\\s是空格,\1代表第一项,就是([a-zA-Z]+)了
    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;
    cout<<regex_match(incorrect, reg2)<<endl;

}
1
0
1
1
0

Lambda表达式

只使用一次的函数对象,能否不要专门为其编写一个类?

只调用一次的简单函数,能否在调用时才写出其函数体?

形式:

[外部变量访问方式说明符](形参表)-> 返回值类型
{
语句组
}
[=] 以传值的形式使用所有外部变量
[] 不使用任何外部变量
[&] 以引用形式使用所有外部变量
[x, &y] x 以传值形式使用,y以引用形式使用
[=, &x, &y] x, y以引用形式使用,其余变量以传值形式使用
[&, x, y] x, y以传值的形式使用,其余变量以引用形式使用
“->返回值类型”也可以没有,没有则编译器自动判断返回值类型。

//lambda表达
#include <iostream>
#include <algorithm>
using namespace std;
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, int m){
        cout<<"x is:"<<x<<endl;
        y++;z++;
        return n*n;
    };
    // 注意,这里如果是cout<<ff(15, 15)<<" "<<y<<","<<z<<endl;这样的化,就是200,300了
    cout<<ff(15, 15)<<endl;
    cout<<" "<<y<<","<<z<<endl;
    // lambda 应用
    cout<<"************"<<endl;
    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<<" ";});
}
3.7
x is:100
225
 201,301
************
11 2 33 4 
//lambda表达
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    vector<int> a{1,2,3,4};
    int total=0;
    // 这个数组每个元素求和,并且乘以2
    for_each(a.begin(), a.end(),[&](int &x){total+=x;x*=2;});
    cout<<total<<endl;
    for_each(a.begin(), a.end(), [](int x){cout<<x<<" ";});
    return 0;
}
10
2 4 6 8 
//lambda表达式递归求斐波那契数列
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main()
{
    // function<int(int)>表示一种类型,表示返回值为int,有一个int参数的函数
    // 下面表示对这个fib变量用lambda进行初始化,对于lambda来说,这个fib是外部变量,
    // 在这个lambda中,[&fib]表示要使用这个fib,而且是以引用的方式
    function<int(int)>fib=[&fib](int n)
    {return n<=2?1:fib(n-1)+fib(n-2);};
    cout<<fib(20)<<endl;
    return 0;
}
6765

猜你喜欢

转载自blog.csdn.net/abc15766228491/article/details/82971406