Impossible is nothing  
  爱过知情重醉过知酒浓   花开花谢终是空   缘份不停留像春风来又走   女人如花花似梦
公告
日历
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
统计
  • 随笔 - 8
  • 文章 - 91
  • 评论 - 16
  • 引用 - 0

导航

常用链接

留言簿(4)

随笔分类(4)

随笔档案(8)

文章分类(77)

文章档案(91)

相册

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

总有一些时候,我们不能够借助于“生成式”的初始化方法来给容器赋值,例如我们已经有了一个数组,要把它作为初值赋给一个容器,常规的做法已经深入人心了

总有一些时候,我们不能够借助于“生成式”的初始化方法来给容器赋值,例如我们已经有了一个数组,要把它作为初值赋给一个容器,常规的做法已经深入人心了:

  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

posted on 2006-06-28 13:45 笑笑生 阅读(139) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © 笑笑生 Powered by: 博客园 模板提供:沪江博客