fork download
  1. #include <iostream>
  2. #include <stack>
  3. #include <algorithm>
  4.  
  5. template < typename T, typename C >
  6. bool is_in_stack( const std::stack<T,C>& stk, const T& v )
  7. {
  8. struct checker : private std::stack<T,C>
  9. {
  10. static bool is_in_stack( const std::stack<T,C>& stk, const T& v )
  11. {
  12. // get a reference to the underlying sequence container
  13. const auto& seq = static_cast< const checker& >(stk).c ;
  14.  
  15. // check for the element in it
  16. return std::find( seq.begin(), seq.end(), v ) != seq.end() ;
  17. }
  18. };
  19. return checker::is_in_stack( stk, v ) ;
  20. }
  21.  
  22. int main()
  23. {
  24. // move constructor
  25. std::stack<int> stk( std::stack<int>::container_type { 1, 3, 5, 7, 9 } ) ;
  26.  
  27. std::cout << std::boolalpha << is_in_stack( stk, 7 ) << '\n' // true
  28. << is_in_stack( stk, 4 ) << '\n' ; // false
  29. }
  30.  
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
true
false