C++ (Qt)typedef std::pair <int, double> TPair;typedef std::vector <TPair> TVec;
C++ (Qt)TVec Merge( const TVec & src1, const TVec & src2 );
C++ (Qt)void Merge( TVec & src1, const TVec & src2 );
C++ (Qt)struct my_predicate{ template <class T> bool operator()(const T & a) const { return (a.second != 0); } template <class T> bool operator()(const T & a, const T & b) const { return b.first <= a.first; }}; template <class Iterator, class Container, class Predicate>void merge_if(Iterator first1, Iterator last1, Iterator first2, Iterator last2, Container & result, Predicate pred){ while (true) { if (first1 == last1) { std::copy_if(first2, last2, std::back_inserter(result), pred); return; } if (first2 == last2) { std::copy(first1, last1, std::back_inserter(result)); return; } if (!pred(*first2)) { ++first2; continue; } if (pred(*first1, *first2)) result.push_back(*first2++); else result.push_back(*first1++); }}
C++ (Qt) if (!pred(*first2)) { ++first2; continue; }
C++ (Qt)struct my_stepper{ template <class T> bool operator()(const T & a) const { return (a.second != 0); } template <class Iter, class Container> bool operator()(Iter & in1, Iter & in2, Container & c) const { if (in2->second == 0) { key = in2->first; ++in2; return false; } if (key) { if (in1->first == *key) { ++in1; return false; } } auto it = std::back_inserter(c); *it++ = (in2.first <= in1.first) ? *in2++ : *in1++; return true; } private: boost::optional<int> key;}; template <class Iterator, class Container, class Stepper>void merge_if(Iterator first1, Iterator last1, Iterator first2, Iterator last2, Container & result, Stepper stepper){ while (true) { if (first1 == last1) { std::copy_if(first2, last2, std::back_inserter(result), stepper); return; } if (first2 == last2) { std::copy_if(first1, last1, std::back_inserter(result), stepper); return; } stepper(first1, first2, result); }}
C++ (Qt) template <class Iter, class Container> bool operator()(Iter & in1, Iter & in2, Container & c) const { if (in2->second == 0) { key = in2->first; ++in2; return false; }...
C++ (Qt)struct my_stepper{ template <class T> bool operator()(const T & a) const { return (a.second != 0) && (keys.find(a.first) == keys.end()); } template <class Iter, class Container> bool operator()(Iter & in1, Iter & in2, Container & c) const { if (in2->second == 0) { keys.insert(in2->first); ++in2; return false; } if (keys.find(in1->first) != keys.end()) { ++in1; return false; } auto it = std::back_inserter(c); if (in2->first == in1->first) { *it++ = *in2++; ++in1; return true; } *it++ = (in2->first < in1->first) ? *in2++ : *in1++; return true; } private: mutable std::set<int> keys;}; template <class Iterator, class Container, class Stepper>void merge_if(Iterator first1, Iterator last1, Iterator first2, Iterator last2, Container & result, Stepper stepper){ while (true) { if (first1 == last1) { std::copy_if(first2, last2, std::back_inserter(result), stepper); return; } if (first2 == last2) { std::copy_if(first1, last1, std::back_inserter(result), stepper); return; } stepper(first1, first2, result); }}
C++ (Qt)template <class Iter, class Container>void Step(Iter & in1, Iter & in2, Container & c) { if (in1->first < in2->first) // ключ в первом меньше c.push_back(*in1++); // добавляем из первого и продвигаем его else { // ключ в первом НЕ меньше if (in2->second) c.push_back(*in2); // добавляем из второго если не ноль if (in1->first == in2->first) ++in1; // двигаем первый если ключи равны ++in2; // всегда двигаем второй }}