C++11 的基本内容和Boost用法整理


2019.6.8

其他

#include<assert.h>
#include<iostream>
#include<functional>
using namespace std;
void learn1()
{
//自动类型推断
    auto shu = 123454;
    cout << "auto type is :" << shu << endl;
//空指针
    int* pot = nullptr;
    if (pot == NULL) cout << "point is null.";
    else cout << "nullptr is not NULL.";
//强类型枚举
    enum class options {none,one,two,all};
    options you = options::one;
    options me = options::all;
    if (you == me){
        cout << "\n咱俩一样。";
    }
    else{
        cout << "\n咱俩不一样哦。";
    }
//lambda 匿名函数
    auto lambda = [](int x, int y){return x + y; };
    cout << "\nlambda(3,4) = " << lambda(3, 4) << endl;
    function<int(int, int)> lambda1 = [](int x, int y){return x + y; };
    cout << "lambda1(3,4) = " << lambda1(3, 4) << endl;
//引用的和传值 效果
    int i = 3;
    int j = 4;
    function<int(void)> f = [i, &j]{return i + j; };
    i = 20, j = 50;
    cout << "f(i,&j) = " << f() << endl;  //53 
//初始化
    int n = [](int x, int y){return x + y; }(3,4);
    cout << "lambda exp n is : " << n << endl;
//nested 嵌套
    int twoplusthree = [](int x){return [](int y){return y * 2; }(x)+3; }(5);
    cout << "twoplusthree is: " << twoplusthree << endl; 
	//结果:13 5初始化的x,x初始化的y。y==5,返回5*2,在返回5*2+3.=13  传入的是括号里的值
    //for(;;[](){}) 正则表达式用在这里
//静态断言
    static const int size = 1;
    static_assert(size < 10, "size is wrong!");
//随机数
    //std::random_device rd;
}

//文件流非C++11
    string str1;
    ifstream foursign;
    foursign.open("foursign.txt");
    if (foursign.is_open()){
        cout << "四签名文件已打开。" << endl;
        getline(foursign, str1);
    }
    foursign.close();

    ofstream tmp("tmplog.txt");
    tmp.write(sentence);
    tmp.close();
    std::vector<string> sentence;


//函数对象  class 换为struct不行
    class funcobj
    {
    public:
        bool operator()() const
        {
            cout << "函数对象调用成功。\n"<<endl;
            return true;
        }
    };
    funcobj jonson;
    jonson();

Boost

Boris Schäling(2008-2010)


如何使用boost库 -L -static: 包含lib下的文件 -I /home/xzz/boost_1_56_0/include :包含头文件



Predicates(谓词)