如何获取由 vector::reserve() 分配的缓冲区的地址?

发布于 2024-09-11 23:27:50 字数 735 浏览 2 评论 0原文

我有一个 std::vector 值,我知道其最大大小,但实际大小在使用过程中会有所不同:

void setupBuffer(const size_t maxSize) {
  myVector.reserve(maxSize);
}

void addToBuffer(const Value& v) {
  myVector.push_back(v);

  if (myVector.size() == maxSize) {
    // process data...
    myVector.clear();
  }
}

但是,在 setupBuffer 中,我需要获取指向 myVector 数据开头的指针。我正在使用第三方库,我必须预先缓存该指针,以便在“处理数据...”部分期间进行的调用中使用。

void setupBuffer(const size_t maxSize) {
  myVector.reserve(maxSize);

  cachePtr(&(myVector[0])); // doesn't work, obviously
}

我不想预先 resize() 向量,因为我想使用 vector.size() 来表示添加到向量中的元素数量。

那么,有什么方法可以在分配(reserve())之后但在它有任何元素之前获取指向向量缓冲区的指针吗?我想象缓冲区存在(并且只要我限制 push_back'd 值的数量就不会移动)......也许这不能保证?

I have a std::vector of values for which I know the maximum size, but the actual size will vary during usage:

void setupBuffer(const size_t maxSize) {
  myVector.reserve(maxSize);
}

void addToBuffer(const Value& v) {
  myVector.push_back(v);

  if (myVector.size() == maxSize) {
    // process data...
    myVector.clear();
  }
}

However, in setupBuffer, I need to obtain a pointer to the start of myVector's data. I'm using a third party library where I must cache this pointer up front for use in a call made during the "process data..." section.

void setupBuffer(const size_t maxSize) {
  myVector.reserve(maxSize);

  cachePtr(&(myVector[0])); // doesn't work, obviously
}

I don't want to resize() the vector up front, as I want to use vector.size() to mean the number of elements added to the vector.

So, is there any way to obtain the pointer to the vector's buffer after allocation (reserve()) but before it has any elements? I would imagine the buffer exists (and won't move as long as I restrict the number of push_back'd values)....maybe this isn't guaranteed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尝蛊 2024-09-18 23:27:50

调用保留后,向量缓冲区将不会移动,除非超出保留容量。您的问题是获取指向第一个元素的指针。显而易见的答案是将单个假条目推入向量中,获取指向它的指针,然后将其删除。

更好的方法是,如果库接受函子而不是指针,它会在需要访问缓冲区时调用它 - 您可以使函子推迟获取地址,直到缓冲区有一些实际内容。然而,我意识到你没有重写图书馆的奢侈。

The vector buffer will not be moved after a call to reserve unless you exceed the reserved capacity. Your problem is getting the pointer to the first element. The obvious answer is to push a single fake entry into the vector, get the pointer to it, and then remove it.

A nicer approach would be if the library accepted a functor rather than a pointer, which it would call when it needed to access the buffer - you could make the functor put off getting the address until the buffer had some real contents. However, I realise you don't have the luxury of rewriting the library.

独﹏钓一江月 2024-09-18 23:27:50

不可以。您不能访问 fector 中索引大于 size 的任何元素。调整大小是您唯一的选择。

你可以做的是:

myvec.resize(SOME_LARGE_VALUE);
myLibrary(&myVec[0]);
myvec.resize(GetSizeUsedByLibrary());

当你调整大小时,向量中的元素不会被破坏,除了那些索引高于新大小的元素。调整大小中设置的数字以下的元素将被保留。 使用 std::basic_string< 的示例/code>,但同样适用于向量

No. You are not allowed to access any element in the fector with an index greater than size. Resize is your only option here.

What you can do is something like:

myvec.resize(SOME_LARGE_VALUE);
myLibrary(&myVec[0]);
myvec.resize(GetSizeUsedByLibrary());

When you resize, elements in the vector are not destroyed, except those which had an index above the new size. Elements below the number set in resize are left alone. Example using std::basic_string, but equally applicable to vector

感受沵的脚步 2024-09-18 23:27:50

你尝试过front()吗?另外,您可以push_back一个元素,像您一样获取地址,然后擦除它。

所有这些都不能得到保证,但您可以阅读向量的来源<>看看如果你必须这样做的话,这是否适合你。您还可以轻松地滚动自己的向量,该向量具有指向数据的指针并且从不移动它。

Did you try front()? Also, you could push_back an element, get the address as you did, and then erase it.

All of this is not guaranteed, but you could read the source of your vector<> to see if it's fine for you if you have to do it. You could also roll your own vector pretty easily that has a pointer to the data and never moves it.

手心的海 2024-09-18 23:27:50

围绕它有很多 hack。但我建议您使用有效的方法 - 覆盖分配器,第二个模板参数:

typedef std::vector<MyItem, MyAllocator> myvector_t;

在 MyAllocator 中提供固定的缓冲区分配之后,并将此分配器实例作为向量构造函数的 argumentmnt 传递

There are lot of hacks around it. But I recommend you valid way - override allocator, the second template argument:

typedef std::vector<MyItem, MyAllocator> myvector_t;

After it in MyAllocator provide fixed buffer allocation, and pass this allocator instance as arguemnt of vector's constructor

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文