泛型算法:Tips (4) --- 再论初始化

来源:互联网 发布:百合网数据库 zip下载 编辑:程序博客网 时间:2024/05/16 15:36
总有一些时候,我们不能够借助于“生成式”的初始化方法来给容器赋值,例如我们已经有了一个数组,要把它作为初值赋给一个容器,常规的做法已经深入人心了:

  int init[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
  std::vector vect(init, init + sizeof(init)/sizeof(int));

通过两个 sizeof 来得到数组的大小在 C 语言里面是很常见的,然而在 C++ 里面,这即便不能称为丑陋,也绝对称不上是好。首先其可读性不好,其次它要进行一次除法来得到一个本来在编译期间就知道的数字,最后,它并不是总能用的!例如下面的例子:

  std::string strs[] = { "Amy", "Ralph", "Simon", "Maggie" };

现在,你打算用 "sizeof " 什么来除以 "sizeof" 什么?

其实,经过了这么多 C++ GP 的磨练,我们很容易就会想到一个在编译期间得到静态数组大小的办法,模板偏特化是我们常用的武器,在这里非常好用:

template
struct ArraySize
{
    static const unsigned int value = 0;
};

template
struct ArraySize
{
    static const unsigned int value = S;
};

就这么简单!虽然它只对付一维数组,但是扩展它是很容易的。不过,模板参数只能为类型,而我们需要传入的是一个变量。好在在计算机科学里面,加一层抽象是可以解决任何问题的,我们只要加一个模板函数,C++ 会自动帮我们做类型推导:

template
unsigned int array_size(const T&)
{
    return ArraySize::value;
}

现在我们可以轻而易举的搞定那些数组了:

  int ints[] = {2, 3, 5, 7, 11, 13, 17, 19, 23};
  std::vector vint(ints, ints + array_size(ints));
 
  std::string strs[] = { "Amy", "Ralph", "Simon", "Maggie" };
  std::vector 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
#include
#include
#include

#include
#include

using namespace boost::assign;

int main()
{
  std::vector vint;
  vint += 2,3,5,7,11,13,17,19,23;
 
  std::vector vstr;
  vstr += "Amy","Ralph","Simon","Maggie";
 
  std::list 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
#include
#include
#include

#include
#include

#include
#include

using namespace std;
using namespace boost::assign;
using namespace boost::lambda;

int main()
{
  map 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 persons = map_list_of
    (2,"Amy")(3,"Ralph")
    (5,"Simon")(7,"Maggie");
   
  for_each( months.begin(), months.end(),
    cout << bind(&map::value_type::second, _1) << "/t"
         << bind(&map::value_type::first, _1) << "/n"
  );
  cout << endl;
  for_each( persons.begin(), persons.end(),
    cout << bind(&map::value_type::first, _1) << "/t"
         << bind(&map::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




原创粉丝点击