简体   繁体   English

将矢量引用复制到另一个

[英]Copying a vector reference to another

I've a structure, say SubscriptionData. 我有一个结构,例如SubscriptionData。 Two variables of the type are declared in function 1. 在函数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. 现在,我调用一个函数来填充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. 在功能breakSubDataLen()中,我应该将元素从aSubscriptionData.begin()+ 3复制到aSubscriptionData.end()到结构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? 谁能帮我吗?

std::copy doesn't allocate elements; std::copy不分配元素; 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::back_inserter ,它调用push_back将每个元素插入到容器中:

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

Another common approach is to preallocate sufficient space in the destination sequence using resize : 另一种常见的方法是使用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. 使用std::back_inserter ,保留序列中的任何元素,并在现有元素之后插入新元素。 With the resize approach, any existing elements are overwritten. 使用resize方法,所有现有元素都将被覆盖。 You can also use the resize approach and retain any existing elements: 您还可以使用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);

(This requires that destination is a random accessible container like std::vector ; if it isn't, you'll have to modify the code accordingly.) (这要求destination是一个可随机访问的容器,如std::vector ;如果不是,则必须相应地修改代码。)

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() . 通过将aResultSubscriptionData.apn_config_file.apn_config.begin()作为std::copy的第三个参数传递,您告诉它您已经分配了足够的空间来分配.begin()开始的元素。 If the vector has 0 elements in it, then clearly .begin() or .begin() + X does not refer to valid iterator. 如果向量中包含0个元素,那么.begin().begin() + X显然不会引用有效的迭代器。 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 vector s range insertion. 您极有可能要使用vector的范围插入。 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM