std::merge
コンテナへのイテレータが入ったコンテナをマージする.
std::vector<>へのconst_iteratorが入ったstd::vector<>をstd::mergeする.
なんか必要になったんで.
今回はintです.
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <vector> #include <boost/bind.hpp> // 参照先を返す template<typename IT> struct unref : std::unary_function<IT, typename std::iterator_traits<IT>::value_type> { const typename std::iterator_traits<IT>::value_type& operator()(const IT& x) const{ return *x; } }; int main(){ typedef std::vector<int>::const_iterator citer; int src_a[] = {3, 5, 67, 1, 3, 90}; int src_b[] = {3, 89, 33, 1, 9, 22, 43, 0, 33}; std::vector<int> a(src_a, src_a + 6); std::vector<int> b(src_b, src_b + 9); // コンテナへのイテレータを入れる std::vector<citer> ia; // a std::vector<citer> ib; // b for(citer it = a.begin(); it != a.end(); ++it) ia.push_back(it); for(citer it = b.begin(); it != b.end(); ++it) ib.push_back(it); // ソート std::sort(ia.begin(), ia.end(), boost::bind(unref<citer>(), _1) < boost::bind(unref<citer>(), _2)); std::sort(ib.begin(), ib.end(), boost::bind(unref<citer>(), _1) < boost::bind(unref<citer>(), _2)); // ia: 1 3 3 5 67 90 // ib: 0 1 3 9 22 33 33 43 89 // マージ std::vector<citer> v; std::merge(ia.begin(), ia.end(), ib.begin(), ib.end(), std::back_inserter(v), boost::bind( std::less<int>(), boost::bind(unref<citer>(), _1), boost::bind(unref<citer>(), _2) ) ); // v: 0 1 1 3 3 3 5 9 22 33 33 43 67 89 90 }
結構簡単でした.
何か最近21時になると眠くなる.
生活リズムがくずれているせい?