使用一个东西,不明白它的道理,不高明
——侯捷老师
1. is_sorted()函数
功能:判断序列[first, last)是否是排序的
1.1 函数声明
// default(1)
template <class ForwardIterator>
bool is_sorted (ForwardIterator first, ForwardIterator last);
// custom(2)
template <class ForwardIterator, class Compare>
bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);
1.2 等价操作实现
template <class ForwardIterator>
bool is_sorted (ForwardIterator first, ForwardIterator last)
{
if (first==last) return true;
ForwardIterator next = first;
while (++next!=last) {
if (*next<*first) // or, if (comp(*next,*first)) for version (2)
return false;
++first;
}
return true;
}
1.3 示例程式
void test_is_sorted() {
std::array<int,4> foo {2, 4, 1, 3};
do {
std::prev_permutation(foo.begin(), foo.end());
cout << "foo: ";
for (auto& x : foo) {
cout << " " << x;
}
cout << endl;
} while (!std::is_sorted(foo.begin(), foo.end()));
cout << "the range is sorted." << endl;
}
输出结果:
image.png
1.4 源码探究
发现内部调用的是is_sorted_until()函数
template<typename _ForwardIterator, typename _Compare>
inline bool
is_sorted(_ForwardIterator __first, _ForwardIterator __last,
_Compare __comp)
{ return std::is_sorted_until(__first, __last, __comp) == __last; }
1.5 参考链接
2. is_sorted_until()函数
功能:返回第一个不满足排序的元素迭代器
2.1 函数声明
// default (1)
template <class ForwardIterator>
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last);
// custom (2)
template <class ForwardIterator, class Compare>
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last,
Compare comp);
2.2 等价操作实现
template <class ForwardIterator>
ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last)
{
if (first==last) return first;
ForwardIterator next = first;
while (++next!=last) {
if (*next<*first) return next;
++first;
}
return last;
}
2.3 示例程式
void test_is_sorted_until() {
std::array<int,4> foo = {2,4,1,3};
std::array<int,4>::iterator it;
do {
std::prev_permutation(foo.begin(), foo.end());
cout << "foo: ";
for (auto& x: foo) {
cout << " " << x;
}
it = std::is_sorted_until(foo.begin(), foo.end());
cout << "( " << (it - foo.begin()) << " elements sorted" << endl;
} while (it != foo.end());
cout << "the range is sorted" << endl;
}
输出结果:
image.png
2.4 源码探究
is_sorted_until函数
template<typename _ForwardIterator, typename _Compare>
inline _ForwardIterator
is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
_Compare __comp)
{
// concept requirements
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
__glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
typename iterator_traits<_ForwardIterator>::value_type,
typename iterator_traits<_ForwardIterator>::value_type>)
__glibcxx_requires_valid_range(__first, __last);
return std::__is_sorted_until(__first, __last,
__gnu_cxx::__ops::__iter_comp_iter(__comp));
}
__is_sorted_until()函数:
template<typename _ForwardIterator, typename _Compare>
_ForwardIterator
__is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
_Compare __comp)
{
if (__first == __last)
return __last;
_ForwardIterator __next = __first;
for (++__next; __next != __last; __first = __next, ++__next)
if (__comp(__next, __first))
return __next;
return __next;
}
2.5 参考链接
http://www.cplusplus.com/reference/algorithm/is_sorted_until/