传递STL容器是否复制? [英] Does Passing STL Containers Make A Copy?

查看:82
本文介绍了传递STL容器是否复制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不记得传递一个STL容器是否创建了容器的副本,或者只是另一个别名。如果我有一个几个容器:

I can't remember whether passing an STL container makes a copy of the container, or just another alias. If I have a couple containers:

std::unordered_map<int,std::string> _hashStuff;
std::vector<char> _characterStuff;

我想把这些变量传递给一个函数, p>

And I want to pass those variables to a function, can I make the function as so:

void SomeClass::someFunction(std::vector<char> characterStuff);

或者这会创建 unordered_map / 矢量?我想我可能需要使用 shared_ptr

Or would this make a copy of the unordered_map / vector? I'm thinking I might need to use shared_ptr.

void SomeClass::someFunction(std::shared_ptr<std::vector<char>> characterStuff);


推荐答案

如果你在你的函数的输入中传递一个 lvalue (在实践中,如果你传递的东西的名字,运算符&的地址可以应用),那么复制构造函数

It depends. If you are passing an lvalue in input to your function (in practice, if you are passing something that has a name, to which the address-of operator & can be applied) then the copy constructor of your class will be invoked.

void foo(vector<char> v)
{
    ...
}

int bar()
{
    vector<char> myChars = { 'a', 'b', 'c' };
    foo(myChars); // myChars gets COPIED
}



如果你传递一个(大致上,没有名称,并且操作符&的地址不能被应用的东西),并且类具有移动构造函数,则对象将被移动(其不是,小心,相同作为创建别名,而是将对象的内容转换为新的骨架,使得之前的骨架无用)。

If you are passing an rvalue (roughly, something that doesn't have a name and to which the address-of operator & cannot be applied) and the class has a move constructor, then the object will be moved (which is not, beware, the same as creating an "alias", but rather transferring the guts of the object into a new skeleton, making the previous skeleton useless).

foo()的调用中, make_vector / code>是一个右值。因此,当在 foo()(即向量移动 $ c>的移动构造函数将被调用):

In the invocation of foo() below, the result of make_vector() is an rvalue. Therefore, the object it returns is being moved when given in input to foo() (i.e. vector's move constructor will be invoked):

void foo(vector<char> v);
{
    ...
}

vector<char> make_vector() 
{ 
    ...
};

int bar()
{
    foo(make_vector()); // myChars gets MOVED
}

一些STL类有一个移动构造函数,没有复制构造函数,因为它们本来就是不可复制的(例如 unique_ptr )。

Some STL classes have a move constructor but do not have a copy constructor, because they inherently are meant to be non-copiable (for instance, unique_ptr). You won't get a copy of a unique_ptr when you pass it to a function.

即使对于这些类,您也不会得到 unique_ptr 的副本有一个复制构造函数,你仍然可以强制移动语义通过使用 std :: move 函数将您的参数从左值更改为右值,但同样, t创建一个别名,它只是将对象的所有权转移到你正在调用的函数。这意味着你不能对原始对象做任何其他事情,而不是重新分配给它另一个值或者它被销毁。

Even for those classes that do have a copy constructor, you can still force move semantics by using the std::move function to change your argument from an lvalue into an rvalue, but again that doesn't create an alias, it just transfers the ownership of the object to the function you are invoking. This means that you won't be able to do anything else with the original object other than reassigning to it another value or having it destroyed.

例如:

void foo(vector<char> v)
{
    ...
}

vector<char> make_vector() 
{ 
    ...
};

int bar()
{
    vector<char> myChars = { 'a', 'b', 'c' };
    foo(move(myChars)); // myChars gets MOVED
    cout << myChars.size(); // ERROR! object myChars has been moved
    myChars = make_vector(); // OK, you can assign another vector to myChars
}

主体的左值和右值引用和移动语义模糊,这是非常容易理解的。我个人觉得本教程非常有帮助:

If you find this whole subject of lvalue and rvalue references and move semantics obscure, that's very understandable. I personally found this tutorial quite helpful:

http:// thbecker .net / articles / rvalue_references / section_01.html

您应该也可以在 http://www.isocpp.org 或YouTube上(查找Scott Meyers的研讨会)。

You should be able to find some info also on http://www.isocpp.org or on YouTube (look for seminars by Scott Meyers).

这篇关于传递STL容器是否复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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