c++11 boost - 工具类

2018-07-11 11:00:48

noncopyable 允许程序实现不能拷贝的类,直接继承它即可。

我们平常也可以自己实现,只是比较繁琐。

#include <boost/noncopyable.hpp>
#私有化拷贝构造和拷贝赋值
class tmp{
private:
    tmp(const &tmp);
    const tmp& operator=(const tmp &);
}
#或者加上delete关键字
class tmp{
public:
    tmp(const &tmp) = delete;
    const tmp& operator=(const tmp &) = delete;
}

default 关键字代表使用默认构造。

#include <iostream>
using namespace std;

#include <boost/noncopyable.hpp>

class do_not_copy: boost::noncopyable{};
int main()
{
    do_not_copy d1;
    //do_not_copy d2(d1);
    //do_not_copy d3;
    //d3 = d1;
}


optional 库使用 容器 语义,包装了可能产生无效值的对象,实现了 未初始化概念。

#include <boost/optional.hpp>
using namespace boost;
// Copyright (c) 2015
// Author: Chrono Law
#include <type_traits>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;


#include <boost/optional.hpp>
using namespace boost;

void case1()
{
    cout << typeid(none).name() << endl;
    
    //是否是指针指向非静态数据成员
    cout << std::is_member_object_pointer<none_t>::value << endl;
}

void case2()
{
	//2个未初始化对象
    optional<int> op0;
    optional<int> op1(none);

    assert(!op0);
    assert(op0 == op1);
    
    //已设置,返回设置的值
    //未设置,返回253
    assert(op1.value_or(253) == 253);
	
	//已设置,返回设置的值
    //未设置,返回函数返回值,下面是lambda表达式定义的函数对象
    cout << op1.value_or_eval(
            [](){return 874;}) << endl;

	//初始化
    optional<string> ops("test");
    cout << *ops << endl;

    ops.emplace("monado", 3);   //没有拷贝代价
    assert(*ops == "mon");      

    vector<int> v(10);
    optional<vector<int>& > opv(v);
    assert(opv);

    opv->push_back(5);
    assert(opv->size() == 11);

    opv = none;
    assert(!opv);
}

optional<double> calc(int x)
{
    return optional<double>( x != 0, 1.0 / x);
}

optional<double> sqrt_op(double x)
{
    return optional<double>(x > 0, sqrt(x));
}

void case3()
{
    optional<double> d = calc(10);

    if (d)
    {
        cout << *d << endl;
        cout << d.value() << endl;
    }

    d = sqrt_op(-10);
    if (!d)
    {   cout << "no result"<< endl; }
}

//工厂函数
void case4()
{
    auto x = make_optional(5);
    assert(*x == 5);
	
    auto y = make_optional<double>((*x > 10), 1.0);
    assert(!y);
}


int main()
{
    case1();
    case2();
    case3();
    case4();
}
[root@192 c++]# g++ -std=c++11 main.cpp 
[root@192 c++]# ./a.out 
N5boost6none_tE
0
874
test
0.1
0.1
no result


assign 库用来简化容器赋值。

list_inserter 类是用来操作容器的工具类。

// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>
using namespace std;

#include <boost/assign.hpp>
//using namespace boost::assign;

//重载了+=和,
void case1()
{
    using namespace boost::assign;
    vector<int> v;
    v += 1,2,3,4,5, 6*6;

    for(auto& x : v)
        cout << x << ",";
    cout << endl;

    set<string> s;
    s += "c", "cpp", "lua", "swift";

    for(auto& x : s)
        cout << x << ",";
    cout << endl;

    map<int, string> m;
    m += make_pair(1, "one"),make_pair(2, "two");

}

//下面是一些工厂函数
#include <forward_list>
void case2()
{
    using namespace boost::assign;

    vector<int> v;
    push_back(v)(1)(2)(3)(4)(5);

    list<string> l;
    push_front(l)("c")("cpp")("lua")("swift");

    forward_list<string> fl;
    push_front(l)("matrix")("reload");

    set<double> s;
    insert(s)(3.14)(0.618)(1.732);

    map<int, string> m;
    insert(m)(1, "one")(2, "two");
}


void case3()
{
    using namespace boost::assign;

    vector<int> v;
    push_back(v),1,2,3,4,5;
    push_back(v)(6),7,64 / 8,(9),10;

    for(auto& x : v)
        cout << x << ",";
    cout << endl;

    deque<string> d;
    push_front(d)() = "breath","of","the","wild";
    assert(d.size()==5);

    for(auto& x : d)
        cout << x << ",";
    cout << endl;

}

//generic_list 类解决了容器构造的时候就初始化数据。
void case4()
{
    using namespace boost::assign;

    vector<int> v = list_of(1)(2)(3)(4)(5);
    // v = [1, 2, 3, 4, 5]

    deque<string> d =
        (list_of("power")("bomb"),"phazon","suit");
    // d = [power bomb phazon suit]

    set<int> s = (list_of(10), 20,30,40,50);
    // s = {10 20 30 40 50}

    map<int, string> m = list_of(make_pair(1, "one"))(make_pair(2, "two"));
    // m = [(1, “one”) (2, “two”)]

    map<int, int> m1 = map_list_of(1, 2)(3, 4)(5, 6);
    //m1 = [(1, 2)(3, 4)(5, 6)]

    map<int, string> m2 = map_list_of(1, "one")(2, "two");
    //m2 = [(1, "one")(2, "two")]

}

#include <cstdlib>                          //for rand()

//重复数据的填充
void case5()
{
    using namespace boost::assign;

    vector<int> v = list_of(1).repeat(3, 2)(3)(4)(5);
    //v = 1,2,2,2,3,4,5
    for(auto& x : v)
        cout << x << ",";
    cout << endl;


    multiset<int> ms ;
    insert(ms).repeat_fun(5, &rand).repeat(2, 1), 10;
    //ms = x,x,x,x,x,1,1,10
    for(auto& x : ms)
        cout << x << ",";
    cout << endl;


    deque<int> d;
    push_front(d).range(v.begin(), v.begin() + 5);
    //d = 3,2,2,2,1
    for(auto& x : d)
        cout << x << ",";
    cout << endl;

}


//容器适配器
#include <stack>
#include <queue>

void case6()
{
    using namespace boost::assign;

    stack<int> stk = (list_of(1), 2, 3).to_adapter();
    stk += 4, 5, 6;
    for(;!stk.empty();)
    {
        cout << stk.top() << " ";
        stk.pop();
    }
    cout << endl;

    queue<string> q = (list_of("china")("us")("uk")).
        repeat(2, "russia").to_adapter();
    push(q)("germany");
    for(;!q.empty();)
    {
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;

    priority_queue<double> pq = (list_of(1.414), 1.732, 2.236).to_adapter();
    push(pq),3.414,2.71828;
    for(;!pq.empty();)
    {
        cout << pq.top() << " " ;
        pq.pop();
    }

}


//效率更高,用法略麻烦
void case7()
{
    using namespace boost::assign;

    int a = 1, b = 2, c = 3;

    vector<int> v = ref_list_of<3>(a)(b)(c);
    assert(v.size() == 3);
}


int main()
{
    case1();
    case2();
    case3();
    case4();
    case5();
    case6();
    case7();
}
[root@192 c++]# g++ -std=c++11 main.cpp 
[root@192 c++]# ./a.out 
1,2,3,4,5,36,
c,cpp,lua,swift,
1,2,3,4,5,6,7,8,9,10,
wild,the,of,breath,,
1,2,2,2,3,4,5,
1,1,10,846930886,1681692777,1714636915,1804289383,1957747793,
3,2,2,2,1,
6 5 4 3 2 1 
china us uk russia russia germany 
3.414 2.71828 2.236 1.732 1.414


 备注

1.编译器版本gcc4.8.5,运行环境centos7 64位
2.本文只做简单记录用,详细用法请参考 Boost Library,或者是罗剑锋的 boost程序库完全开发指南 书本
3..原文地址http://www.freecls.com/a/2712/9f


©著作权归作者所有
收藏
推荐阅读
简介
天降大任于斯人也,必先苦其心志。