向量的 Reserve() 的奇怪之处 [英] Weirdness of the reserve() of vector

查看:32
本文介绍了向量的 Reserve() 的奇怪之处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码来测试向量从另一个向量构造时的容量值.这是受到此处.

I wrote the following code to test the value of capacity of a vector when it was constructed from another. This was inspired by a question asked here.

#include <iostream>
#include<vector>
int main()
{
    std::vector<int> a;
    a.reserve(65536);
    a[0]=10;
    std::vector<int> b(a);  //NOTE: b is constructed from a
    std::cout<<a[0]<<"  " <<b[0];
}

程序编译成功,但运行时报错如下

The Program compiled successfully, but when it was run it threw the following error

 Error during execution:
         Segmentation Fault(core dumped).

然后为了检查该值是否已初始化为vector a,我将代码更改为如下所示:

Then to check that the value was initialized to vector a, I changed the code as shown below:

#include <iostream>
#include<vector>
int main()
{
    std::vector<int> a(65536);
    a[0]=10;
    std::vector<int> b(a);  //NOTE: b is constructed from a
    std::cout<<a[0];
}

这次程序成功运行,没有错误,输出如下:

This time the program ran successfully without errors and gave the output as:

Output: 
      10

然后我尝试将其更改为以下代码:

I then tried to changed it to the following code :

#include <iostream>
#include<vector>
int main()
{
    std::vector<int> a(65536);
    a[0]=10;
    std::vector<int> b(a);  //NOTE: b is constructed from a
    std::cout<<a[0]<<"  " <<b[0];
}

令我惊讶的是,以下输出符合预期:

and to my surprise the following output came as expected :

Output:
     10  10

我不明白为什么当向量在构造函数中用它的大小初始化时程序能成功运行,但在使用 reserve() 给定它的容量时却不能.

I could not understand why the program ran successfully when the vector was initialized with it's size in the constructor but not when it's capacity was given using reserve().

我还了解到使用 reserve() 的向量保留容量不会初始化向量的大小.下面的代码也让我更加困惑.

I also came to know that the reserving capacity for vector using reserve() does not initialize the size of the vector. The following code also confused me further.

#include <iostream>
#include<vector>

int main()
{
    std::vector<int> a;
    a.reserve(1);
    for(int i=0;i<5;i++)
        a[i]=i;
    std::vector<int> b(a);  //NOTE: b is constructed from a
    for(int i=0;i<5;i++)
    std::cout<<a[i]<<"  ";
    std::cout<<"\n"<<a.size()<<"  "<<a.capacity();
 }

执行时显示以下奇怪的输出:

When executed it displays the following weird output:

Output:
     0  1  2  3  4
     0  1

向量存储了 5 个值,但向量的大小保持为 0.

The vector stored the 5 values but the size of the vector remained 0.

我无法理解矢量的这种行为.大小不变怎么能取值.

I couldn't understand this behaviour of vector. How could it take in values with the size not changing.

推荐答案

您正在混淆容量和大小.容量只是一个实现特性.它允许您通过指定向量最终包含多少元素来优化内存分配,因此其实现不必在向量每次增长时重新分配内存(实际增长策略也是一个实现细节).

You're mixing up capacity and size. Capacity is just an implementation feature. It allows you to optimize memory allocation by specifying how many elements the vector will contain in the end, so its implementation doesn't have to re-allocate the memory each time the vector grows (the actual growth strategy is also an implementation detail).

大小是合乎逻辑的.它定义了实际存在的元素数量.它绝不能超过容量(实际上,如果向量调整大小超过容量,容量会根据需要增长).

Size is a logical thing. It defines how many elements are actually there. It must never exceed the capacity (and indeed the capacity will grow as necessary if the vector is resized beyond it).

访问 0 .. size - 1 范围之外的元素是非法的,无论容量如何,都可能会或可能不会中断,这取决于您的运气.这称为未定义行为.

Accessing elements outside of the 0 .. size - 1 range is illegal and may or may not break depending on your luck irregardless of whatever the capacity is. That's called undefined behavior.

因此,当您调用方法或使用构造函数时,您必须弄清楚它是设置容量还是大小.

So when you call a method or use a constructor you have to figure out whether it sets capacity or size.

这篇关于向量的 Reserve() 的奇怪之处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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