向量< char> VS vector< bool>在C ++ 11中 [英] vector<char> VS vector<bool> in C++11

查看:61
本文介绍了向量< char> VS vector< bool>在C ++ 11中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们应该使用 vector< char> 而不是 vector< bool> vector< char> 更快的原因是什么?

Why should we use vector<char> instead of vector<bool>? What is the reason that vector<char> is faster?

推荐答案

std :: vector< bool> std :: vector< T> 的专业化,主要是为了提高空间效率(值得商))。但是,的行为与常规 std :: vector< T> 相似但不相同。这主要归因于 std :: vector< bool> 不是通常的C ++标准库意义上的容器,而是位数组。 std :: vector< bool> 与常规 std :: vector 之间的一些区别是:

std::vector<bool> is a specialisation of std::vector<T> that's done mainly for space efficiency (debatable). However, it behaves similarly but not equally as a regular std::vector<T>. This is attributed mainly to the fact that std::vector<bool> is not a container in the usual C++ standard library sense but rather an array of bits. Some of the differences between a std::vector<bool> and a regular std::vector are:


  • std :: vector< bool> :: iterator 不是随机访问的迭代器。

  • std :: vector< bool> 不需要将其元素存储为连续数组。

  • std :: vector< bool> class std :: vector< bool> :: reference 公开为一种访问单个位的方法。特别是,此类的对象由 operator [] std :: vector< bool> :: front 返回, std :: vector< bool> :: back 按值。

  • std :: vector< bool> ; 不使用 std :: allocator_traits :: construct 来构造位值。

  • std :: vector< bool> 不保证同一容器中的不同元素可以被不同的线程同时修改。

  • std :: vector< bool> 具有不同的接口(即 flip()

  • std::vector<bool>::iterator is not a random-access iterator.
  • std::vector<bool> is not required to store its elements as a contiguous array.
  • std::vector<bool> exposes class std::vector<bool>::reference as a method of accessing individual bits. In particular, objects of this class are returned by operator[], std::vector<bool>::front, std::vector<bool>::back by value.
  • std::vector<bool> does not use std::allocator_traits::construct to construct bit values.
  • std::vector<bool> does not guarantee that different elements in the same container can be modified concurrently by different threads.
  • std::vector<bool> has a different interface (i.e., flip())

除了打破 std :: vector 作为连续容器的概念含义外,这些区别也有打破的趋势。用户代码,尤其是当该代码是通用代码(即模板代码)时。

These differences except from breaking the conceptual meaning of std::vector as a contiguous container, have also a tendency to break user code, especially when this code is generic (i.e., template code).

为澄清起见,请考虑以下示例:

To clarify consider the following example:

template<class T> void foo(std::vector<T> &v) {
 T &frnt = v.front();
 ...
}

上面的示例适用于每个 T ,除非 T = bool 。如前所述,失败的原因是 v.front()的返回值是一个临时值(按值返回),因此不能绑定到引用。

The above example works for every T except when T = bool. As already mentioned, the reason for this failure is that v.front()'s returned value is a temporary (returns by value) and as such cannot bind to a reference.

为避免这种破坏,许多编码人员避免使用 std :: vector< bool> ,而更喜欢使用 std :: vector< char> 。由于引起的许多问题,许多人指出,使用 std :: vector< bool> 可能被认为是过早的优化。为了解决该问题,许多杰出的C ++社区成员提出了建议。这些建议之一建议删除 std :: vector< bool> 的另一个名称,即不会引用C ++标准库容器。

To avoid this havoc many coders avoid the use of std::vector<bool> and prefer the use of std::vector<char>. Due to the many problems caused, it is stated by many that use of std::vector<bool> could be considered as a premature optimisation. To the resolution of that matter there are proposals from many distinguished C++ community members. One of these proposals suggests to remove std::vector<bool> under a different name that it will not refer to C++ standard library containers.

由于时间性能问题, std :: vector< bool> 的主要问题是它们各自的 std: :algorithm 算法并未为此进行优化。因此,尽管您可以通过使用它来获得空间,但使用它可能会导致非常显着的速度悲观。

Now to the time performance issue, the main problem with std::vector<bool> is that its respective std::algorithm algorithms are not optimised for it. Thus, although you might gain space by using it, it's use might lead to a very significant speed pessimization.


  1. 向量:更多问题,更好的解决方案,草药销售商

  2. vector< bool> ,霍华德·辛南特(Howard Hinnant)

  3. std :: vector< bool>

  1. vector: More Problems, Better Solutions, Herb Sutter
  2. On vector<bool>, Howard Hinnant
  3. std::vector<bool>

这篇关于向量&lt; char&gt; VS vector&lt; bool&gt;在C ++ 11中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆