Difference between revisions of "CPP/STL/SetDifference"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) |
Daviddoria (Talk | contribs) |
||
| Line 7: | Line 7: | ||
#include <iterator> | #include <iterator> | ||
| − | |||
template<class Coll> | template<class Coll> | ||
| − | void print(const Coll c){ | + | void print(const Coll c) |
| − | + | { | |
| − | + | ||
| − | + | ||
typename Coll::const_iterator itr = c.begin(); | typename Coll::const_iterator itr = c.begin(); | ||
| − | cout << "{ "; | + | std::cout << "{ "; |
while(itr != c.end()) | while(itr != c.end()) | ||
{ | { | ||
| − | cout << *itr++ << " "; | + | std::cout << *itr++ << " "; |
} | } | ||
| − | cout << "}"; | + | std::cout << "}"; |
} | } | ||
int main() | int main() | ||
| − | { | + | { |
using namespace std; | using namespace std; | ||
| − | string s1 = " | + | string s1 = "abc"; |
| − | string s2 = " | + | string s2 = "abd"; |
list<char> a = list<char>(s1.begin(),s1.end()); | list<char> a = list<char>(s1.begin(),s1.end()); | ||
list<char> b = list<char>(s2.begin(),s2.end()); | list<char> b = list<char>(s2.begin(),s2.end()); | ||
list<char> res; | list<char> res; | ||
| − | |||
| − | |||
| − | |||
std::set_difference(a.begin(),a.end(),b.begin(),b.end(),std::back_insert_iterator<list<char> >(res)); | std::set_difference(a.begin(),a.end(),b.begin(),b.end(),std::back_insert_iterator<list<char> >(res)); | ||
| − | |||
print(a); | print(a); | ||
| − | cout << " - "; | + | std::cout << " - "; |
| − | print(b); | + | print(b); |
| − | cout << " = "; | + | std::cout << " = "; |
print(res); | print(res); | ||
| − | cout << endl; | + | std::cout << endl; |
return 0; | return 0; | ||
} | } | ||
| + | </source> | ||
| + | |||
| + | ==CMakeLists.txt== | ||
| + | <source lang="cmake"> | ||
| + | cmake_minimum_required(VERSION 2.6) | ||
| + | |||
| + | Project(SetDifference) | ||
| + | |||
| + | ADD_EXECUTABLE(SetDifference SetDifference.cpp) | ||
| + | |||
</source> | </source> | ||
Latest revision as of 08:23, 26 February 2011
SetDifference.cpp
#include <iostream> #include <list> #include <algorithm> #include <string> #include <iterator> template<class Coll> void print(const Coll c) { typename Coll::const_iterator itr = c.begin(); std::cout << "{ "; while(itr != c.end()) { std::cout << *itr++ << " "; } std::cout << "}"; } int main() { using namespace std; string s1 = "abc"; string s2 = "abd"; list<char> a = list<char>(s1.begin(),s1.end()); list<char> b = list<char>(s2.begin(),s2.end()); list<char> res; std::set_difference(a.begin(),a.end(),b.begin(),b.end(),std::back_insert_iterator<list<char> >(res)); print(a); std::cout << " - "; print(b); std::cout << " = "; print(res); std::cout << endl; return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) Project(SetDifference) ADD_EXECUTABLE(SetDifference SetDifference.cpp)