How to check whether a container is empty in C++ 17

1 Answer

0 votes
#include <iostream>
#include <vector>
 
template <class T>
void print(const T& container) {
    if ( !std::empty(container) ) {
         for ( const auto& element : container )
             std::cout << element << ", ";
    }
    else  {
        std::cout << "Empty\n";
    }
    std::cout << '\n';
}
 
int main() 
{
    std::vector<int> v = { 5, 3, 1, 8, 2 };
    v.clear();
    print(v);
 
    int arr[] = { 9, 3, 1, 2 };
    print(arr);
 
    auto lst = { 7, 1, 6, 2, 4, 8 };
    print(lst);
}
 


 
/*
run:
 
Empty

9, 3, 1, 2, 
7, 1, 6, 2, 4, 8, 
 
*/

 



answered Jun 21, 2020 by avibootz

Related questions

1 answer 236 views
1 answer 191 views
191 views asked Apr 22, 2022 by avibootz
1 answer 116 views
1 answer 203 views
1 answer 175 views
1 answer 258 views
1 answer 201 views
...