Difference between revisions of "Template:cpp/container/capacity"
From cppreference.com
m (→Example: fix) |
m (update example to show -- operator and not --> (also shorten output)) |
||
Line 36: | Line 36: | ||
int main() | int main() | ||
{ | { | ||
− | int sz = | + | int sz = 100; |
std::vector<int> v; | std::vector<int> v; | ||
auto cap = v.capacity(); | auto cap = v.capacity(); | ||
− | std::cout << "initial capacity=" << cap << '\n'; | + | std::cout << "initial size=" << v.size(); |
+ | std::cout << " capacity=" << cap << '\n'; | ||
std::cout << "\nDemonstrate the capacity's growth policy." | std::cout << "\nDemonstrate the capacity's growth policy." | ||
"\nSize: Capacity: Ratio:\n" << std::left; | "\nSize: Capacity: Ratio:\n" << std::left; | ||
− | while (sz --> 0) { | + | while (sz-- > 0) { |
v.push_back(sz); | v.push_back(sz); | ||
if (cap != v.capacity()) { | if (cap != v.capacity()) { | ||
Line 54: | Line 55: | ||
} | } | ||
− | std::cout << "\nfinal size=" << v.size() | + | std::cout << "\nfinal size=" << v.size(); |
− | std::cout << " | + | std::cout << " capacity=" << v.capacity() << '\n'; |
} | } | ||
}} | }} | ||
| p=true | | p=true | ||
| output= | | output= | ||
− | initial capacity=0 | + | initial size=200 capacity=0 |
Demonstrate the capacity's growth policy. | Demonstrate the capacity's growth policy. | ||
Line 72: | Line 73: | ||
33 64 2 | 33 64 2 | ||
65 128 2 | 65 128 2 | ||
− | |||
− | final size=200 | + | final size=200 capacity=256 |
− | + | ||
}} | }} | ||
Revision as of 14:02, 22 January 2022
Returns the number of elements that the container has currently allocated space for.
Contents |
Parameters
(none)
Return value
Capacity of the currently allocated storage.
Complexity
Constant.
Example
This section is incomplete Reason: no example |
See also
returns the number of elements (public member function of std::{{{1}}} )
| |
reserves storage (public member function of std::{{{1}}} )
|