Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/container/size"

From cppreference.com
m (fmt)
m (+inplace_vector.)
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
{{cpp/container/{{{1|}}}/title|size}}
+
{{#vardefine:cont|{{{1|inplace_vector}}}}}<!--
{{cpp/container/{{{1|}}}/navbar}}
+
-->{{cpp/container/{{#var:cont}}/title|size}}
 +
{{cpp/container/{{#var:cont}}/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{#switch:{{{1|}}}|array=
+
{{#switch:{{#var:cont}}
 +
|array=
 
{{dcl|since=c++11|
 
{{dcl|since=c++11|
 
constexpr size_type size() const noexcept;
 
constexpr size_type size() const noexcept;
 
}}
 
}}
 
|vector=
 
|vector=
{{dcl rev multi|
+
{{dcl|notes={{mark noexcept since c++11}}<br>{{mark constexpr since c++20}}|
|dcl1=
+
 
size_type size() const;
 
size_type size() const;
|since2=c++11|dcl2=
+
}}
size_type size() const noexcept;
+
|inplace_vector=
|since3=c++20|dcl3=
+
{{dcl|since=c++26|
constexpr size_type size() const noexcept;
+
constexpr size_type size() const;
 
}}
 
}}
 
|
 
|
{{cpp/container/noexcept dcl|{{{1|}}}|dcl=
+
{{cpp/container/noexcept dcl|{{#var:cont}}|dcl=
 
size_type size() const
 
size_type size() const
 
}}
 
}}
Line 34: Line 35:
  
 
===Example===
 
===Example===
{{include|cpp/container/{{{1|}}}/example_size}}
+
{{#switch:{{#var:cont}}
 +
|set|multiset|unordered_set|unordered_multiset
 +
={{include|cpp/container/set/example size|{{#var:cont}}}}
 +
|map|multimap|unordered_map|unordered_multimap
 +
={{include|cpp/container/map/example size|{{#var:cont}}}}
 +
|{{include|cpp/container/{{#var:cont}}/example size}}
 +
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{#ifeq:{{{1|}}}|vector|{{dsc inc|cpp/container/dsc capacity|{{{1|}}}}}}}
+
{{#switch:{{#var:cont}}|vector|inplace_vector={{dsc inc|cpp/container/dsc capacity|{{#var:cont}}}}}}
{{dsc inc|cpp/container/dsc empty|{{{1|}}}}}
+
{{dsc inc|cpp/container/dsc empty|{{#var:cont}}}}
{{dsc inc|cpp/container/dsc max_size|{{{1|}}}}}
+
{{dsc inc|cpp/container/dsc max_size|{{#var:cont}}}}
{{#switch:{{{1|}}}|vector|deque|list|forward_list={{dsc inc|cpp/container/dsc resize|{{{1|}}}}}}}
+
{{#switch:{{#var:cont}}|vector|inplace_vector|deque|list|forward_list={{dsc inc|cpp/container/dsc resize|{{#var:cont}}}}}}
 
{{dsc inc|cpp/iterator/dsc size}}
 
{{dsc inc|cpp/iterator/dsc size}}
 
{{dsc end}}
 
{{dsc end}}

Latest revision as of 22:44, 9 August 2024

 
 
 
 
constexpr size_type size() const;
(since C++26)

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

Contents

[edit] Parameters

(none)

[edit] Return value

The number of elements in the container.

[edit] Complexity

Constant.

[edit] Example

#include <cassert>
#include <inplace_vector>
 
int main()
{
    std::inplace_vector<int, 4> nums;
    assert(nums.size() == 0);
    nums = {1, 2, 3, 4};
    assert(nums.size() == 4);
}

[edit] See also

[static]
returns the number of elements that can be held in currently allocated storage
(public static member function of std::inplace_vector<T,N>) [edit]
checks whether the container is empty
(public member function of std::inplace_vector<T,N>) [edit]
[static]
returns the maximum possible number of elements
(public static member function of std::inplace_vector<T,N>) [edit]
changes the number of elements stored
(public member function of std::inplace_vector<T,N>) [edit]
(C++17)(C++20)
returns the size of a container or array
(function template) [edit]