總有一些時候,我們不能夠借助于“生成式”的初始化方法來給容器賦值,例如我們已經有了一個數組,要把它作為初值賦給一個容器,常規的做法已經深入人心了:
? int init[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
? std::vector<int> vect(init, init + sizeof(init)/sizeof(int));
通過兩個 sizeof 來得到數組的大小在 C 語言里面是很常見的,然而在 C++ 里面,這即便不能稱為丑陋,也絕對稱不上是好。首先其可讀性不好,其次它要進行一次除法來得到一個本來在編譯期間就知道的數字,最后,它并不是總能用的!例如下面的例子:
? std::string strs[] = { "Amy", "Ralph", "Simon", "Maggie" };
現在,你打算用 "sizeof " 什么來除以 "sizeof" 什么?
其實,經過了這么多 C++ GP 的磨練,我們很容易就會想到一個在編譯期間得到靜態數組大小的辦法,模板偏特化是我們常用的武器,在這里非常好用:
template <class T>
struct ArraySize
{
??? static const unsigned int value = 0;
};
template <class T, int S>
struct ArraySize<T[S]>
{
??? static const unsigned int value = S;
};
就這么簡單!雖然它只對付一維數組,但是擴展它是很容易的。不過,模板參數只能為類型,而我們需要傳入的是一個變量。好在在計算機科學里面,加一層抽象是可以解決任何問題的,我們只要加一個模板函數,C++ 會自動幫我們做類型推導:
template <class T>
unsigned int array_size(const T&)
{
??? return ArraySize<T>::value;
}
現在我們可以輕而易舉的搞定那些數組了:
? int ints[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
? std::vector<int> vint(ints, ints + array_size(ints));
?
? std::string strs[] = { "Amy", "Ralph", "Simon", "Maggie" };
? std::vector<std::string> vstr(strs, strs + array_size(strs));
?
? std::for_each(vint.begin(), vint.end(), std::cout << _1 << " ");
? std::cout << std::endl;
? std::for_each(vstr.begin(), vstr.end(), std::cout << _1 << " ");
輸出:
2 3 5 7 11 13 17 19 23
Amy Ralph Simon Maggie
順便說一下,在 boost.type_traits 里面有一個類似于 ArraySize 的工具,叫做 extent ,它更加強大,可以對付多維數組,不過是否值得為了這個而把 boost.type_traits 包含到工程里面去就看讀者自己抉擇了。
=================================================================================
容器的初始化是如此的常見,以至于 boost 提供了一個 assign 庫來簡化這些操作。boost.assign 大量利用了重載的逗號和括號來簡化賦值操作,提供了甚至比用數組更加簡潔的語法:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <boost/assign/std/vector.hpp>
#include <boost/assign/std/list.hpp>
using namespace boost::assign;
int main()
{
? std::vector<int> vint;
? vint += 2,3,5,7,11,13,17,19,23;
?
? std::vector<std::string> vstr;
? vstr += "Amy","Ralph","Simon","Maggie";
?
? std::list<std::string> lstr;
? lstr += "Amy","Ralph","Simon","Maggie";
???
? std::for_each(vint.begin(), vint.end(), std::cout << _1 << " ");
? std::cout << std::endl;
? std::for_each(vstr.begin(), vstr.end(), std::cout << _1 << " ");
? std::cout << std::endl;
? std::for_each(lstr.begin(), lstr.end(), std::cout << _1 << " ");
}
運行這個程序,輸出與前面的大致相同,但是我們注意到初始化更加簡潔了,而且也不需要額外的空間來存儲數組,對于各種類型,都能夠以統一的方式來初始化,真是妙不可言。有趣的是 assign 的作者在文檔中還特意引用了 Bjarne Stroustrup 的話作為引子:
There appear to be few practical uses of operator,()
.
Bjarne Stroustrup, The Design and Evolution of C++
這也許就是 C++ 最大的魅力之一:你無法預料它可以辦到些什么。
下面關于 map 的例子也使用 boost.assign ,可以看到重載的括號給我們帶來了多少方便。
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/assign/list_inserter.hpp>
#include <boost/assign/list_of.hpp>
using namespace std;
using namespace boost::assign;
using namespace boost::lambda;
int main()
{
? map<string,int> months;?
?
? insert( months )
??? ( "january",?? 31 )( "february", 28 )
??? ( "march",???? 31 )( "april",??? 30 )
??? ( "may",?????? 31 )( "june",???? 30 )
??? ( "july",????? 31 )( "august",?? 31 )
??? ( "september", 30 )( "october",? 31 )
??? ( "november",? 30 )( "december", 31 );
???
? map<int,string> persons = map_list_of
??? (2,"Amy")(3,"Ralph")
??? (5,"Simon")(7,"Maggie");
???
? for_each( months.begin(), months.end(),
??? cout << bind(&map<string,int>::value_type::second, _1) << "\t"
???????? << bind(&map<string,int>::value_type::first, _1) << "\n"
? );
? cout << endl;
? for_each( persons.begin(), persons.end(),
??? cout << bind(&map<int,string>::value_type::first, _1) << "\t"
???????? << bind(&map<int,string>::value_type::second, _1) << "\n"
? );?
}
輸出:
30????? april
31????? august
31????? december
28????? february
31????? january
31????? july
30????? june
31????? march
31????? may
30????? november
31????? october
30????? september
2?????? Amy
3?????? Ralph
5?????? Simon
7?????? Maggie