vendredi 28 octobre 2016

Comparing 2 std::lists of std::pairs - Keeping Unique and Different elements

I have 2 Lists of Pairs, each having matching pairs, non matching pairs, and unique pairs. I want to keep the unique and non matching pairs.

I made this code, which works great for finding the mismatches, but not the unique:

std::list <std::pair<std::string,std::string>> outputList1;
std::list <std::pair<std::string,std::string>> outputList2;
for (auto x: outputList1){
  for (auto y: outputList2){
  //compare x to y
         if(x.first == y.first)
         {
                   if(x.second != y.second)
                   {
                        //mismatch on the second element
                        //do something with it
                    }


        }

  }
}

This is what I tried to pull in the unique. I tried to check to see if I am at the last element of y, and if so, save that pair to pull out the unique elements, but it is pulling in everything. I tried writing this out on paper, but my head is spinning.

  auto &last = *(--outputList2.end());
  for (auto x: outputList1){
  for (auto& y: outputList2){
  //compare x to y
         if(x.first == y.first)
         {
                   if(x.second != y.second)
                   {
                       //mistmatch found so I save it off somewhere
                    }


        }
        else if(&y == &last)
         {
             //unique item found in list x
         }

  }
}

I also tried this to get the end of y on the else if:

        else if(&y == &*std::prev(std::end(outputList2)))

Can you see what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire