Stack functions

Jul 7, 2013 at 2:05pm
Hi guys! Here is the functions that I have. It checks if element is in stack:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template<class T>
    bool CheckElem(Stack<T>& A, T x)
      {
        Stack<T> B;
        bool check = false;
        while(!A.Empty())
          {
            if(A.Top() == x) check = true;
            B.Push(A.Top());
            A.Pop();               
          }
        A = B;
        if(check) return true;
        return false;
      }


I was wondering if there is another way to go through stack's elements to check if an element exists insted of saving the stack in another stack in order to not loose the original stack's elemets - because I should Pop all elements to reach the last element for example.
And when I save the elements in another stack they are not anymore reversed (Like stack is supposed to be).
Last edited on Jul 7, 2013 at 2:09pm
Jul 7, 2013 at 2:34pm
First of all your program is invalid because it at least 1) reverses the original stack in case there is no the target element or 2) destroys the original stack in case there is the target element in the stack.

The function could look for example the following way

1
2
3
4
5
6
template<class T>
bool CheckElem( Stack<T> A, const T &x )
{
        while ( !A.Empty() && A.Top() != x ) A.Pop();
        return ( !A.Empty() );
}
Last edited on Jul 7, 2013 at 2:45pm
Jul 7, 2013 at 2:48pm
1
2
3
4
5
6
template<class T>
bool CheckElem( Stack<T> A, const T &x )
{
        while ( !A.Empty() && A.Top() != x ) A.Pop();
        return ( !A.Empty() );
}


But in this case I loose the elements that are already popped :/
Jul 7, 2013 at 2:52pm
You loose nothing because the original stack is not modified. You deal with its copy.
Jul 7, 2013 at 3:01pm
> I was wondering if there is another way to go through stack's elements
> to check if an element exists insted of saving the stack in another stack

There is a way; but we need a derived class to access the underlying sequence container (it is a protected member).

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
}

http://ideone.com/KCb0N5
Jul 7, 2013 at 7:05pm
@JLBorges


it seems that the code has undefined behaviour.
Jul 8, 2013 at 4:59am
> it seems that the code has undefined behaviour.

Ah, does it? And where is that said? Somewhere in that fickle and ephemeral document called the International Standard? Shouldn't we be starting with the assumption that anything it says is probably wrong; that if it says something in one place, it may assert exactly the opposite in another place; that by the time it takes to make a post, the IS might have changed its mind?

My opinion doesn't count, of course; but listen to what the cognoscenti has to say:
The standard contains many contradictions and even bugs. So it should not be considered as something frozen.

http://www.cplusplus.com/forum/beginner/104832/#msg565402
Jul 8, 2013 at 6:48am
well, you can use iterators....
Topic archived. No new replies allowed.