#include <algorithm>
#include <functional>
#include <iterator>
#include <vector>
#include <iostream>
template <typename Tp>
class IfIn : public std::unary_function<typename Tp::value_type, bool> {
public:
IfIn(Tp begin, Tp end) : begin_(begin), end_(end) {}
IfIn(const IfIn &rhs) : begin_(rhs.begin_), end_(rhs.end_) {}
bool operator()(const typename Tp::value_type &v) const {
return std::binary_search(begin_, end_, v);
}
private:
Tp begin_;
Tp end_;
};
template <class Tp>
inline IfIn<Tp> ifIn(Tp begin, Tp end) {
return IfIn<Tp>(begin, end);
}
int main() {
char c1[4] = {'a', 'd', 'c', 'g'};
char c2[5] = {'a', 'k', 'c', 'f', 'h'};
std::vector<char> v1(c1, c1+4);
std::vector<char> v2(c2, c2+5);
std::sort(v2.begin(), v2.end());
std::vector<char>::iterator end = std::remove_if(v1.begin(), v1.end(), ifIn(v2.begin(), v2.end()));
v1.erase(end, v1.end());
std::copy(v1.begin(), v1.end(), std::ostream_iterator<char>(std::cout, ","));
std::cout << std::endl;
v1.assign(c1, c1+4);
v2.assign(c2, c2+5);
std::sort(v2.begin(), v2.end());
end = std::remove_if(v1.begin(), v1.end(), std::not1(ifIn(v2.begin(), v2.end())));
v1.erase(end, v1.end());
std::copy(v1.begin(), v1.end(), std::ostream_iterator<char>(std::cout, ","));
std::cout << std::endl;
return 0;
}