将向量引用复制到另一个向量引用

发布于 2024-10-11 18:08:42 字数 864 浏览 5 评论 0原文

我有一个结构,例如 SubscriptionData。在函数 1 中声明了该类型的两个变量。

SubscriptionData aSubscriptionData;
SubscriptionData aResultSubscriptionData;

该结构本身具有另一个结构,而该结构又具有另一个结构的向量。下面给出了向量,

aSubscriptionData.apn_config_file.apn_config

我已经正确初始化了结构。现在,我调用一个函数来填充订阅数据。然后我调用另一个函数,其原型如下所示。

void breakSubDataLen(ImsMsg::SubscriptionData& aSubscriptionData, ImsMsg::SubscriptionData& aResultSubscriptionData);

在函数breakSubDataLen()中,我应该将元素从aSubscriptionData.begin()+3复制到aSubscriptionData.end()到结构aResultSubscriptionData。

我正在为此进行以下操作。

std::copy(aSubscriptionData.apn_config_file.apn_config.begin() + 3, 
    aSubscriptionData.apn_config_file.apn_config.end(),
    aResultSubscriptionData.apn_config_file.apn_config.begin());

这似乎不起作用。有人可以帮我吗?

I've a structure, say SubscriptionData. Two variables of the type are declared in function 1.

SubscriptionData aSubscriptionData;
SubscriptionData aResultSubscriptionData;

The structure in itself has another structure, which in turn has a vector of another structure. The vector is given below

aSubscriptionData.apn_config_file.apn_config

I've initialized the structures properly. Now, I call a function to populate aSubscriptionData. Then I call another function whose prototype is given below.

void breakSubDataLen(ImsMsg::SubscriptionData& aSubscriptionData, ImsMsg::SubscriptionData& aResultSubscriptionData);

In the function breakSubDataLen(), I'm supposed to copy the elements from aSubscriptionData.begin()+3 to aSubscriptionData.end() to the structure aResultSubscriptionData.

I'm doing the following operation for that.

std::copy(aSubscriptionData.apn_config_file.apn_config.begin() + 3, 
    aSubscriptionData.apn_config_file.apn_config.end(),
    aResultSubscriptionData.apn_config_file.apn_config.begin());

It doesn't seem to work. Can anyone help me out?

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

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

发布评论

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

评论(2

橘寄 2024-10-18 18:08:42

std::copy 不分配元素;您必须确保目标范围中有足够的空间来容纳源范围中的所有元素。

执行此操作的常见方法是使用 std::back_inserter,它调用 push_back 将每个元素插入到容器中:

std::copy(source.begin(), source.end(), std::back_inserter(destination));

另一种常见方法是在目标中预先分配足够的空间使用 resize 进行序列:

destination.resize(std::distance(source.begin(), source.end()));
std::copy(source.begin(), source.end(), destination.begin());

当然,这两种方法具有不同的行为。使用std::back_inserter,序列中的任何元素都会被保留,并且新元素会插入到现有元素之后。使用resize方法,任何现有元素都会被覆盖。您还可以使用resize方法并保留任何现有元素:(

const std::size_t original_size = destination.size();
destination.resize(destination.size() + 
                   std::distance(source.begin(), source.end());

std::copy(source.begin(), source.end(), destination.begin() + original_size);

这要求destination是一个随机可访问的容器,如std::vector;如果不是,您必须相应地修改代码。)

std::copy doesn't allocate elements; you have to make sure there is sufficient space in the destination range to hold all of the elements in the source range.

A common way to do this is to use std::back_inserter, which calls push_back to insert each element into a container:

std::copy(source.begin(), source.end(), std::back_inserter(destination));

Another common approach is to preallocate sufficient space in the destination sequence using resize:

destination.resize(std::distance(source.begin(), source.end()));
std::copy(source.begin(), source.end(), destination.begin());

These two approaches have different behavior, of course. With std::back_inserter, any elements in the sequence are retained and new elements are inserted after the existing elements. With the resize approach, any existing elements are overwritten. You can also use the resize approach and retain any existing elements:

const std::size_t original_size = destination.size();
destination.resize(destination.size() + 
                   std::distance(source.begin(), source.end());

std::copy(source.begin(), source.end(), destination.begin() + original_size);

(This requires that destination is a random accessible container like std::vector; if it isn't, you'll have to modify the code accordingly.)

爱人如己 2024-10-18 18:08:42

通过将 aResultSubscriptionData.apn_config_file.apn_config.begin() 作为第三个参数传递给 std::copy,您就告诉它您已为其分配了足够的空间分配.begin()开始的元素。如果向量中有 0 个元素,则显然 .begin().begin() + X 不引用有效的迭代器。如果向量已经包含足够的元素,或者调整了大小,则这将起作用,在这种情况下,它将把元素复制到新向量中。

您很可能想要使用向量的范围插入。我相信这会更有效率:

aResultSubscriptionData.apn_config_file.apn_config.insert(0, aSubscriptionData.apn_config_file.apn_config.begin() + 3, aSubscriptionData.apn_config_file.apn_config.end());

By passing aResultSubscriptionData.apn_config_file.apn_config.begin() as the third argument to std::copy, you are telling it that you have allocated enough space for it to assign the elements starting at .begin(). If the vector has 0 elements in it, then clearly .begin() or .begin() + X does not refer to valid iterator. This would work if the vector already held enough elements, or if it was resized, in which case it would copy the elements into the new vector.

More than likely, what you want to use is to use vectors range insertion. It will be much more efficient I believe:

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