Namespaces
Variants
Views
Actions

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

From cppreference.com
(empty on arrays is a peculiar concept)
m (+inplace_vector.)
 
(24 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{cpp/container/{{{1|}}}/title | empty}}
+
{{#vardefine:cont|{{{1|}}}}}<!--
{{cpp/container/{{{1|}}}/navbar}}
+
-->{{cpp/container/{{#var:cont}}/title|empty}}
{{ddcl | notes={{cpp/container/mark since c++11 | {{{1|}}} }} |
+
{{cpp/container/{{#var:cont}}/navbar}}
{{#ifeq:{{{1|}}}|array|constexpr bool empty();|bool empty() const;}}
+
{{dcl begin}}
 +
{{#switch:{{#var:cont}}
 +
|array=
 +
{{dcl|since=c++11|
 +
constexpr bool empty() const noexcept;
 
}}
 
}}
Checks if the container has no elements, i.e. whether {{c|begin() {{==}} end()}}.
+
|vector=
 +
{{dcla|anchor=no|noexcept=c++11|constexpr=c++20|
 +
bool empty() const;
 +
}}
 +
|inplace_vector=
 +
{{dcl|anchor=no|since=c++26|
 +
constexpr bool empty() const noexcept;
 +
}}
 +
|list|deque|set|map|multiset|multimap=
 +
{{dcla|anchor=no|noexcept=c++11|
 +
bool empty() const;
 +
}}
 +
|
 +
{{dcl|since={{cpp/std|{{#var:cont}}}}|
 +
bool empty() const noexcept;
 +
}}
 +
}}
 +
{{dcl end}}
 +
 
 +
Checks if the container has no elements, i.e. whether {{c|1=begin() == end()}}.
  
 
===Parameters===
 
===Parameters===
Line 10: Line 33:
  
 
===Return value===
 
===Return value===
{{c|true}} if the container is empty, {{c|false}} otherwise
+
{{c|true}} if the container is empty, {{c|false}} otherwise.
 
+
===Exceptions===
+
{{noexcept}}
+
  
 
===Complexity===
 
===Complexity===
Constant
+
Constant.
  
 
===Example===
 
===Example===
{{example
+
{{#switch:{{#var:cont}}
| The following code uses {{tt|empty}} to check if a {{c|std::{{{1}}}<int>}} contains any elements:
+
|set|multiset|unordered_set|unordered_multiset
| code=
+
={{include|cpp/container/set/example_empty|{{#var:cont}}}}
#include <{{{1}}}>
+
|map|multimap|unordered_map|unordered_multimap
#include <iostream>
+
={{include|cpp/container/map/example_empty|{{#var:cont}}}}
+
|{{include|cpp/container/{{#var:cont}}/example_empty}}
int main()
+
{
+
    {{#ifeq: {{{1|}}} | array
+
|std::array<int, 4> numbers {3, 1, 4, 1};
+
    std::array<int, 0> no_numbers;
+
 
+
    std::cout << "numbers.empty(): " << numbers.empty() << '\n';
+
    std::cout << "no_numbers.empty(): " << no_numbers.empty() << '\n';
+
|std::{{{1}}}<int> numbers;
+
    std::cout << "Initially, numbers.empty(): " << numbers.empty() << '\n';
+
 
+
    numbers.push_back(42);
+
    numbers.push_back(13317);
+
    std::cout << "After adding elements, numbers.empty(): " << numbers.empty() << '\n';
+
 
}}
 
}}
}
 
| output=
 
{{#ifeq: {{{1|}}} | array
 
|numbers.empty(): 0
 
no_numbers.empty(): 1
 
|Initially, numbers.empty(): 1
 
After adding elements, numbers.empty(): 0
 
}}
 
}}
 
{{#ifeq: {{{1|}}} | forward_list||
 
===See also===
 
  
{{dcl list begin}}
+
===See also===
{{dcl list template | cpp/container/dcl list size |{{{1|}}}}}
+
{{dsc begin}}
{{dcl list end}}
+
{{#switch:{{#var:cont}}
 +
|forward_list=
 +
{{dsc inc|cpp/iterator/dsc distance}}
 +
|
 +
{{dsc inc|cpp/container/dsc size|{{#var:cont}}}}
 +
{{dsc inc|cpp/iterator/dsc empty}}
 
}}
 
}}
 +
{{dsc end}}

Latest revision as of 00:05, 13 August 2024

bool empty() const noexcept;
(since {std})

Checks if the container has no elements, i.e. whether begin() == end().

Contents

[edit] Parameters

(none)

[edit] Return value

true if the container is empty, false otherwise.

[edit] Complexity

Constant.

[edit] Example

[edit] See also

returns the number of elements
(public member function of std::{{{1}}}) [edit]
(C++17)
checks whether the container is empty
(function template) [edit]