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