在线文档教程
C++
容器 | Containers

std::vector::size

STD::向量::大小

size_type size() const;

返回容器中的元素数,即std::distance(begin(), end())...

参数

%280%29

返回值

容器中的元素数。

例外

(none)(until C++11)
noexcept specification: noexcept(since C++11)

复杂性

常量。

下面的代码使用size若要显示std::vector<int>*

二次

#include <vector> #include <iostream> int main() { std::vector<int> nums {1, 3, 5, 7}; std::cout << "nums contains " << nums.size() << " elements.\n"; }

二次

产出:

二次

nums contains 4 elements.

二次

另见

capacityreturns the number of elements that can be held in currently allocated storage (public member function)
emptychecks whether the container is empty (public member function)
max_sizereturns the maximum possible number of elements (public member function)
resizechanges the number of elements stored (public member function)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cpPreference.com/w/cpp/容器/Vectoral/size