2017-09-02 44 views
1

我有許多隊列中他們總是成對出現。像隊列& B,隊列Ç& d等迭代器通過許多不同類型的隊列

這些隊列是在不同類型的,但是從一個模板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; 

我的目標是a和b(然後c和之間進行比較d,然後....)看看它們是否相同。我當然可以爲不同的對做3次while循環。但是有沒有什麼聰明的方法,以防萬一他們會有這麼多的隊列。

+0

正在隊列成對出現或數值,也成對出現的?在後一種情況下 - 您可以將這些值對存儲在一個隊列中 –

+0

隊列始終配對。 A和B是一對。 C和D是一對。 – thundium

+0

我的意思是 - a中的任何條目在b中是否有一對,並且可以將它們作爲std :: pair添加到同一個隊列中?因此,您將使用類似find_if –

回答

1

您可以編寫模板化函數,該函數需要您明確指定的兩個類型的類型,以便您可以比較所需的任何元素。

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; 
} 

函數調用(爲整數):

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

See it live here.