STL中算法锦集(四)

STL中算法锦集(四)

文章目录

  • STL中算法锦集(四)
    • 一、< algorithm >
      • 1.std::is_permutation
      • 2.std::is_sorted
      • 3.std::is_sorted_until
      • 4.std::iter_swap
      • 5.std::lexicographical_compare
      • 6.std::lower_bound
      • 7.std::make_heap
      • 8.std::max_element

一、< algorithm >

STL中算法锦集(四)_第1张图片

1.std::is_permutation

  • 原型:
template <class ForwardIterator1, class ForwardIterator2>
   bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2);
	
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, BinaryPredicate pred);
  • 参数:
  • ForwardIterator1 first1:区间1的开始
  • ForwardIterator1 last1:区间1的结束
  • ForwardIterator2 first2: 区间2的开始
  • BinaryPredicate pred:自定义比较规则,可以是函数指针或者仿函数对象、lambda表达式
  • 作用:用来检查一个序列是不是另一个序列的排列,
  • 返回值:如果是,会返回 true
  • 实现:
template <class InputIterator1, class InputIterator2>
  bool is_permutation (InputIterator1 first1, InputIterator1 last1,
                       InputIterator2 first2)
{
  std::tie (first1,first2) = std::mismatch (first1,last1,first2);
  if (first1==last1) return true;
  InputIterator2 last2 = first2; std::advance (last2,std::distance(first1,last1));
  for (InputIterator1 it1=first1; it1!=last1; ++it1) {
    if (std::find(first1,it1,*it1)==it1) {
      auto n = std::count (first2,last2,*it1);
      if (n==0 || std::count (it1,last1,*it1)!=n) return false;
    }
  }
  return true;
}
  • 案例:
// is_permutation example
#include      // std::cout
#include     // std::is_permutation
#include         // std::array

int main () {
  std::array<int,5> foo = {1,2,3,4,5};
  std::array<int,5> bar = {3,1,4,5,2};

  if ( std::is_permutation (foo.begin(), foo.end(), bar.begin()) )
    std::cout << "foo and bar contain the same elements.\n";

  return 0;
}

Output:
foo and bar contain the same elements.

2.std::is_sorted

  • 原型:
template <class ForwardIterator>
  bool is_sorted (ForwardIterator first, ForwardIterator last);

template <class ForwardIterator, class Compare>
  bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);
  • 参数:
  • ForwardIterator first:区间的开始
  • ForwardIterator last:区间的结束
  • Compare comp:自定义比较规则,可以是函数指针或者仿函数对象、lambda表达式
  • 作用:判断[first,last)区间内的元素是否满足comp的排序规则
  • 返回值:是返回true
  • 实现:
template <class ForwardIterator>
  bool is_sorted (ForwardIterator first, ForwardIterator last)
{
  if (first==last) return true;
  ForwardIterator next = first;
  while (++next!=last) {
    // or, if (comp(*next,*first))
    if (*next<*first)     
      return false;
    ++first;
  }
  return true;
}
  • 案例:
// is_sorted example
#include      // std::cout
#include     // std::is_sorted, std::prev_permutation
#include         // std::array

int main () {
  std::array<int,4> foo {2,4,1,3};

  do {
    // try a new permutation:
    std::prev_permutation(foo.begin(),foo.end());

    // print range:
    std::cout << "foo:";
    for (int& x:foo) std::cout << ' ' << x;
    std::cout << '\n';

  } while (!std::is_sorted(foo.begin(),foo.end()));

  std::cout << "the range is sorted!\n";

  return 0;
}

Output:
foo: 2 3 4 1
foo: 2 3 1 4
foo: 2 1 4 3
foo: 2 1 3 4
foo: 1 4 3 2
foo: 1 4 2 3
foo: 1 3 4 2
foo: 1 3 2 4
foo: 1 2 4 3
foo: 1 2 3 4
the range is sorted!

3.std::is_sorted_until

  • 原型:
template <class ForwardIterator>
  ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last);

template <class ForwardIterator, class Compare>
  ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last,
                                   Compare comp);
  • 参数:
  • ForwardIterator first:区间的开始
  • ForwardIterator last:区间的结束
  • Compare comp:自定义比较规则,可以是函数指针或者仿函数对象、lambda表达式
  • 作用:能检测出[first,last)区间是否满足come规则有序
  • 返回值:还会返回一个正向迭代器,该迭代器指向的是当前序列中第一个破坏有序状态的元素。
  • 实现:
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;
}
  • 案例:
// is_sorted_until example
#include      // std::cout
#include     // std::is_sorted_until, std::prev_permutation
#include         // std::array

int main () {
  std::array<int,4> foo {2,4,1,3};
  std::array<int,4>::iterator it;

  do {
    // try a new permutation:
    std::prev_permutation(foo.begin(),foo.end());

    // print range:
    std::cout << "foo:";
    for (int& x:foo) std::cout << ' ' << x;
    it=std::is_sorted_until(foo.begin(),foo.end());
    std::cout << " (" << (it-foo.begin()) << " elements sorted)\n";

  } while (it!=foo.end());

  std::cout << "the range is sorted!\n";

  return 0;
}

Output:
foo: 2 3 4 1 (3 elements sorted)
foo: 2 3 1 4 (2 elements sorted)
foo: 2 1 4 3 (1 elements sorted)
foo: 2 1 3 4 (1 elements sorted)
foo: 1 4 3 2 (2 elements sorted)
foo: 1 4 2 3 (2 elements sorted)
foo: 1 3 4 2 (3 elements sorted)
foo: 1 3 2 4 (2 elements sorted)
foo: 1 2 4 3 (3 elements sorted)
foo: 1 2 3 4 (4 elements sorted)
the range is sorted!

4.std::iter_swap

  • 原型:
template <class ForwardIterator1, class ForwardIterator2>
  void iter_swap (ForwardIterator1 a, ForwardIterator2 b);
  • 参数:
  • ForwardIterator1 a:区间的开始
  • ForwardIterator1 b:区间的结束
  • 作用:交换a,b两个迭代器的元素内容
  • 返回值:没有返回值
  • 实现:
template <class ForwardIterator1, class ForwardIterator2>
  void iter_swap (ForwardIterator1 a, ForwardIterator2 b)
{
  swap (*a, *b);
}
  • 案例:
// iter_swap example
#include      // std::cout
#include     // std::iter_swap
#include        // std::vector

int main () {

  int myints[]={10,20,30,40,50 };              //   myints:  10  20  30  40  50
  std::vector<int> myvector (4,99);            // myvector:  99  99  99  99

  std::iter_swap(myints,myvector.begin());     //   myints: [99] 20  30  40  50
                                               // myvector: [10] 99  99  99

  std::iter_swap(myints+3,myvector.begin()+2); //   myints:  99  20  30 [99] 50
                                               // myvector:  10  99 [40] 99

  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

Output:
myvector contains: 10 99 40 99

5.std::lexicographical_compare

  • 原型:
template <class InputIterator1, class InputIterator2>
  bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,
                                InputIterator2 first2, InputIterator2 last2);
	
template <class InputIterator1, class InputIterator2, class Compare>
  bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,
                                InputIterator2 first2, InputIterator2 last2,
                                Compare comp);
  • 参数:
  • InputIterator1 first1:区间1的开始
  • InputIterator1 last1:区间1的结束
  • InputIterator2 first2:区间2的开始
  • InputIterator2 last2:区间2的结束
  • Compare comp:自定义比较规则,可以是函数指针或者仿函数对象、lambda表达式
  • 作用:求[first1,last1)区间的字典序是否小于[first2,last2)区间的元素
  • 返回值:是就返回true
  • 实现:
template <class InputIterator1, class InputIterator2>
  bool lexicographical_compare (InputIterator1 first1, InputIterator1 last1,
                                InputIterator2 first2, InputIterator2 last2)
{
  while (first1!=last1)
  {
    if (first2==last2 || *first2<*first1) return false;
    else if (*first1<*first2) return true;
    ++first1; ++first2;
  }
  return (first2!=last2);
}
  • 案例:
// lexicographical_compare example
#include      // std::cout, std::boolalpha
#include     // std::lexicographical_compare
#include        // std::tolower

// a case-insensitive comparison function:
bool mycomp (char c1, char c2)
{ return std::tolower(c1)<std::tolower(c2); }

int main () {
  char foo[]="Apple";
  char bar[]="apartment";

  std::cout << std::boolalpha;

  std::cout << "Comparing foo and bar lexicographically (foo;

  std::cout << "Using default comparison (operator<): ";
  std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9);
  std::cout << '\n';

  std::cout << "Using mycomp as comparison object: ";
  std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9,mycomp);
  std::cout << '\n';

  return 0;
}

Output:
Comparing foo and bar lexicographically (foo<bar):
Using default comparison (operator<): true
Using mycomp as comparison object: false

6.std::lower_bound

  • 原型:
template <class ForwardIterator, class T>
  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
                               const T& val);

template <class ForwardIterator, class T, class Compare>
  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
                               const T& val, Compare comp);
  • 参数:
  • ForwardIterator first:区间的开始
  • ForwardIterator last:区间的结束
  • const T& val:
  • Compare comp:自定义比较规则,可以是函数指针或者仿函数对象、lambda表达式
  • 作用:在first和last中的前闭后开区间进行二分查找
  • 返回值:返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置.
  • 实现:
template <class ForwardIterator, class T>
  ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
  ForwardIterator it;
  iterator_traits<ForwardIterator>::difference_type count, step;
  count = distance(first,last);
  while (count>0)
  {
    it = first; step=count/2; advance (it,step);
    if (*it<val) {                 // or: if (comp(*it,val)), for version (2)
      first=++it;
      count-=step+1;
    }
    else count=step;
  }
  return first;
}
  • 案例:
// lower_bound/upper_bound example
#include      // std::cout
#include     // std::lower_bound, std::upper_bound, std::sort
#include        // std::vector

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  std::vector<int> v(myints,myints+8);           // 10 20 30 30 20 10 10 20

  std::sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30

  std::vector<int>::iterator low,up;
  low=std::lower_bound (v.begin(), v.end(), 20); //          ^
  up= std::upper_bound (v.begin(), v.end(), 20); //                   ^

  std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
  std::cout << "upper_bound at position " << (up - v.begin()) << '\n';

  return 0;
}

Output:
lower_bound at position 3
upper_bound at position 6

7.std::make_heap

  • 原型:
template <class RandomAccessIterator>
  void make_heap (RandomAccessIterator first, RandomAccessIterator last);

template <class RandomAccessIterator, class Compare>
  void make_heap (RandomAccessIterator first, RandomAccessIterator last,
                  Compare comp );
  • 参数:
  • RandomAccessIterator first:区间的开始
  • RandomAccessIterator last:区间的结束
  • Compare comp:自定义比较规则,可以是函数指针或者仿函数对象、lambda表达式
  • 作用:根据[first,last)区间的元素构建一个comp规则的堆

  • 返回值:没有返回值

  • 案例:

// range heap example
#include      // std::cout
#include     // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include        // std::vector

int main () {
  int myints[] = {10,20,30,5,15};
  std::vector<int> v(myints,myints+5);

  std::make_heap (v.begin(),v.end());
  std::cout << "initial max heap   : " << v.front() << '\n';

  std::pop_heap (v.begin(),v.end()); v.pop_back();
  std::cout << "max heap after pop : " << v.front() << '\n';

  v.push_back(99); std::push_heap (v.begin(),v.end());
  std::cout << "max heap after push: " << v.front() << '\n';

  std::sort_heap (v.begin(),v.end());

  std::cout << "final sorted range :";
  for (unsigned i=0; i<v.size(); i++)
    std::cout << ' ' << v[i];

  std::cout << '\n';

  return 0;
}
 Edit & Run


Output:
initial max heap   : 30
max heap after pop : 20
max heap after push: 99
final sorted range : 5 10 15 20 99

8.std::max_element

  • 原型:
template <class ForwardIterator>
  ForwardIterator max_element (ForwardIterator first, ForwardIterator last);

template <class ForwardIterator, class Compare>
  ForwardIterator max_element (ForwardIterator first, ForwardIterator last,
                               Compare comp);
  • 参数:
  • ForwardIterator first:区间的开始
  • ForwardIterator last:区间的结束
  • Compare comp:自定义比较规则,可以是函数指针或者仿函数对象、lambda表达式
  • 作用:求[first,last)区间内最大的元素
  • 返回值:返回区间内最大的元素的迭代器
  • 实现:
template <class ForwardIterator>
  ForwardIterator max_element ( ForwardIterator first, ForwardIterator last )
{
  if (first==last) return last;
  ForwardIterator largest = first;

  while (++first!=last)
    if (*largest<*first)    // or: if (comp(*largest,*first)) for version (2)
      largest=first;
  return largest;
}
  • 案例:
// min_element/max_element example
#include      // std::cout
#include     // std::min_element, std::max_element

bool myfn(int i, int j) { return i<j; }

struct myclass {
  bool operator() (int i,int j) { return i<j; }
} myobj;

int main () {
  int myints[] = {3,7,2,5,6,4,9};

  // using default comparison:
  std::cout << "The smallest element is " << *std::min_element(myints,myints+7) << '\n';
  std::cout << "The largest element is "  << *std::max_element(myints,myints+7) << '\n';

  // using function myfn as comp:
  std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myfn) << '\n';
  std::cout << "The largest element is "  << *std::max_element(myints,myints+7,myfn) << '\n';

  // using object myobj as comp:
  std::cout << "The smallest element is " << *std::min_element(myints,myints+7,myobj) << '\n';
  std::cout << "The largest element is "  << *std::max_element(myints,myints+7,myobj) << '\n';

  return 0;
}
 Edit & Run


Output:
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9
The smallest element is 2
The largest element is 9

你可能感兴趣的:(C/C++)