std::vector<T,Allocator>::capacity
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Containers library
(C++17)
(C++11)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++23)
(C++23)
(C++20)
(C++23)
Tables
std::vector
(C++23)
(C++11)
(C++11)
(C++11)
(C++11)
vector::capacity
(DR*)
(C++23)
(C++23)
(C++11)
(C++11)
(C++20)
(C++20)(C++20)
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)
Deduction guides (C++17)
size_type capacity() const;
(noexcept since C++11)(constexpr since C++20)
Returns the number of elements that the container has currently allocated space for.
[edit] Parameters
(none)
[edit] Return value
Capacity of the currently allocated storage.
[edit] Complexity
Constant.
[edit] Example
Run this code
#include <iomanip> #include <iostream> #include <vector> int main() { int sz = 100; std::vector <int> v; auto cap = v.capacity(); std::cout << "Initial size: " << v.size() << ", capacity: " << cap << '\n'; std::cout << "\nDemonstrate the capacity's growth policy." "\nSize: Capacity: Ratio:\n" << std::left ; while (sz-- > 0) { v.push_back(sz); if (cap != v.capacity()) { std::cout << std::setw ( 7) << v.size() << std::setw (11) << v.capacity() << std::setw (10) << v.capacity() / static_cast<float>(cap) << '\n'; cap = v.capacity(); } } std::cout << "\nFinal size: " << v.size() << ", capacity: " << v.capacity() << '\n'; }
Possible output:
Initial size: 0, capacity: 0 Demonstrate the capacity's growth policy. Size: Capacity: Ratio: 1 1 inf 2 2 2 3 4 2 5 8 2 9 16 2 17 32 2 33 64 2 65 128 2 Final size: 100, capacity: 128