
- C 標準庫
- C 標準庫
- C++ 標準庫
- C++ 庫 - 首頁
- C++ 庫 - <fstream>
- C++ 庫 - <iomanip>
- C++ 庫 - <ios>
- C++ 庫 - <iosfwd>
- C++ 庫 - <iostream>
- C++ 庫 - <istream>
- C++ 庫 - <ostream>
- C++ 庫 - <sstream>
- C++ 庫 - <streambuf>
- C++ 庫 - <atomic>
- C++ 庫 - <complex>
- C++ 庫 - <exception>
- C++ 庫 - <functional>
- C++ 庫 - <limits>
- C++ 庫 - <locale>
- C++ 庫 - <memory>
- C++ 庫 - <new>
- C++ 庫 - <numeric>
- C++ 庫 - <regex>
- C++ 庫 - <stdexcept>
- C++ 庫 - <string>
- C++ 庫 - <thread>
- C++ 庫 - <tuple>
- C++ 庫 - <typeinfo>
- C++ 庫 - <utility>
- C++ 庫 - <valarray>
- C++ STL 庫
- C++ 庫 - <array>
- C++ 庫 - <bitset>
- C++ 庫 - <deque>
- C++ 庫 - <forward_list>
- C++ 庫 - <list>
- C++ 庫 - <map>
- C++ 庫 - <multimap>
- C++ 庫 - <queue>
- C++ 庫 - <priority_queue>
- C++ 庫 - <set>
- C++ 庫 - <stack>
- C++ 庫 - <unordered_map>
- C++ 庫 - <unordered_set>
- C++ 庫 - <vector>
- C++ 庫 - <algorithm>
- C++ 庫 - <iterator>
- C++ 高階庫
- C++ 庫 - <any>
- C++ 庫 - <barrier>
- C++ 庫 - <bit>
- C++ 庫 - <chrono>
- C++ 庫 - <cinttypes>
- C++ 庫 - <clocale>
- C++ 庫 - <condition_variable>
- C++ 庫 - <coroutine>
- C++ 庫 - <cstdlib>
- C++ 庫 - <cstring>
- C++ 庫 - <cuchar>
- C++ 庫 - <charconv>
- C++ 庫 - <cfenv>
- C++ 庫 - <cmath>
- C++ 庫 - <ccomplex>
- C++ 庫 - <expected>
- C++ 庫 - <format>
- C++ 庫 - <future>
- C++ 庫 - <flat_set>
- C++ 庫 - <flat_map>
- C++ 庫 - <filesystem>
- C++ 庫 - <generator>
- C++ 庫 - <initializer_list>
- C++ 庫 - <latch>
- C++ 庫 - <memory_resource>
- C++ 庫 - <mutex>
- C++ 庫 - <mdspan>
- C++ 庫 - <optional>
- C++ 庫 - <print>
- C++ 庫 - <ratio>
- C++ 庫 - <scoped_allocator>
- C++ 庫 - <semaphore>
- C++ 庫 - <source_location>
- C++ 庫 - <span>
- C++ 庫 - <spanstream>
- C++ 庫 - <stacktrace>
- C++ 庫 - <stop_token>
- C++ 庫 - <syncstream>
- C++ 庫 - <system_error>
- C++ 庫 - <string_view>
- C++ 庫 - <stdatomic>
- C++ 庫 - <variant>
- C++ STL 庫速查表
- C++ STL - 速查表
C++ vector::reserve() 函式
C++ `vector::reserve()` 函式用於預留向量的容量。容量必須足夠大,以便能夠容納 n 個元素。任何給定向量,只要其容量大於或等於我們在 `reserve()` 函式中指定的新的容量,其容量就可以透過 `reserve()` 函式來增加。
`reserve()` 函式只會預留向量的空間;它不會增加向量的大小。如果預留的向量大小大於當前大小,所有這些調整都將失效。`reserve()` 函式的時間複雜度最多為線性。
語法
以下是 C++ `vector::reserve()` 函式的語法:
void reserve (size_type n);
引數
n − 表示將儲存在向量中的元素數量。
示例 1
讓我們考慮以下示例,我們將使用 `reserve()` 函式。
#include <iostream> #include <vector> using namespace std; int main(void) { vector<int> v1; vector<int> v2; ssize_t size; size = v1.capacity(); for (int i = 0; i < 25; ++i) { v1.push_back(i); if (size != v1.capacity()) { size = v1.capacity(); cout << "Expanding vector v1 to hold " << size<< " elements" << endl; } } cout << endl << endl; v2.reserve(25); for (int i = 0; i < 25; ++i) { v2.push_back(i); if (size != v2.capacity()) { size = v2.capacity(); cout << "Expanding vector v2 to hold " << size<< " elements" << endl; } } return 0; }
輸出
編譯並執行上述程式後,將產生以下結果:
Expanding vector v1 to hold 1 elements Expanding vector v1 to hold 2 elements Expanding vector v1 to hold 4 elements Expanding vector v1 to hold 8 elements Expanding vector v1 to hold 16 elements Expanding vector v1 to hold 32 elements Expanding vector v2 to hold 25 elements
示例 2
考慮另一種情況,我們將分配和釋放空間。
#include <cstddef> #include <iostream> #include <new> #include <vector> template<class Tp> struct tutorial{ typedef Tp value_type; tutorial() = default; template<class x> tutorial(const tutorial<x>&) {} Tp* allocate(std::size_t n){ n *= sizeof(Tp); Tp* p = static_cast<Tp*>(::operator new(n)); std::cout << "allocating " << n << " bytes @ " << p << '\n'; return p; } void deallocate(Tp* p, std::size_t n){ std::cout << "deallocating " << n * sizeof *p << " bytes @ " << p << "\n\n"; ::operator delete(p); } }; template<class x, class y> bool operator==(const tutorial<x>&, const tutorial<y>&) { return true; } template<class x, class y> bool operator!=(const tutorial<x>&, const tutorial<y>&) { return false; } int main(){ constexpr int max_elements = 10; std::cout << "using reserve() function : \n"; { std::vector<int, tutorial<int>> v1; v1.reserve(max_elements); for (int n = 0; n < max_elements; ++n) v1.push_back(n); } }
輸出
執行上述程式後,將產生以下結果:
using reserve() function : allocating 40 bytes @ 0x55981828e2c0 deallocating 40 bytes @ 0x55981828e2c0
示例 3
在下面的示例中,我們將檢查使用 `reserve()` 函式和不使用 `reserve()` 函式的情況。
#include <cstddef> #include <iostream> #include <new> #include <vector> template<class a> struct tutorial{ typedef a value_type; tutorial() = default; template<class x> tutorial(const tutorial<x>&) {} a* allocate(std::size_t n){ n *= sizeof(a); a* p = static_cast<a*>(::operator new(n)); std::cout << "allocating " << n << " bytes @ " << p << '\n'; return p; } void deallocate(a* p, std::size_t n){ std::cout << "deallocating " << n * sizeof *p << " bytes @ " << p << "\n\n"; ::operator delete(p); } }; template<class x, class y> bool operator==(const tutorial<x>&, const tutorial<y>&) { return true; } template<class x, class y> bool operator!=(const tutorial<x>&, const tutorial<y>&) { return false;} int main(){ constexpr int max_elements = 8; std::cout << "using reserve() function: \n"; { std::vector<int, tutorial<int>> v1; v1.reserve(max_elements); for (int n = 0; n < max_elements; ++n) v1.push_back(n); } std::cout << "without reserve() function : \n"; { std::vector<int, tutorial<int>> v1; for (int n = 0; n < max_elements; ++n){ if (v1.size() == v1.capacity())std::cout << "size() == capacity() == " << v1.size() << '\n'; v1.push_back(n); } } }
輸出
執行上述程式後,將產生以下結果:
using reserve() function: allocating 32 bytes @ 0x55d7dd6ca2c0 deallocating 32 bytes @ 0x55d7dd6ca2c0 without reserve() function : size() == capacity() == 0 allocating 4 bytes @ 0x55d7dd6ca2f0 size() == capacity() == 1 allocating 8 bytes @ 0x55d7dd6ca310 deallocating 4 bytes @ 0x55d7dd6ca2f0 size() == capacity() == 2 allocating 16 bytes @ 0x55d7dd6ca2f0 deallocating 8 bytes @ 0x55d7dd6ca310 size() == capacity() == 4 allocating 32 bytes @ 0x55d7dd6ca2c0 deallocating 16 bytes @ 0x55d7dd6ca2f0 deallocating 32 bytes @ 0x55d7dd6ca2c0
廣告