stl reserve_vector :: reserve()函数以及C ++ STL中的示例

stl reserve

C ++ vector :: reserve()函数 (C++ vector::reserve() function)

vector::reserve() is a library function of "vector" header, which is used to request change in vector allocation. Refer to example to understand in details.

vector :: reserve()“ vector”头的库函数,用于请求矢量分配的更改。 请参阅示例以详细了解。

Note: To use vector, include <vector> header.

注意:要使用向量,请包含<vector>标头。

Syntax of vector::reserve() function

vector :: reserve()函数的语法

    vector::reserve(n);

Parameter(s): int n – It accepts n as a parameter where n be the input capacity.

参数: int n –接受n作为参数,其中n是输入容量。

Return value: void – It returns nothing in case of valid request. But if the capacity requested is greater than the maximum size of the vector (vector::max_size), a length_error exception is thrown.

返回值: void –在有效请求的情况下不返回任何内容。 但是,如果请求的容量大于向量的最大大小( vector :: max_size ),则会引发length_error异常。

Example: Case 1: (without reserve())

示例:情况1 :(没有reserve())

vector<int> arr1; //usual dynamic allocation
size = arr1.capacity();
cout << "arr1 growing with usual dynamic allocation:\n";
for (int i = 0; i < 50; ++i) {
    arr1.push_back(i);
    if (size != arr1.capacity()) {
        size = arr1.capacity();
        cout << "capacity changed to : " << size << '\n';
    }
}

In this case, we have not used reserve thus the growth is as per dynamic allocation, which increases in a factor of two. Like, 1, 2, 4, 8, 16, 32, 64, 128…..so on till max_size.

在这种情况下,我们没有使用储备金,因此增长是按动态分配进行的,增长了两倍。 像1,2,4,8,16,16,32,64,128…..so直到max_size

Example: Case 2: (with reserve())

示例:情况2 :(带有reserve())

vector<int> arr2; //using reserve
size = arr2.capacity();
arr2.reserve(50); // use of reserve function
cout << "arr2 growing with using reverse:\n";
for (int i = 0; i < 50; ++i) {
    arr2.push_back(i);
    if (size != arr2.capacity()) {
        size = arr2.capacity();
        cout << "capacity changed to: " << size << '\n';
    }
}

In this case, we have not used reserve thus the growth is as per dynamic allocation, which increases in a factor of two. Like, 1, 2, 4, 8, 16, 32, 64, 128…..so on till max_size.

在这种情况下,我们没有使用储备金,因此增长是按动态分配进行的,增长了两倍。 像1,2,4,8,16,16,32,64,128…..so直到max_size

C ++程序演示vector :: reserve()函数的示例 (C++ program to demonstrate example of vector::reserve() function)

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int>::size_type size;

    vector<int> arr1; //usual dynamic allocation
    size = arr1.capacity();
    cout << "arr1 growing with usual dynamic allocation:\n";
    for (int i = 0; i < 50; ++i) {
        arr1.push_back(i);
        if (size != arr1.capacity()) {
            size = arr1.capacity();
            cout << "capacity changed to : " << size << '\n';
        }
    }

    vector<int> arr2; //using reserve
    size = arr2.capacity();
    arr2.reserve(50); // use of reserve function
    cout << "arr2 growing with using reverse:\n";
    for (int i = 0; i < 50; ++i) {
        arr2.push_back(i);
        if (size != arr2.capacity()) {
            size = arr2.capacity();
            cout << "capacity changed to: " << size << '\n';
        }
    }

    return 0;
}

Output

输出量

arr1 growing with usual dynamic allocation:
capacity changed to : 1
capacity changed to : 2
capacity changed to : 4
capacity changed to : 8
capacity changed to : 16
capacity changed to : 32
capacity changed to : 64
arr2 growing with using reverse:
capacity changed to: 50

Reference: C++ vector::reserve()

参考: C ++ vector :: reserve()

翻译自: https://www.includehelp.com/stl/vector-reserve-function-with-example.aspx

stl reserve

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值