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
|
namespace my{
template <typename Itr>
void sort(Itr,Itr);
template <typename Ran>
void sort_helper(Ran beg, Ran end, std::random_access_iterator_tag) {
std::sort(beg, end, std::less<decltype(*beg)>());
}
template <typename For>
void sort_helper(For beg, For end, std::forward_iterator_tag) {
std::vector<decltype(*beg)> v{beg, end};
sort(v.begin(), v.end());
std::copy(v.begin(), v.end(), beg);
}
template <typename Itr>
void sort(Itr beg, Itr end) {
sort_helper(beg, end, std::iterator_traits<Itr>
::iterator_category());
}
}
int main(){
std::forward_list<int> fl;
std::default_random_engine e;
std::uniform_int_distribution<> d(0, 100);
for(int i = 0; i < 10; ++i){
fl.push_front(d(e));
}
my::sort(fl.begin(), fl.end());
for (auto item: fl) {
std::cout << item << " ";
}
std::cout << std::endl;
}
|