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的內容很多,許多東西只需要瞭解即可,有些東西確實很實用需要掌握,但是有些內容感覺就是過度編程,使用價值很小,需要適可而止。