在复习算法设计和学习 C++11 的同时,我想出了以下堆排序的实现:
template <typename It, typename Comp> void heapSort(It begin, It end, Comp compFunc, std::random_access_iterator_tag) { std::make_heap(begin, end, compFunc); std::sort_heap(begin, end, compFunc); } template <typename It, typename Comp, typename IterCat> void heapSort(It begin, It end, Comp compFunc, IterCat) { typedef typename It::value_type value_type; std::vector<value_type> randomAccessContainer; randomAccessContainer.reserve(std::distance(begin, end)); std::move(begin, end, std::back_inserter(randomAccessContainer)); heapSort(std::begin(randomAccessContainer), std::end(randomAccessContainer), compFunc, std::random_access_iterator_tag()); std::move(std::begin(randomAccessContainer), std::end(randomAccessContainer), begin); }
[begin, end)
首先从新容器移入新容器,然后从该容器移回新容器是标准 C++[begin, end)
吗?