STL向量reserve()和copy() [英] STL vector reserve() and copy()

查看:143
本文介绍了STL向量reserve()和copy()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,

我试图使用以下2个缩写代码行执行从一个向量(vec1)到另一个向量(vec2)的复制应用程式如下):

I am trying to perform a copy from one vector (vec1) to another vector (vec2) using the following 2 abbreviated lines of code (full test app follows):

vec2.reserve( vec1.size() );
copy(vec1.begin(), vec1.end(), vec2.begin());

当调用vec2设置矢量vec2的容量时,数据复制到vec2似乎不是填充从vec1到vec2的值。

While the call to vec2 sets the capacity of vector vec2, the copying of data to vec2 seems to not fill in the values from vec1 to vec2.

使用对push_back()的调用替换copy()函数正常工作。

Replacing the copy() function with calls to push_back() works as expected.

我在这里缺少什么?

感谢您的帮助。

编译程序:gcc 3.4.4 on cygwin。

Compiler: gcc 3.4.4 on cygwin.

/**
 * vectest.cpp
 */

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> vec1;
    vector<int> vec2;

    vec1.push_back(1);
    vec1.push_back(2);
    vec1.push_back(3);
    vec1.push_back(4);
    vec1.push_back(5);
    vec1.push_back(6);
    vec1.push_back(7);

    vec2.reserve( vec1.size() );
    copy(vec1.begin(), vec1.end(), vec2.begin());

    cout << "vec1.size()     = " << vec1.size() << endl;
    cout << "vec1.capacity() = " << vec1.capacity() << endl;

    cout << "vec1: ";
    for( vector<int>::const_iterator iter = vec1.begin(); iter < vec1.end(); ++iter ) {
        cout << *iter << " ";
    }
    cout << endl;

    cout << "vec2.size()     = " << vec2.size() << endl;
    cout << "vec2.capacity() = " << vec2.capacity() << endl;
    cout << "vec2: ";
    for( vector<int>::const_iterator iter = vec2.begin(); iter < vec2.end(); ++iter ) {
        cout << *iter << endl;
    }

    cout << endl;
}


输出:

vec1.size()     = 7
vec1.capacity() = 8
vec1: 1 2 3 4 5 6 7 
vec2.size()     = 0
vec2.capacity() = 7
vec2:


推荐答案

如其他答案和注释中所述,您应该使用vector的内置功能。但是:

As noted in other answers and comments, you should just use vector's built-in functionality for this. But:

当您 reserve()元素时,向量将分配足够的空间许多元素。元素不存在于向量中,但内存已准备好使用。这样可能会加快 push_back(),因为内存已分配。

When you reserve() elements, the vector will allocate enough space for (at least?) that many elements. The elements do not exist in the vector, but the memory is ready to be used. This will then possibly speed up push_back() because the memory is already allocated.

当您 resize()向量,它将为这些元素分配足够的空间 ,但也将它们添加到向量

When you resize() the vector, it will allocate enough space for those elements, but also add them to the vector.

因此,如果您将向量调整为100,您可以访问元素0 - 99,但如果您保留100个元素,则它们不会被插入,只是可以使用。

So if you resize a vector to 100, you can access elements 0 - 99, but if you reserve 100 elements, they are not inserted yet, just ready to be used.

你想要的是这样的:

vec2.reserve( vec1.size() );
copy(vec1.begin(), vec1.end(), std::back_inserter(vec2));

std :: back_inserter std / iterator /rel =nofollow> < iterator>

std::back_inserter is defined in <iterator>

这篇关于STL向量reserve()和copy()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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