Created
June 3, 2014 16:14
-
-
Save duckie/2f90c0168adc99b007d0 to your computer and use it in GitHub Desktop.
Compute intersection of two lists - Method 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <set> | |
#include <unordered_set> | |
#include <list> | |
#include <algorithm> | |
using namespace std; | |
template <typename T> using intersection_set = set<reference_wrapper<T const>, less<T>>; | |
list<string> compare_list_warning(list<string> const& list_one, list<string> const& list_two) | |
{ | |
intersection_set<string> set_one, set_two; | |
set_one.insert(begin(list_one), end(list_one)); | |
set_two.insert(begin(list_two), end(list_two)); | |
list<string> output; | |
set_intersection(begin(set_one), end(set_one), begin(set_two), end(set_two), back_inserter(output), less<string>()); | |
return output; | |
} | |
int main() { | |
list<string> common_names = compare_list_warning({"Roger", "Marcel", "Camille", "Hubert"}, {"Huguette", "Cunegond", "Marcelle", "Camille"}); | |
for(string const& common : common_names) { | |
std::cout << "Common element: " << common << "\n"; | |
} | |
std::cout << std::flush; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment