std :: vector中的数据存储是否连续? [英] Is data storage in std::vector continuous?

查看:65
本文介绍了std :: vector中的数据存储是否连续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个chars向量,我想将其内容作为char *传递给另一个函数:

I have a vector of chars and I want to pass it's content as a char* to another function:

void foo(boost::shared_ptr<std::vector<boost::uint8_t> > data)
{
    bar(data->size()?reinterpret_cast<char*>(&(data.get()->front())):NULL);
}

我可以假定数据始终以连续方式存储吗?谢谢.

Can I assume that the data is always stored in a contiguous manner? Thanks.

推荐答案

来自n2798(C ++ 0x草案):

From the n2798 (draft of C++0x)::

23.2.6类模板向量[vector]

1向量是支持随机访问的序列容器迭代器.另外,它支持(摊销)固定时间插入最后清除操作;在中间插入并删除线性时间.尽管有提示,但存储管理是自动处理的可以提高效率.向量的元素是连续存储,这意味着如果v是向量,则T是一些类型不是bool,则服从& v [n] ==& v [0] + n对于所有0< = n<v.size().

1 A vector is a sequence container that supports random access iterators. In addition, it supports (amortized) constant time insert and erase operations at the end; insert and erase in the middle take linear time. Storage management is handled automatically, though hints can be given to improve efficiency. The elements of a vector are stored contiguously, meaning that if v is a vector where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size().

在向数组添加元素之后,还要检查数组是否被重新分配(使所有指针和迭代器无效).

Also do check array being reallocated (invalidating any pointers and iterators) after adding elements to it.

也请查看此文章:-实际上,连续性是向量抽象的一部分.是这样重要的是,事实上,当发现C ++ 98标准并不能完全保证连续性,C ++ 03标准进行了修改,以明确添加担保.

contiguity is in fact part of the vector abstraction. It’s so important, in fact, that when it was discovered that the C++98 standard didn’t completely guarantee contiguity, the C++03 standard was amended to explicitly add the guarantee.

也来自 C ++常见问题解答

Also from the C++ FAQ

#include <vector>
#include "Foo.h"  /* get class Foo */

// old-style code that wants an array
void f(Foo* array, unsigned numFoos);

void g()
{
  std::vector<Foo> v;
  ...
  f(v.empty() ? NULL : &v[0], v.size());  ← safe
}

有趣的表达 v.empty()吗?NULL:& v [0] 仅在v为空时传递NULL指针,否则将指针传递给v的第一个(第零个)元素.如果您知道先验的v不为空,则可以更改就是& v [0].通常,这意味着您可以保证& v [0] + n ==& v [n] ,其中v是 std :: vector< T> ,并且n是范围 0 .. v.size()-1.

The funny expression v.empty() ? NULL : &v[0] simply passes the NULL pointer if v is empty, otherwise passes a pointer to the first (zeroth) element of v. If you know a priori that v is not empty, you can change that to simply &v[0]. In general, it means you are guaranteed that &v[0] + n == &v[n], where v is a std::vector<T> and n is an integer in the range 0 .. v.size()-1.

v.begin()不能保证为T *,这意味着v.begin()不能保证与& v [0]相同:

However v.begin() is not guaranteed to be a T*, which means v.begin() is not guaranteed to be the same as &v[0]:

void g()
{
  std::vector<Foo> v;
  ...
  f(v.begin(), v.size());  ← Error!! Not Guaranteed!!
    ^^^^^^^^^-- cough, choke, gag; not guaranteed to be the same as &v[0]
}

这篇关于std :: vector中的数据存储是否连续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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