stl reserve_vector :: reserve()函数以及C ++ STL中的示例
stl reserve
C ++ vector :: reserve()函數(shù) (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”頭的庫函數(shù),用于請(qǐng)求矢量分配的更改。 請(qǐng)參閱示例以詳細(xì)了解。
Note: To use vector, include <vector> header.
注意:要使用向量,請(qǐng)包含<vector>標(biāo)頭。
Syntax of vector::reserve() function
vector :: reserve()函數(shù)的語法
vector::reserve(n);Parameter(s): int n – It accepts n as a parameter where n be the input capacity.
參數(shù): int n –接受n作為參數(shù),其中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 –在有效請(qǐng)求的情況下不返回任何內(nèi)容。 但是,如果請(qǐng)求的容量大于向量的最大大小( vector :: max_size ),則會(huì)引發(fā)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.
在這種情況下,我們沒有使用儲(chǔ)備金,因此增長是按動(dòng)態(tài)分配進(jìn)行的,增長了兩倍。 像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.
在這種情況下,我們沒有使用儲(chǔ)備金,因此增長是按動(dòng)態(tài)分配進(jìn)行的,增長了兩倍。 像1,2,4,8,16,16,32,64,128…..so直到max_size 。
C ++程序演示vector :: reserve()函數(shù)的示例 (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: 50Reference: C++ vector::reserve()
參考: C ++ vector :: reserve()
翻譯自: https://www.includehelp.com/stl/vector-reserve-function-with-example.aspx
stl reserve
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的stl reserve_vector :: reserve()函数以及C ++ STL中的示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 编码 解码 读写文件
- 下一篇: 解析取值_圆锥曲线——高中解析几何全归纳