简体   繁体   English

C ++ 11如何改变了标准容器?

[英]How C++11 has changed the standard containers?

I get that C++ 11 has introduced the new move semantics and as consequence data containers are changed to meet the new definitions and specifications of the language, I don't really get how the standard containers benefits from them. 我知道C ++ 11引入了新的move语义,结果数据容器被更改为满足该语言的新定义和规范,我并没有真正理解标准容器如何从中受益。

Also I think that I have got what an Rvalue is and how the move semantics act, the problem is i don't see any useful point about this, moving things and changing their labels doesn't sound like a meaningful feature. 另外我认为我已经了解了Rvalue是什么以及移动语义如何起作用,问题是我没有看到任何有用的要点,移动事物和更改其标签听起来并不像是有意义的功能。

I can ask for a good resource about how map, list, vector, ... are changing in the new C++11 ? 我可以要求一个很好的资源,以了解新的C ++ 11中的map, list, vector, ...如何变化?

You can check out the containers' descriptions at cppreference , which explains the interface of STL. 您可以在cppreference上查看容器的说明,该说明解释了STL的接口。 There, you can find what the C++11 standard adds - it is marked with ' (since C++11) ' tag. 在这里,您可以找到C ++ 11标准添加的内容-它带有' (since C++11)标记(since C++11)的标记。 If you click on a container type, it will show you which methods are new for it. 如果单击容器类型,它将为您显示哪些新方法。

The reason RValues and LValues were introduced is it can be much faster to move object data than it is to perform a copy on it. 引入RValues和LValues的原因是,移动对象数据比对其执行复制要快得多。 The performance gain is mainly because of internally stored pointers that do not need to be replicated during a move, which would otherwise involve needless malloc calls and memcpys. 性能的提高主要是由于内部存储的指针在移动过程中不需要复制,否则将涉及不必要的malloc调用和memcpys。 For instance, std::string contains a pointer to a char array that can be very large. 例如, std::string包含一个指向非常大的char数组的指针。 Copying it would involve copying the data in that char array, moving simply involves copying the pointer to that data. 复制将涉及复制该char数组中的数据,移动仅涉及复制指向该数据的指针。

With respect to LValues and RValues, the only things, of which I'm aware, that have changed, is now we have a shiny new constructor to play with, and many of the member functions have been rewritten to take advantage of move semantics. 关于LValues和RValues,我所知道的唯一改变了,现在我们可以使用一个闪亮的新构造函数,并且重写了许多成员函数以利用move语义。

For instance, std::vector now has a std::vector::vector(std::vector&& move) ctor, and functions like push_back have been changed to also accept RValues. 例如, std::vector现在有一个std::vector::vector(std::vector&& move) ctor,并且push_back功能已更改为也接受RValues。

This should be, for the most part, seemless to you. 在大多数情况下,这对您来说应该是看不到的。 If you're writing a library, rather than just using one, you need to know this, and URefs too. 如果您正在编写一个库,而不仅仅是使用一个库,则需要知道这一点,并且也需要知道URefs。

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

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