r2 - 11 Aug 2006 - 13:40:58 - TWikiGuestYou are here: TWiki >  Sandbox Web  > BrightmanSandbox

C++ Boost.Array文档(翻译)

译者:Brightman

Chapter 2.Boost.Array

Introduction

C++标准模板库STL作为C++标准库的一部分提供了针对不同容器的算法的框架。但是,普通数组并未提供STL容器的接口(虽然它们有STL容器的迭代器的接口)。
STL提供了std::vector来替代普通数组,由于std::vector()采用动态数组机制,因此它能管理具有动态个数的数据,但对于仅需要静态数组来说这将产生额外负担。
在Matthew H. Austern的《Generic Programming and the STL》中,他引入一种包装普通数组和静态大小的类,叫做block。这个类更安全以及不比普通数组差多少的性能。在《The C++ Programming Language》第三版中,Bjarne Stroustrup也介绍了一个相似功能的类c_array,我(Nicolai Josuttis)在我的著作《The C++ Standard Library-A Tutorial and Reference》中对c_array稍作修改,改为carray类。
考虑到这些不同的命名,我们决定将其简单地命名为array。
注意:该类已经被建议扩充到C++标准库中。http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1548.htm
类array完成了大部分(非全部)“可逆容器”的要求。(see Section 23.1, [lib.container.requirements] of the C++ Standard)
为什么array不是一个STL 可逆容器?
  • 没有提供构造函数
  • 元素可能有一个未知的初值
  • swap()非常量性能
  • size()返回常量,取决于类型的第二个模板参数
  • 该容器类未提供内存适配器(allocator)

该类没有实现序列式容器(sequence)的要求(see Section 23.1.1, [lib.sequence.reqmts] of the C++ Standard), 只是提供了下列函数:

  • front()和back()
  • operator[]和at()

Reference

Header <boost/array.hpp>

Header <boost/array.hpp>

namespace boost {
        template<typename T, std::size_t N> class array;
        template<typename T, std::size_t N> void swap(array<T, N>&, array<T, N>&);
        template<typename T, std::size_t N> 
        bool operator==(const array<T, N>&, const array<T, N>&);
        template<typename T, std::size_t N> 
        bool operator!=(const array<T, N>&, const array<T, N>&);
        template<typename T, std::size_t N> 
        bool operator<(const array<T, N>&, const array<T, N>&);
        template<typename T, std::size_t N> 
        bool operator>(const array<T, N>&, const array<T, N>&);
        template<typename T, std::size_t N> 
        bool operator<=(const array<T, N>&, const array<T, N>&);
        template<typename T, std::size_t N> 
        bool operator>=(const array<T, N>&, const array<T, N>&);
}

Design Rationale

考虑到构造函数,我们在设计上有重大权衡(There was an important design tradeoff regarding the constructors)。我们将array作为一个“集合”(see Section 8.5.1, [dcl.init.aggr], of the C++ Standard)来实现。这意味着:
  • array能被大括弧包括的、逗号分隔的初始化列表初始化arrary的各元素,依照元素下标递增顺序(从0开始)初始化:
boost::array<int,4> a = { { 1, 2, 3 } };
注意:初始化列表可以包含比该数组元素个数更少的初始值,余下的元素将被默认初始化(即某个预定义的值)。

但是这种方法有它的缺点:比如没有传递初始化列表意味着每个元素有不确定的初值。从这条规则,我们可以推测这个“集合”(类array)可能会:

  • 没有用户声明的构造函数
  • 没有private或protected非静态数据成员
  • 没有父类
  • 没虚函数

注意:对于兼容标准的编译器,你可以使用更少的大括弧来初始化arrary(according to 8.5.1 (11) of the Standard),如下:

boost::array<int,4> a = { 1, 2, 3 };

我感谢任何有建设性的反馈。不过请注意,由于我没有时间读所有的boost email,为确保反馈信息发送至本人, please send me a copy of each mail regarding this class.

For more information...

关于使用C++的普通数组更多细节和STL的框架可以参见Nicolai M. Josuttis的《The C++ Standard Library-A Tutorial and Reference》
Home Page of Nicolai Josuttis

Acknowledgements

Doug Gregor ported the documentation to the BoostBook format.

Copyright © 2001-2004 Nicolai M. Josuttis
Permission to copy, use, modify, sell and distribute this software is granted provided this copyright notice appears in all copies. This software is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.

Edit | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r2 < r1 | More topic actions

tip TWiki Tip of the Day
Revision control
TWiki has "Soft security" anyone can change anything, but changes are logged. There is a complete ... Read on Read more

 
Powered by TWiki
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback