Namespaces
Variants
Views
Actions

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

From cppreference.com
(- nodiscard)
m (fmt.)
Line 1: Line 1:
{{cpp/container/{{{1|}}}/title|empty}}
+
{{#vardefine:cont|{{{1|stack}}}}}<!--
{{cpp/container/{{{1|}}}/navbar}}
+
-->{{cpp/container/{{#var:cont}}/title|empty}}
 +
{{cpp/container/{{#var:cont}}/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{#switch:{{{1|}}}
+
{{#switch:{{#var:cont}}
 
|flat_set|flat_multiset|flat_map|flat_multimap=
 
|flat_set|flat_multiset|flat_map|flat_multimap=
 
{{dcl|since=c++23|
 
{{dcl|since=c++23|
 
bool empty() const noexcept;
 
bool empty() const noexcept;
 
}}
 
}}
|{{dcl|since={{cpp/std|{{{1|}}}}}|
+
|{{dcl|since={{cpp/std|{{#var:cont}}}}|
 
bool empty() const;
 
bool empty() const;
 
}}
 
}}
Line 13: Line 14:
 
{{dcl end}}
 
{{dcl end}}
  
{{#switch:{{{1|}}}
+
{{#switch:{{#var:cont}}
|flat_map|flat_multimap
+
|flat_map|flat_multimap=
=Checks if the underlying containers have no elements. Equivalent to {{c|1=return begin() == end();}}.
+
Checks if the underlying containers have no elements. Equivalent to: {{c|1=return begin() == end();}}.
|flat_set|flat_multiset
+
|flat_set|flat_multiset=
=Checks if the underlying container has no elements. Equivalent to {{c|1=return begin() == end();}}.
+
Checks if the underlying container has no elements. Equivalent to: {{c|1=return begin() == end();}}.
<!--stack, queue, priority_queue:-->
+
|<!--stack|queue|priority_queue=-->
|Checks if the underlying container has no elements. Equivalent to {{c|return c.empty();}}.
+
Checks if the underlying container has no elements. Equivalent to: {{box|{{c/core|return}}{{nbspt}}{{rlpt|/#Member objects|c}}{{c/core|.empty()}}}}.
 
}}
 
}}
  
Line 26: Line 27:
  
 
===Return value===
 
===Return value===
{{#switch:{{{1|}}}
+
{{#switch:{{#var:cont}}
 
|flat_map|flat_multimap
 
|flat_map|flat_multimap
 
={{c|true}} if the underlying containers are empty, {{c|false}} otherwise.
 
={{c|true}} if the underlying containers are empty, {{c|false}} otherwise.
Line 36: Line 37:
  
 
===Example===
 
===Example===
{{#vardefine:id|{{cpp/container/get header|{{{1|}}}}}}}
+
{{#vardefine:id|{{cpp/container/get header|{{#var:cont}}}}}}
{{#switch:{{{1|}}}
+
{{#switch:{{#var:cont}}
|flat_set|flat_multiset={{include|cpp/container/set/example empty|{{{1|}}}}}
+
|flat_set|flat_multiset={{include|cpp/container/set/example empty|{{#var:cont}}}}
|flat_map|flat_multimap={{include|cpp/container/map/example empty|{{{1|}}}}}
+
|flat_map|flat_multimap={{include|cpp/container/map/example empty|{{#var:cont}}}}
 
|
 
|
 
{{example
 
{{example
 
|code=
 
|code=
#include <algorithm>
 
 
#include <iostream>
 
#include <iostream>
 
#include <{{#var:id}}>
 
#include <{{#var:id}}>
Line 51: Line 51:
 
     std::cout << std::boolalpha;
 
     std::cout << std::boolalpha;
  
     std::{{{1|}}}<int> {{#var:id}};
+
     std::{{#var:cont}}<int> {{#var:id}};
  
 
     std::cout << "Initially, {{#var:id}}.empty(): " << {{#var:id}}.empty() << '\n';
 
     std::cout << "Initially, {{#var:id}}.empty(): " << {{#var:id}}.empty() << '\n';
Line 66: Line 66:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc|cpp/container/dsc size|{{{1|}}}}}
+
{{dsc inc|cpp/container/dsc size|{{#var:cont}}}}
 
{{dsc inc|cpp/iterator/dsc empty}}
 
{{dsc inc|cpp/iterator/dsc empty}}
 
{{dsc end}}
 
{{dsc end}}

Revision as of 17:49, 1 November 2024

 
 
 
 
bool empty() const;

Checks if the underlying container has no elements. Equivalent to: return c.empty().

Contents

Parameters

(none)

Return value

true if the underlying container is empty, false otherwise.

Complexity

Constant.

Example

#include <iostream>
#include <stack>
 
int main()
{
    std::cout << std::boolalpha;
 
    std::stack<int> stack;
 
    std::cout << "Initially, stack.empty(): " << stack.empty() << '\n';
 
    stack.push(42);
    std::cout << "After adding elements, stack.empty(): " << stack.empty() << '\n';
}

Output:

Initially, stack.empty(): true
After adding elements, stack.empty(): false

See also

returns the number of elements
(public member function of std::stack<T,Container>) [edit]
(C++17)
checks whether the container is empty
(function template) [edit]