使用指针写入std容器 [英] Using pointers to write to a std container

查看:224
本文介绍了使用指针写入std容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要能够创建一个固定长度的容器(向量 deque ?)作为一个缓冲区,然后给另一个对象的向量指向允许写入的缓冲区的位置。

I'd like to be able to create a fixed-length container (vector? deque?) to act a buffer, and then give another vector of objects a pointer to the location of the buffer where they are allowed to write in.

示例(不可编译代码) p>

Example (not compileable code)

class Item {
  *p //pointer to a place in the vector
  vector<int> values
}

vector<Item> items;

for(auto item : items) {
  for(auto value : values) {
    buffer[p] = item->value
    ++(item->p);
  }
}

但我不知道如何关系,每个项目清楚它们应该开始写入缓冲区。

However, I'm not sure how to make the relationship where it's clear to each Item where they should start writing in the buffer.

我应该注意,对于每个在项上迭代,最终的缓冲区具有已知的固定大小 - 但是在函数调用之间, Items

I should note that for each iteration over items, the final buffer has a known fixed size -- but between function calls, the number of Items could change.

感谢,

推荐答案

问题正确(我不知道),你应该使用一个索引,而不是一个指针或迭代器,因为它是一个相对偏移缓冲区的开始,而不是一个绝对地址将被无效的更改

If I've understood the question correctly (which I'm not sure about), you should use an index, not a pointer or an iterator, because it is a relative offset to the start of the buffer, not an absolute address that would be invalidated by changes to the buffer.

class Item
{
  size_t pos;  // index into the buffer
  vector<int> values;
};

vector<Item> items;
// ...
std::vector<int> buffer;
buffer.resize(N);
for (auto& item : items)
{
  assert(buffer.size() >= (item.pos + item.values.size()));
  std::copy(std::begin(item.values), std::end(item.values),
            std::begin(buffer)+item.pos);
}

这将使用向量 deque 作为缓冲区(或任何其他与RandomAccessIterators),但因为你似乎不需要在缓冲区的开始添加/删除元素(只调整一次并分配给现有元素),那么没有理由使用 deque。因此,您应该优先使用 vector`,通常应该是您的默认选择,除非您需要其他容器的特定特性。

This will work with either a vector or deque as the buffer (or anything else with RandomAccessIterators), but as you don't appear to need to add/remove elements at the start of the buffer (only resize it once and assign to existing elements) then there is no reason to use deque. Therefore you should prefervector`, which should generally be your default choice of container unless you need the particular characteristics of one of the other containers.

我不知道你如何计划设置 Item :: pos 值,也许这是有道理的:

I don't know how you plan to set the Item::pos values, maybe this would make sense:

size_t pos = 0;
for (auto& item : items)
{
  item.pos = pos;
  pos += item.values.size();
  assert(buffer.size() >= pos);
  std::copy(std::begin(item.values), std::end(item.values),
            std::begin(buffer)+item.pos);
}



这会将每个项目依次放入缓冲区,

This would put each item in turn into the buffer, and record the position on the fly.

甚至可以在不知道总缓冲区大小的情况下工作,根据需要调整缓冲区大小:

This could even be made to work without knowing the total buffer size in advance, resizing the buffer as needed:

size_t pos = 0;
for (auto& item : items)
{
  item.pos = pos;
  pos += item.values.size();
  if (buffer.size() < pos)
    buf.resize(pos);
  std::copy(std::begin(item.values), std::end(item.values),
            std::begin(buffer)+item.pos);
}

因为您要存储索引,而不是绝对地址,即使在调整缓冲区大小并将其内容重新定位到不同的内存块之后仍然可以工作。

Because you're storing an index, not an absolute address, it continues to work even after the buffer is resized and its contents relocated to a different block of memory.

这篇关于使用指针写入std容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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