Namespaces
Variants
Views
Actions

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

From cppreference.com
m (Text replace - "<!-- ======== --> " to "")
m (+inplace_vector.)
 
(44 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{cpp/container/{{{1|}}}/title | size}}
+
{{#vardefine:cont|{{{1|inplace_vector}}}}}<!--
{{cpp/container/{{{1|}}}/sidebar}}
+
-->{{cpp/container/{{#var:cont}}/title|size}}
{{ddcl | notes={{cpp/container/mark c++0x feature | {{{1|}}} }} |
+
{{cpp/container/{{#var:cont}}/navbar}}
 +
{{dcl begin}}
 +
{{#switch:{{#var:cont}}
 +
|array=
 +
{{dcl|since=c++11|
 +
constexpr size_type size() const noexcept;
 +
}}
 +
|vector=
 +
{{dcl|notes={{mark noexcept since c++11}}<br>{{mark constexpr since c++20}}|
 
size_type size() const;
 
size_type size() const;
 
}}
 
}}
 +
|inplace_vector=
 +
{{dcl|since=c++26|
 +
constexpr size_type size() const;
 +
}}
 +
|
 +
{{cpp/container/noexcept dcl|{{#var:cont}}|dcl=
 +
size_type size() const
 +
}}
 +
}}
 +
{{dcl end}}
  
Returns the number of elements in the container, i.e. {{cpp|std::distance(begin(), end())}}.
+
Returns the number of elements in the container, i.e. {{c|std::distance(begin(), end())}}.
  
 
===Parameters===
 
===Parameters===
{{param none}}
+
(none)
  
 
===Return value===
 
===Return value===
 
+
The number of elements in the container.
the number of elements in the container
+
  
 
===Complexity===
 
===Complexity===
{{complex constant}}
+
Constant.
  
===See also===
+
===Example===
 +
{{#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}}
 +
}}
  
{{dcl list begin}}
+
===See also===
{{#ifeq: {{{1|}}} | vector | {{dcl list template | cpp/container/dcl list capacity |{{{1|}}}}} }}
+
{{dsc begin}}
{{dcl list template | cpp/container/dcl list empty |{{{1|}}}}}
+
{{#switch:{{#var:cont}}|vector|inplace_vector={{dsc inc|cpp/container/dsc capacity|{{#var:cont}}}}}}
{{dcl list template | cpp/container/dcl list max_size |{{{1|}}}}}
+
{{dsc inc|cpp/container/dsc empty|{{#var:cont}}}}
{{#switch:{{{1|}}} | vector | deque | list | forward_list={{dcl list template | cpp/container/dcl list resize |{{{1|}}}}} }}
+
{{dsc inc|cpp/container/dsc max_size|{{#var:cont}}}}
{{dcl list end}}
+
{{#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 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]