简体   繁体   English

std :: vector调整大小算法

[英]std::vector resize algorithm

I'm actually trying to implement a simple copy of std::vector. 我实际上是在尝试实现std :: vector的简单副本。 During my tests to see if my data is consistent by comparing with the original one I noticed something. 在测试期间,通过与原始数据进行比较来查看我的数据是否一致,我注意到了一些东西。

std::vector<std::string> *v = new std::vector<std::string>(2);
std::string str1("Hello");
std::string str2("World");

v->push_back(str1);
v->push_back(str2);

v->resize(5);

for (std::vector<std::string>::const_iterator it = v->begin(); it != v->end(); it++)
{
    std::cout << "/" << (*it) << "/" << std::endl;
}

here is the result : 结果如下:

//
//
/Hello/
/World/
//

Can someone explain me why resize doesn't append std::string()'s like this : 有人可以解释一下为什么调整大小后不附加std :: string()的原因:

/Hello/
/World/
//
//
//

What is the algorithm behind? 背后的算法是什么?

The key is here: 关键在这里:

std::vector<std::string> *v = new std::vector<std::string>(2);

This creates a vector with 2 elements, which are default-constructed (meaning - two empty strings). 这将创建一个包含2个元素的向量,这些元素是默认构造的(意味着-两个空字符串)。 Then you push_back Hello and World . 然后您将push_back HelloWorld Now you have 4 elements. 现在您有4个元素。 Then the resize( 5 ) adds just one more element (also default-constructed). 然后, resize( 5 )仅添加一个元素(也是默认构造的)。

I guess you wanted to manipulate/increase the capacity? 我想您想操纵/增加容量? You need std::vector::reserve instead. 您需要改为std::vector::reserve


You should create an empty vector and then use push_back s to see the behavior, you expect. 您应该创建一个空向量,然后使用push_back看到预期的行为。 Or just use operator[] instead of the two push_back s. 或者只使用operator[]而不是两个push_back


Is there a really good reason to create the vector on the heap, instead of on the stack? 是否有充分的理由在堆上而不是在堆栈上创建向量? You (almost?) always should avoid this. 您(几乎?)总是应该避免这种情况。

You are constructing a std::vector with 2 items inside, default constructed. 您正在构建一个std :: vector,其中包含2个项目,默认构造。
Reference: std::vector constructors 参考: std :: vector构造函数

Replace: 更换:

std::vector<std::string> *v = new std::vector<std::string>(2);

with: 与:

std::vector<std::string> *v = new std::vector<std::string>();

And, by the way, why are you allocating the vector on the heap with new? 而且,顺便说一句,为什么要用new在堆上分配向量? Just declare it as: 只需声明为:

std::vector<std::string> v;

In the construction of vector you passed 2 as argument. 在向量的构造中,您传递了2作为参数。 If you take a look at that construction you will see this: 如果您查看该构造,您将看到以下内容:

explicit vector(size_type _Count)

Here you are creating a vector with two default constructed string in it and then push two other string into vector. 在这里,您将创建一个带有两个默认构造字符串的向量,然后将另外两个字符串推入向量。 Use default construction and then use std::vector::reserve to increase the capacity of vector. 使用默认构造,然后使用std::vector::reserve增加vector的容量。

To get the result that you are wanting to see, try the following: 要获得想要的结果,请尝试以下操作:

std::vector<std::string> *v = new std::vector<std::string>({"s1","s2"});

Then go about your resizing and looping. 然后进行大小调整和循环。 This should give you the expected padding as you have suggested. 如您所建议的那样,这应该为您提供预期的填充。

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

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