STLvectorbool的介绍(1)
原文地址:http://www.cplusplus.com/reference/vector/vector-bool/
class template specialization
std::vector
template < class T, class Alloc = allocator
template
Vector of bool
This is a specialized version of vector, which is used for elements of type bool and optimizes for space.
这是特别为了当元素类型是bool类型的时候,优化空间所编写的一个vector版本。
It behaves like the unspecialized version of vector, with the following changes:
这个vector的行为和未特化的vector版本类似,但是有下列的变化。
- The storage is not necessarily an array of bool values, but the library implementation may optimize storage so that each value is stored in a single bit.
存储的时候存储一列的bool值是没有必要的,编译器可能将每一个bool值存放在一个bit里面以优化存储空间。 - Elements are not constructed using the allocator object, but their value is directly set on the proper bit in the internal storage.
元素的构造不使用内存分配器,而是直接在内存相应的bit里面设置其值。 - Member function flip and a new signature for member swap.
成员方法flip和新特性的swap方法。 - A special member type, reference, a class that accesses individual bits in the container’s internal storage with an interface that emulates a bool reference. Conversely, member type const_reference is a plain bool.
一个特别的成员类型,reference,一个类用来在容器存储内存位置访问单个bits的接口,该接口用来模仿bool的reference,反之,成员类型const_reference是一个真正的bool. - The pointer and iterator types used by the container are not necessarily neither pointers nor conforming iterators, although they shall simulate most of their expected behavior.
容器使用的指针以及迭代器类型既不必要指向也不必要符合迭代器。(这句翻译的好怪。。。),即便他们看起来应该模范他们应该做的行为。
These changes provide a quirky interface to this specialization and favor memory optimization over processing (which may or may not suit your needs). In any case, it is not possible to instantiate the unspecialized template of vector for bool directly. Workarounds to avoid this range from using a different type (char, unsigned char) or container (like deque) to use wrapper types or further specialize for specific allocator types.
这些变化提供了一个离奇的接口给这个特例化的vector,以及促使数据整理时的内存优化(这可能适合也可能不适合你的需要)。在任何情况下,直接实例化vector的非特例化的bool版本是不可能的。避免该限制的变通方法是使用其他的类型(char,unsigned char)或者容器(像deque)的封装类型,或者使用更远的特别的分配器类型。
bitset is a class that provides a similar functionality for fixed-size arrays of bits.
bitset是一个为固定大小的bits数组提供了相似功能一个类。
Template parameters
模版参数
Alloc
Type of the allocator object used to define the storage allocation model. By default, allocator
Alloc是定义用来分配内存模型的内存分配器对象。默认是使用allocator
Aliased as member type vector
Member types
member type
definition
notes
value_type
The first template parameter (bool)
allocator_type
The second template parameter (Alloc)
defaults to: allocator
A specific member class (see reference below)
const_reference
bool
pointer
a type that simulates pointer behavior
convertible to const_pointer
const_pointer
a type that simulates pointer to const behavior
iterator
a type that simulates random access iterator behavior
convertible to const_iterator
const_iterator
a type that simulates random access iterator to const behavior
reverse_iterator
const_reverse_iterator
reverse_iterator<const_iterator>
difference_type
a signed integral type
usually the same as ptrdiff_t
size_type
an unsigned integral type
usually the same as size_t
Member classes
Reference type (public member class )
Member functions
The specialization has the same member functions as the unspecialized vector, except data, emplace, and emplace_back, that are not present in this specialization.
特例化的版本和未特例化的版本具有相同的成员方法。除了data,emplace以及emplace_back,这些在特例化版本中不会出现。
测试一下即可。
#include
#include
using namespace std;
int main()
{
vector
cout<<”vb=”;
for(bool b:vb){
cout<<b<<” “;
}
cout<<endl;
cout<<”vb.emplace(vb.begin(),false)”<<endl;
vb.emplace(vb.begin(),false);
for(bool b:vb){
cout<<b<<” “;
}
}
编译错误截图:
It adds the following:
Flip bits (public member function )
Swap containers or elements (public member function )
Non-member class specializations
Hash for vector (class template specialization )
Data races
Simultaneous access to different elements is not guaranteed to be thread-safe (as storage bytes may be shared by multiple bits).
同时访问不同的元素不保证是线程安全的(内存的bit可能被共享为多重的bit)
//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
2014-8-20
于GDUT
- 本文作者: royalchen
- 本文链接: http://www.royalchen.com/2016/02/24/stlvector-bool的介绍(1)/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!