Namespaces
Variants
Views
Actions

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

From cppreference.com
m (Text replace - "{{mark pre c++11 version}}" to "{{mark until c++11}}")
m (+inplace_vector.)
 
(34 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++11 feature | {{{1|}}} }} |
+
{{cpp/container/{{#var:cont}}/navbar}}
{{#ifeq:{{{1|}}}|array|constexpr size_type size() | size_type size() const;}}
+
{{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;
 +
}}
 +
|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===
Line 11: Line 29:
  
 
===Return value===
 
===Return value===
the number of elements in the container
+
The number of elements in the container.
 
+
===Exceptions===
+
{{noexcept}}
+
  
 
===Complexity===
 
===Complexity===
{{#ifeq: {{{1|}}}|list|Constant or linear {{mark until c++11}}
+
Constant.
  
Constant {{mark since c++11}}|Constant}}
+
===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}}
 +
}}
  
 
===See also===
 
===See also===
 
+
{{dsc begin}}
{{dcl list begin}}
+
{{#switch:{{#var:cont}}|vector|inplace_vector={{dsc inc|cpp/container/dsc capacity|{{#var:cont}}}}}}
{{#ifeq: {{{1|}}} | vector | {{dcl list template | cpp/container/dcl list capacity |{{{1|}}}}} }}
+
{{dsc inc|cpp/container/dsc empty|{{#var:cont}}}}
{{dcl list template | cpp/container/dcl list empty |{{{1|}}}}}
+
{{dsc inc|cpp/container/dsc max_size|{{#var:cont}}}}
{{dcl list template | cpp/container/dcl list max_size |{{{1|}}}}}
+
{{#switch:{{#var:cont}}|vector|inplace_vector|deque|list|forward_list={{dsc inc|cpp/container/dsc resize|{{#var:cont}}}}}}
{{#switch:{{{1|}}} | vector | deque | list | forward_list={{dcl list template | cpp/container/dcl list resize |{{{1|}}}}} }}
+
{{dsc inc|cpp/iterator/dsc size}}
{{dcl list 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]