(C++17) | ||||
| Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
| Associative | ||||
| Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
| Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Views | ||||
(C++20) | ||||
(C++23) | ||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
| Member types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Non-member functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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.
Contents |
(none)
Capacity of the currently allocated storage.
Constant.
#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 inf2 2 23 4 25 8 29 16 217 32 233 64 265 128 2 Final size: 100, capacity: 128
| returns the number of elements (public member function)[edit] | |
| reserves storage (public member function)[edit] |