#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
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlIDxhbGdvcml0aG0+Cgp0ZW1wbGF0ZSA8IHR5cGVuYW1lIFQsIHR5cGVuYW1lIEMgPgpib29sIGlzX2luX3N0YWNrKCBjb25zdCBzdGQ6OnN0YWNrPFQsQz4mIHN0aywgY29uc3QgVCYgdiApCnsKICAgIHN0cnVjdCBjaGVja2VyIDogcHJpdmF0ZSBzdGQ6OnN0YWNrPFQsQz4KICAgIHsKICAgICAgICBzdGF0aWMgYm9vbCBpc19pbl9zdGFjayggY29uc3Qgc3RkOjpzdGFjazxULEM+JiBzdGssIGNvbnN0IFQmIHYgKQogICAgICAgIHsKICAgICAgICAgICAgLy8gZ2V0IGEgcmVmZXJlbmNlIHRvIHRoZSB1bmRlcmx5aW5nIHNlcXVlbmNlIGNvbnRhaW5lcgogICAgICAgICAgICBjb25zdCBhdXRvJiBzZXEgPSBzdGF0aWNfY2FzdDwgY29uc3QgY2hlY2tlciYgPihzdGspLmMgOwoKICAgICAgICAgICAgLy8gY2hlY2sgZm9yIHRoZSBlbGVtZW50IGluIGl0CiAgICAgICAgICAgIHJldHVybiBzdGQ6OmZpbmQoIHNlcS5iZWdpbigpLCBzZXEuZW5kKCksIHYgKSAhPSBzZXEuZW5kKCkgOwogIC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