3

I am working with stacks and need to check to see if two are the same. I have overloaded the

bool operator== 

function in my code, and now I need to put the logic in the code. I will check a few things to see if the two stacks are the same: the length, the data type, and the content of each element. Length and content are no problem, its the data type that is giving me issues.

I tried to make a function:

...
    Type getType();
};

template <class Type>
Type getType(){ returnType;}

But this did not work.

I also thought about:

bool operator== (stack<Type> &lhs, stack<Type> &rhs){

    return (lsh.Type == rhs.Type);
//additional conditions will be checked.
}

How to I check if they are the same type?

EDIT: What if I just checked the data type of the top elements of the two stacks? would that be sufficient?

Jeff
  • 4,285
  • 15
  • 63
  • 115
  • 1
    How are your stacks implemented? – Basile Starynkevitch Oct 12 '12 at 17:38
  • 2
    "Did not work" is a bit un-specific. Are you getting compile time errors? Run time errors? The wrong result? – Joachim Isaksson Oct 12 '12 at 17:39
  • If they're not the same type, then the element contents shouldn't equal each other. I'd just defer that to the elements. I.e. instead of "if the stack elements are the same type", make it "if the stack elements are equality-comparable" – millimoose Oct 12 '12 at 17:39
  • @Basile, In my main? Something like this: stackType a(100); stackType b(100); – Jeff Oct 12 '12 at 17:39
  • see this question: [C++ equivalent of instanceof](http://stackoverflow.com/questions/500493/c-equivalent-of-instanceof) – dualed Oct 12 '12 at 17:47
  • Are you using `std:stack`, or have you implemented your own stack data structure? If this is anything other than a learning exercise, just use `std::stack`; it already has an `operator==`. – Robᵩ Oct 12 '12 at 17:50
  • I am not using std::stack... its my own stackType class. Even if it were two arrays, two lists, two queues, etc. I should be able to know if they are of the same type. – Jeff Oct 12 '12 at 17:54

5 Answers5

9

If you implement operator== like this:

template<typename Type>
bool operator== (const stack<Type> &lhs, const stack<Type> &rhs){
    // compare and return.
}

You already know that both stacks will contain elements of the same type. There's no need to check for that.

If you want to allow stacks using different template arguments to be compared, you could do something like this:

template<typename Type1, typename Type2>
bool operator== (const stack<Type1> &lhs, const stack<Type2> &rhs){
    // compare
}

Then compare the elements using operator== and you'll be done. Of course, if there's no operator== for parameters of type Type1 and Type2, then the compiler will issue an error.

Edit: Since you want a nice error message, you could do this(using C++11):

#include <type_traits>

template<typename Type1, typename Type2>
bool operator== (const stack<Type1> &lhs, const stack<Type2> &rhs){
    // This is a compile time assert!
    static_assert(std::is_same<Type1, Type2>::value, "Types are not equal!");
    // compare
}

I'd avoid this anyway. It's simpler to let the compiler issue its own error, rather than doing this check yourself.

mfontanini
  • 21,410
  • 4
  • 65
  • 73
  • No. Actually you will only get a compile error if one of those differs from the stack this op is defined on. In other words, the code requires them to be the same, but the OP does not want to use it this way – dualed Oct 12 '12 at 17:41
  • If I say, stack a(100) == stack b(100); it wouldnt error? – Jeff Oct 12 '12 at 17:42
  • 1
    No what? If you try to compare stacks using different template arguments, a call to `opeator==` will fail. – mfontanini Oct 12 '12 at 17:42
  • @Jeff yes, but do you really want to allow that comparison to succeed? Those stacks should be different. – mfontanini Oct 12 '12 at 17:43
  • I'd use `const` like this: `bool operator==(const stack & lhs, const stack & rhs)`. – Mr.C64 Oct 12 '12 at 17:44
  • @mfontanini, No, I don't want that type of comparison to suceed; I would want that to fail immediately so I would not iterate through each element and cause a run-time error. – Jeff Oct 12 '12 at 17:46
  • 1
    @Jeff then the first example will work. As soon as the compiler figures out you're using different template parameters for those stack objects, it will provide a compile-time error(not a runtime one). – mfontanini Oct 12 '12 at 17:49
  • @Mr.C64 copy-pasted, forgot about that. – mfontanini Oct 12 '12 at 17:50
  • @mfontanini What if I want it to compile and output a "these are not the same type" message? – Jeff Oct 12 '12 at 17:50
7

C++ is not like other (dynamic) languages in that you do not need to check types. If two stacks contain different types, your code will not compile if you try to compare them. In other words, skip this step, the compiler will do it for you.

Paul Manta
  • 30,618
  • 31
  • 128
  • 208
2

If you implement the stack data structure using templates (e.g. template <typename T> class Stack...), then you just need to overload operator== and check sizes and do an item-wise comparison:

template <typename T>
bool operator==(const Stack<T> & lhs, const Stack<T> & rhs)
{
  if (lhs.Size() != rhs.Size())
    return false;

  // Compare item by item
  ...
}

The type will be implicitly checked by the C++ compiler type system (if you have a Stack<int> and try to compare against a Stack<string> you will have a compiler error).

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • This is what I will do next, but first I want to see if the two types are the same. No sense in comparing size and each element if they are not the same Type. – Jeff Oct 12 '12 at 17:51
  • @Jeff try reading the answer. The compiler already checks that for you. They will be the same type. The compiler will not let you call the function on stacks that hold different types. – Dirk Holsopple Oct 12 '12 at 17:57
  • I understand that. In my opinion, I should be able to say, int x=5; string cat="cat"; cout << "is a cat = to 5?" << cat==5; and it compile and run fine. I want to do this with stacks. – Jeff Oct 12 '12 at 18:10
1

I found a simple program which will solve your purpose, Its simple and very efficient way of checking if two stacks are equal or not.

  bool isEqual(stack<Type> &stack1, stack<Type> &stack2) {
    if (stack1.empty() && stack2.empty())
      return true;
    else if (stack1.empty() || stack2.empty())
      return false;
    while (!stack1.empty() && !stack2.empty())
    {
      if (stack1.top() != stack2.top())
        return false;
      stack1.pop();
      stack2.pop();
    }
    if (stack1.empty() && stack2.empty())
      return true;
    else
      return false;
  }
Ganesh M S
  • 1,073
  • 2
  • 13
  • 24
0

As you can see in C++ tag definition of stackoverflow

C++ is a widely-used, statically-typed, free-form, compiled,
multi-paradigm, multi-level, imperative, general-purpose,
object-oriented programming language.

In this definition statically-typed mean all type information must be clean in compile time so in C++ if you want to have container of different types or you want to compare containers of different types, you must identify it in compile time through use of templates.

So when you are comparing 2 different type of containers (here stack) you know type of both containers and you do not need to compare their types. So if you want containers of different types, they should all derive from same class so they can stored as same type or you must hold them as void* (this is very dangerous unless you know what you are doing). in case that you derive all classes from same class, you can simply make base class polymorphic (by adding at least one virtual function) and use typeid to get type of object or you can add a virtual function that do comparison and then specialize it in each derived class (or even you can mix both techniques) but in case of void* you must have another way to identify type of objects!

BigBoss
  • 6,904
  • 2
  • 23
  • 38