어떻게 std::is_sorted
사용하지 않는 약 :
Live On Coliru
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
template <typename Container,
typename It = typename Container::const_iterator,
typename T = typename std::iterator_traits<It>::value_type>
bool if_sorted(Container const &container) {
using std::begin;
using std::end;
It l(end(container));
return l == std::adjacent_find(begin(container),l,std::greater_equal<T>());
}
int main()
{
std::vector<int> v(10);
do std::generate_n(v.begin(), v.size(), rand);
while (!if_sorted(v));
std::cout << "Yay, found a vector that was accidentally sorted: \n";
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " "));
}
인쇄 무작위 분류,
Yay, found a vector that was accidentally sorted:
338521972 564591732 631716774 818567430 923731840 1036769782 1094628595 1228665979 1863863464 2024865477
1. 당신을 FO 추천 정수의 시퀀스 질문을 진술하십시오. 2.'i'를 초기화하는 것을 잊었습니다. –
다음을보십시오 : http://en.cppreference.com/w/cpp/algorithm/is_sorted – taocp
is_sorted 또는 다른 기능을 사용하고 싶지 않습니다. 'iostream' 라이브러리에서만. 'i'는 for-loop 바로 위에서 초기화됩니다. – user3653164