Difference between revisions of "Template:cpp/container/empty ad"
From cppreference.com
D41D8CD98F (Talk | contribs) (- nodiscard) |
m (fmt.) |
||
Line 1: | Line 1: | ||
− | {{ | + | {{#vardefine:cont|{{{1|stack}}}}}<!-- |
− | {{cpp/container/{{ | + | -->{{cpp/container/{{#var:cont}}/title|empty}} |
+ | {{cpp/container/{{#var:cont}}/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{#switch:{{ | + | {{#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|{{ | + | |{{dcl|since={{cpp/std|{{#var:cont}}}}| |
bool empty() const; | bool empty() const; | ||
}} | }} | ||
Line 13: | Line 14: | ||
{{dcl end}} | {{dcl end}} | ||
− | {{#switch:{{ | + | {{#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();}}. | |
− | |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 | + | |<!--stack|queue|priority_queue=--> |
− | + | 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:{{ | + | {{#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|{{ | + | {{#vardefine:id|{{cpp/container/get header|{{#var:cont}}}}}} |
− | {{#switch:{{ | + | {{#switch:{{#var:cont}} |
− | |flat_set|flat_multiset={{include|cpp/container/set/example empty|{{ | + | |flat_set|flat_multiset={{include|cpp/container/set/example empty|{{#var:cont}}}} |
− | |flat_map|flat_multimap={{include|cpp/container/map/example empty|{{ | + | |flat_map|flat_multimap={{include|cpp/container/map/example empty|{{#var:cont}}}} |
| | | | ||
{{example | {{example | ||
|code= | |code= | ||
− | |||
#include <iostream> | #include <iostream> | ||
#include <{{#var:id}}> | #include <{{#var:id}}> | ||
Line 51: | Line 51: | ||
std::cout << std::boolalpha; | std::cout << std::boolalpha; | ||
− | std::{{ | + | 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|{{ | + | {{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
Run this code
#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> )
| |
(C++17) |
checks whether the container is empty (function template) |