开发者

Copying a vector reference to another

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& aSubscriptionDa开发者_运维技巧ta, 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?


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.)


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());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜