将数据放入C ++中的std :: vector是否会创建数据的副本?

时间:2022-04-17 16:05:50

I am interested if creating a new std::vector (or calling its assign method) creates a copy of the data?

我感兴趣的是,如果创建一个新的std :: vector(或调用其assign方法)创建数据的副本?

For example,

void fun(char *input) {
    std::vector<char> v(input, input+strlen(input));
    // is it safe to assume that the data input points to was COPIED into v?
}

5 个解决方案

#1


11  

Yes. Elements are always copied into or out of STL containers. (At least until move semantics are added in C++0x)

是。元素始终复制到STL容器中或从STL容器中复制出来。 (至少在C ++ 0x中添加移动语义之前)

EDIT: Here's how you can test for copying yourself:

编辑:这是你如何测试自己复制:

#include <vector>
#include <iostream>

class CopyChecker
{
public:
    CopyChecker()
    {
        std::cout << "Hey, look! A new copy checker!" << std::endl;
    }
    CopyChecker(const CopyChecker& other)
    {
        std::cout << "I'm the copy checker! No, I am! Wait, the"
            " two of us are the same!" << std::endl;
    }
    ~CopyChecker()
    {
        std::cout << "Erroap=02-0304-231~No Carrier" << std::endl;
    }
};

int main()
{
    std::vector<CopyChecker> doICopy;
    doICopy.push_back(CopyChecker());
}

The output should be:

输出应该是:

Hey, look! A new copy checker!
I'm the copy checker! No, I am! Wait, the two of us are the same!
Erroap=02-0304-231~No Carrier
Erroap=02-0304-231~No Carrier

你看!一个新的复制检查器!我是复制检查员!不,我是!等等,我们两个是一样的! Erroap = 02-0304-231~无载流子错误= 02-0304-231~无载波

#2


9  

Elements are always copied into or out of STL containers.

元素始终复制到STL容器中或从STL容器中复制出来。

Although the element may just be a pointer, in which case the pointer is copied but not the underlying data

虽然元素可能只是一个指针,但在这种情况下指针被复制但不复制基础数据

#3


1  

About the move semantics, here is how you could move the contents in C++0x if you wanted to:

关于移动语义,如果你想要,你可以在这里移动C ++ 0x中的内容:

void fun_move(char *input)
{
    std::vector<char> v;
    auto len = strlen(input);
    v.reserve(len);
    std::move(input, input+len, std::back_inserter(v));
}

#4


0  

If you want your data to be moved, use std::swap_ranges, but you have to allocate for memory first :

如果要移动数据,请使用std :: swap_ranges,但必须先为内存分配:

vector<T> v;
v.reserve(std::distance(beg, end));
std::swap_ranges(beg, end, v.begin());

#5


0  

If you do not want the object copy semantics, then you can create a vector of pointers-to-objects instead so that only the pointer is copied. However you then have to ensure that the pointers then remain valid for the lifetime of the container.

如果您不想要对象复制语义,则可以创建指向对象的向量,以便仅复制指针。但是,您必须确保指针在容器的生命周期内保持有效。

#1


11  

Yes. Elements are always copied into or out of STL containers. (At least until move semantics are added in C++0x)

是。元素始终复制到STL容器中或从STL容器中复制出来。 (至少在C ++ 0x中添加移动语义之前)

EDIT: Here's how you can test for copying yourself:

编辑:这是你如何测试自己复制:

#include <vector>
#include <iostream>

class CopyChecker
{
public:
    CopyChecker()
    {
        std::cout << "Hey, look! A new copy checker!" << std::endl;
    }
    CopyChecker(const CopyChecker& other)
    {
        std::cout << "I'm the copy checker! No, I am! Wait, the"
            " two of us are the same!" << std::endl;
    }
    ~CopyChecker()
    {
        std::cout << "Erroap=02-0304-231~No Carrier" << std::endl;
    }
};

int main()
{
    std::vector<CopyChecker> doICopy;
    doICopy.push_back(CopyChecker());
}

The output should be:

输出应该是:

Hey, look! A new copy checker!
I'm the copy checker! No, I am! Wait, the two of us are the same!
Erroap=02-0304-231~No Carrier
Erroap=02-0304-231~No Carrier

你看!一个新的复制检查器!我是复制检查员!不,我是!等等,我们两个是一样的! Erroap = 02-0304-231~无载流子错误= 02-0304-231~无载波

#2


9  

Elements are always copied into or out of STL containers.

元素始终复制到STL容器中或从STL容器中复制出来。

Although the element may just be a pointer, in which case the pointer is copied but not the underlying data

虽然元素可能只是一个指针,但在这种情况下指针被复制但不复制基础数据

#3


1  

About the move semantics, here is how you could move the contents in C++0x if you wanted to:

关于移动语义,如果你想要,你可以在这里移动C ++ 0x中的内容:

void fun_move(char *input)
{
    std::vector<char> v;
    auto len = strlen(input);
    v.reserve(len);
    std::move(input, input+len, std::back_inserter(v));
}

#4


0  

If you want your data to be moved, use std::swap_ranges, but you have to allocate for memory first :

如果要移动数据,请使用std :: swap_ranges,但必须先为内存分配:

vector<T> v;
v.reserve(std::distance(beg, end));
std::swap_ranges(beg, end, v.begin());

#5


0  

If you do not want the object copy semantics, then you can create a vector of pointers-to-objects instead so that only the pointer is copied. However you then have to ensure that the pointers then remain valid for the lifetime of the container.

如果您不想要对象复制语义,则可以创建指向对象的向量,以便仅复制指针。但是,您必须确保指针在容器的生命周期内保持有效。