比較しないソートの作成
Posted feedbacks - C++
皆さんのコードを参考に実装してみましたが、イテレータのサンプルみたいになってしまいました orz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
template<typename InputIterator, typename OutputIterator>
void sort(int min, int max, InputIterator begin, InputIterator end, OutputIterator dst)
{
std::vector<int> counts(max - min + 1);
for(InputIterator i = begin; i != end; ++i)
{
++counts[*i - min];
}
for(std::size_t i = 0; i < counts.size(); ++i)
{
std::vector<int> c(counts[i], i + min);
std::copy(c.begin(), c.end(), dst);
}
}
int main(int, char* [])
{
static const int array[] = { -1, 9, 4, 8, 9, 6, 3, 9, 5, 2 };
std::vector<int> dst;
// ソート; dst に並べ替えられた値が入ります
sort(-1, 10, array, array + (sizeof(array) / sizeof(*array)), std::back_inserter(dst));
// 結果表示
std::copy(dst.begin(), dst.end(), std::ostream_iterator<int>(std::cout, ", "));
return 0;
}
|


sweetie089 #6628() Rating3/3=1.00
[ reply ]