简体   繁体   English

C ++中的容器与智能指针

[英]Containers vs Smart pointers in C++

How can I decide when choosing between std::containers ( std::vector or std::array ) and smart pointers pointing to arrays 在std :: containers( std::vectorstd::array )和指向数组的智能指针之间进行选择时,如何确定

I know containers are objects for memory managment. 我知道容器是用于内存管理的对象。 They are exception safe and there will not be any memory leak and they also provide veriuty of functions for memory managment(push.back etc) and smart pointers are pointer that also do not leak memory because they delete themsefs when they are not needed anymore(like unique_ptr when geting out of scope). 它们是异常安全的,不会有任何内存泄漏,并且它们还提供了用于内存管理的大量功能(push.back等),并且智能指针也是不会泄漏内存的指针,因为它们在不再需要时会删除它们就像超出范围时的unique_ptr一样)。 Propably in containers there is an overhead every time they are created. 可能在容器中,每次创建容器时都会产生开销。

My question is how can i decide which method to use and why. 我的问题是我该如何决定使用哪种方法以及为什么。

std::vector <unsigned char>myArray(3 * outputImageHight * outputImageWidth);

std::unique_ptr<unsigned char[]>myArray(new unsigned char[3 * outputImageHight * outputImageWidth]);

I would use the vector. 我会使用向量。 Your pointer version offers basically no improvements over the vector, and you lose a lot of useful functionality. 您的指针版本基本上没有改进向量,并且您失去了很多有用的功能。 You're most likely going to need to measure the size and iterate your array at some point, with a vector you get this for free, whereas you'd need to implement it yourself for your pointer version; 您很可能需要在某个点上测量大小并迭代数组,使用向量可以免费获得它,而您需要针对指针版本自己实现它。 at which point you may as well have just used the vector to begin with. 在这一点上,您可能也只是使用向量开始的。

There may be a performance cost instantiating the vector, but I doubt that it would be a bottleneck for most applications. 实例化向量可能会降低性能,但是我怀疑这对于大多数应用程序来说是瓶颈。 If you're creating so many vectors that instantiating them is costing you time, you can probably be smarter about managing them (pooling your memory, custom vector allocators, etc). 如果创建的向量太多,实例化它们会浪费您的时间,那么您可能会更聪明地管理它们(池化内存,自定义向量分配器等)。 If in doubt, measure. 如有疑问,请测量。

One example where you might need to use the unique_ptr<> version might be if you're working with a library written in C where you lose ownership of the array. 您可能需要使用unique_ptr<>版本的一个示例是,如果您正在使用用C编写的库,而该库失去了数组的所有权。 For example: 例如:

std::unique_ptr<unsigned char[]>myArray(
    new unsigned char[3 * outputImageHight * outputImageWidth]);

my_c_lib_data_t cLibData;
int result = my_c_lib_set_image(cLibData, myArray);

if (MYLIB_SUCCESS == result)
    // mylib successfully took ownership of the char array, so release the pointer.
    myArray.release();

If you have the choice though, prefer to use C++ style containers where you can. 不过,如果您有选择的话,请尽可能使用C ++样式的容器。

std::vector , primarily because it better represents the "sequence of items in contiguous memory", it is the default representation for that, and enables a wide range of common operations. std::vector ,主要是因为它更好地表示了“连续内存中项目的顺序”,这是它的默认表示,并启用了广泛的常用操作。

vector has move semantics, so the benefit of std::unique_ptr is limited. vector具有移动语义,因此std :: unique_ptr的好处是有限的。

If you are lucky, your STL implementation `provides small vector optimization, skipping the memory allocation for small sizes. 如果幸运的话,您的STL实现`提供了小向量优化,跳过了小尺寸的内存分配。
-- edit: I wasn't aware SBO is apparently prohibited by the standard - sorry for getting your hopes up, thanks @KarlNicholl - 编辑:我不知道该标准显然禁止SBO-非常感谢您的指望,谢谢@KarlNicholl

If pointer semantics are required, a unique_ptr<vector<T>> or shared_ptr<vector<T>> is a valid choice with little overhead. 如果需要指针语义,则unique_ptr<vector<T>>shared_ptr<vector<T>>是有效的选择,开销很小。

boost did introduce shared_array etc., that represent your second option better, but I haven't seen them get much traction. boost确实引入了shared_array等,这更好地代表了您的第二个选择,但是我还没有看到它们有多少吸引力。

Always use STL containers except in situation where you have good reason to use pointers. 除非有充分的理由使用指针,否则请始终使用STL容器。 Reasons are reliability and readability, IMO. 原因是可靠性和可读性,IMO。

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

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