boost-工具类1

1.noncopyable类

位置:#include <boost/noncopyable.hpp> 或者#include <boost/utility.hpp>

c++类中,如果不明确定义拷贝构造函数好拷贝赋值操作符,编译器会为我们合成一个,为了避免这种浅赋值”(会很危险),我们可以通过继承noncopyable类。

即可以通过隐式private继承,也可以public继承。noncopyable类的实现也很简单,只要私有化拷贝构造函数和赋值操作符。

2.typeof类

位置:#include <boost/typeof/typeof.hpp>为了减轻书写烦琐的变量类型声明的工作,简化代码。

例如:stl迭代器的声明:std::map<std::string, std::string>::iterator pos =s.begin(); 我们使用宏BOOST_TYPEOF可以在编译期间自动推导表达式的类型。

这样就不用写这么长的声明了。

例如:

#include <iostream>
#include <vector>
#include <utility>//pair类
#include <string>
#include <boost/typeof/typeof.hpp>

using namespace boost;
using std::cout;
using std::endl;
using std::string;

int main() 
{
BOOST_TYPEOF(2.1*3) x = 2.1*3;//定义x变量,typeof自动推导x的类型。
cout<<x<<endl;//6.3
//std::pair p = std::make_pair(1, "string");
BOOST_AUTO(p, std::make_pair(1, "string"));//用法类似typeof,自动推导p,类型为pair类型。
cout<<p.first<<endl;//1.
cout<<p.second<<endl;//string.
}

3.assign.

库的命名空间:boost::assign。头文件 #include <boost/assign.hpp>

作用:因为STL容器仅提供了容纳这些数据的方法,但是填充初始化的步骤却相当麻烦,必须调用insert()或者push_back()等成员函数,

但是boost.assign提供了填入大量数据的方法。

应用实例

#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
#include <iostream>
#include <utility>
#include <boost/assign.hpp>

using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;
using std::list;
using std::set;
using std::deque;

int main()
{
    using namespace boost::assign;
    //第一种方法:
    //使用assign中的+=操作符初始化,非常方便;
    //但是它仅限于标准容器(vector,list,set,map等)
    vector<int> v;
    v += 1,2,3,4,5,6*6;
    
    vector<int>::iterator it;
    for(it=v.begin(); it!=v.end(); ++it)
        cout<<*it<<endl;

    std::set<std::string> s;
    s += "cpp", "java", "c#", "python";

    //第二种方法:
    //使用assign的()操作符向容器增加元素。
    //不能直接使用operator(),而应当使用assign库提供的三个辅助函数
    //insert(),push_front(), push_back();
    vector<int> iv;
    push_back(iv) (1) (2) (3) (4) (5);
    for(it=iv.begin(); it!=iv.end(); ++it)
        cout<<*it<<endl;

    list<string> l;
    push_front(l) ("cpp")("java")("c#")("python");

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

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

    //第三种方法:在容器构造的时候就完成初始化。
    //assign库使用list_of(),map_list_of()/pair_list_of()
    //和tuple_list_of();三个函数解决了问题。

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

    deque<string> d2 = list_of("power")("bomb")("suit");//注意括号
    //d = [power, bomb suit];

    set<int> s2 = (list_of (10), (20), (30), (40), (50));//注意括号

    map<int, string> m2 = (list_of(std::make_pair(1, "one")));

    map<int, int> m3 = map_list_of(1,2)(3,4)(5,6);//map_list_of的用法。

    //使用repeat()方法减少重复输入    
    vector<int> v3 = list_of(1).repeat(3,2)(3)(4)(5);
    //v3 = 1,2,2,2,3,4,5;

    set<int> ms;
    //第一个参数时重复次数,第二个参数是一个函数对象,返回填入的值,
    //这里使用rand随机函数。
    insert(ms).repeat_fun(5,&rand).repeat(2,1), 10;

    deque<int> di;
    //range()函数将v3容器中的前五个元素插入到di中。
    push_front(di).range(v3.begin(), v3.begin()+6);
    //4,3,2,2,2,1 由于deque底层采用大顶堆实现,所以是降序输出。
    
    return 0;   
}
4.swap,是std::swap的增强和泛化。 位置:#include <boost/swap.hpp>
 std::swap的实现:
template<typename T>//要求T类型必须可以拷贝构造和拷贝赋值。(重载operator=)
void swap(T& a, T& b)
{
	T tmp(a);//需要创建临时对象。如果对象较大,代价会很高。
	a = b;
	b = tmp;
}
 boost::swap()实现:
 声明:template<class T1, class T2>
 void swap(T1& left, T2& right);
区别:std::swap使用了标准的交换操作,而boost::swap通过ADL规则找到了全局名字空间的特化交换函数。
尽量使用boost::swap,它提供了比std::swap更好的优化策略。
int main()
{
    int a1[10];
    int a2[10];//如果长度不相同,则不能交换,编译错误。

    std::fill_n(a1, 10, 5);
    std::fill_n(a2, 10, 20);

    //实现就是用for循环,对数组中的每个元素进行交换得到的。  
    boost::swap(a1, a2);

    cout<<a1[1]<<endl;//20
    cout<<a2[1]<<endl;//5

    vector<int> v1(5, 10);
    vector<int> v2(5, 20);
    boost::swap(v1, v2);
    cout<<v1[1]<<endl;
    cout<<v2[1]<<endl;

    return 0;   
}
5. tribool 类似bool类型,
但是它有三种状态:true(真),和false(假),还有indeterminate(未知、不确定)
位置:#include <boost/logic/tribool.hpp>
类摘要:
class tribool
{
tribool(bool value);//默认值是false
enum value_t {false_value, ture_value, indeterminate_value } value;
}
三态布尔逻辑:
1.任何与indeterminate的比较操作结果都是indeterminate;
2.与indeterminate的逻辑||操作,只有与true运算结果为true,其余均为indeterminate。
3.与indeterminate的逻辑&&操作,只有与false运算结果为false,其余均为indeterminate;
4.indeterminate的非操作!结果仍为indeterminate;
ps:可以使用BOOST_TRIBOOL_THIRD_STATE(unknow);给indeterminate更换名称。
tribool的输入输出:

另外包含#include <boost/logic/tribool_io.hpp>

#include <boost/swap.hpp>
#include <boost/logic/tribool.hpp>
#include <boost/logic/tribool_io.hpp>

using std::cout;
using std::endl;
using std::vector;

using namespace boost;


//BOOST_TRIBOOL_THIRD_STATE(unknow);

int main()
{
    tribool tb1(true);
    tribool tb2 = indeterminate;

    if(tb1)
        cout<<"true"<<endl;


    tb2 = indeterminate;
    tribool tb3 = false;
    cout<<tb1<<endl;//1
    cout<<tb2<<endl;//2
    cout<<tb3<<endl;//0

    if(tb2 == indeterminate)
        cout<<"indeterminate"<<endl;

    cout<<(tb2 || true)<<endl;//true
    cout<<(tb2 && false)<<endl;//false 

    return 0;   
}


  看了这么久,感觉boost的内容很多,许多东西只需要了解即可,有些东西确实很实用需要掌握,但是有些内容感觉就是过度编程,使用价值很小,需要适可而止。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值