std::empty
Defined in header <array> |
||
|---|---|---|
Defined in header <deque> |
||
Defined in header <forward_list> |
||
Defined in header <iterator> |
||
Defined in header <list> |
||
Defined in header <map> |
||
Defined in header <regex> |
||
Defined in header <set> |
||
Defined in header <span> |
(с C++20) | |
Defined in header <string> |
||
Defined in header <string_view> |
||
Defined in header <unordered_map> |
||
Defined in header <unordered_set> |
||
Defined in header <vector> |
||
| (1) | ||
template< class C > constexpr auto empty( const C& c ) -> decltype(c.empty()); |
(с C++17) (до C++20) |
|
template< class C > [[nodiscard]] constexpr auto empty( const C& c ) -> decltype(c.empty()); |
(с C++20) | |
| (2) | ||
template< class T, std::size_t N > constexpr bool empty( const T (&array)[N] ) noexcept; |
(с C++17) (до C++20) |
|
template< class T, std::size_t N > [[nodiscard]] constexpr bool empty( const T (&array)[N] ) noexcept; |
(с C++20) | |
| (3) | ||
template< class E > constexpr bool empty( std::initializer_list<E> il ) noexcept; |
(с C++17) (до C++20) |
|
template< class E > [[nodiscard]] constexpr bool empty( std::initializer_list<E> il ) noexcept; |
(с C++20) |
Возвращает, пустой ли заданный диапазон.
1) Возвращает
c.empty().
2) Возвращает
false.
3) Возвращает
il.size() == 0.Параметры
| c | - | контейнер или представление с функцией-членом empty |
| array | - | массив произвольного типа |
| il | - | список инициализации |
Значение возврата
true если диапазон не содержит элементов.
Исключения
1) Может выбросить исключение, определённое реализацией.
Примечания
Перегрузка для std::initializer_list необходима, так как у неё нет функции-члена empty.
| Тест на наличие функции макрос | Значение | Стандарт | Функция |
|---|---|---|---|
__cpp_lib_nonmember_container_access |
201411L | (C++17) |
std::size(), std::data(), и std::empty() |
Возможная реализация
| Первый вариант |
|---|
template<class C>
[[nodiscard]] constexpr auto empty(const C& c) -> decltype(c.empty())
{
return c.empty();
} |
| Второй вариант |
template<class T, std::size_t N>
[[nodiscard]] constexpr bool empty(const T (&array)[N]) noexcept
{
return false;
} |
| Третий вариант |
template<class E>
[[nodiscard]] constexpr bool empty(std::initializer_list<E> il) noexcept
{
return il.size() == 0;
} |
Пример
#include <iostream>
#include <vector>
template<class T>
void print(const T& container)
{
if (std::empty(container))
std::cout << "Empty\n";
else
{
std::cout << "Elements:";
for (const auto& element : container)
std::cout << ' ' << element;
std::cout << '\n';
}
}
int main()
{
std::vector<int> c = {1, 2, 3};
print(c);
c.clear();
print(c);
int array[] = {4, 5, 6};
print(array);
auto il = {7, 8, 9};
print(il);
}Вывод:
Elements: 1 2 3 Empty Elements: 4 5 6 Elements: 7 8 9
См. также
|
(C++20)
|
проверяет, пустой ли диапазон (объект точек настройки) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/iterator/empty