日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/11 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 allocationsize = 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 reservesize = arr2.capacity();arr2.reserve(50); // use of reserve functioncout << "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

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的stl reserve_vector :: reserve()函数以及C ++ STL中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。