thundium
thundium

Reputation: 1053

Iterator through many queues with different types

I have many queues in which they always comes in pairs. Like queue A & B, queue C & D and so on

The queues are in different types but from one template class.Like

template<class objType>
struct myqueueType {
   objType obj;
   ....other content....
}

std::deque<myqueueType<int> > a;
std::deque<myqueueType<int> > b;

std::deque<myqueueType<double> > c;
std::deque<myqueueType<double> > d;

std::deque<myqueueType<float> > e;
std::deque<myqueueType<float> > f;

My target is to compare between a and b(then c and d, then ....)to see if they are the same. I can of course do the while loop 3 times for different pairs. But is there any smart way in case they are going to be many many queues like that.

Upvotes: 1

Views: 55

Answers (1)

kocica
kocica

Reputation: 6467

You may write templated function which expects two deques of type which you explicitly specify, so you may compare whatever elements you want.

template<typename T, typename Container = std::deque<T>>
bool CompareQueues(Container& first, Container& second)
{
    if (first.size() != second.size())
    {
         return false;
    }

    for (auto it1 = first.begin(),
              it2 = second.begin();
         it1 != first.end() &&
         it2 != second.end();
         it1++, it2++)
    {
        if (*it1 != *it2)  // Overload operator '!=' in 'myqueueType', or compare them as you wish
        {
            return false;
        }
    }
    return true;
}

Function call (for integers):

std::deque<myqueueType<int> > a;
std::deque<myqueueType<int> > b;
CompareQueues<int>(a, b);

See it live here.

Upvotes: 1

Related Questions