Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/array/size"

From cppreference.com
< cpp‎ | container‎ | array
(std::array size is constexpr. And noexcept.)
(Undo revision 8624 by Korval (talk) apply the changes to the template)
Line 1: Line 1:
{{cpp/container/array/title | size}}
+
{{cpp/container/size|array}}
{{cpp/container/array/sidebar}}
+
{{ddcl | notes={{cpp/container/mark c++11 feature | array }} |
+
constexpr size_type size() noexcept;
+
}}
+
 
+
Returns the number of elements in the container, i.e. {{cpp|std::distance(begin(), end())}}.
+
 
+
===Parameters===
+
{{param none}}
+
 
+
===Return value===
+
 
+
the number of elements in the container
+
 
+
===Complexity===
+
{{complex constant}}
+
 
+
===See also===
+
 
+
{{dcl list begin}}
+
{{#ifeq: array | vector | {{dcl list template | cpp/container/dcl list capacity |array}} }}
+
{{dcl list template | cpp/container/dcl list empty |array}}
+
{{dcl list template | cpp/container/dcl list max_size |array}}
+
{{#switch:array | vector | deque | list | forward_list={{dcl list template | cpp/container/dcl list resize |array}} }}
+
{{dcl list end}}
+

Revision as of 15:07, 14 August 2011

 
 
 
 
constexpr size_type size() const noexcept;
(since C++11)

Returns the number of elements in the container, i.e. std::distance(begin(), end()).

Contents

Parameters

(none)

Return value

The number of elements in the container.

Complexity

Constant.

Example

The following code uses size to display the number of elements in a std::array:

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

Output:

nums contains 4 elements.

See also

checks whether the container is empty
(public member function) [edit]
returns the maximum possible number of elements
(public member function) [edit]
(C++17)(C++20)
returns the size of a container or array
(function template) [edit]