C++(count;count_if;mismatch;equal;is_permutation;next_permutation;prev_permutation;search;search_n)

一、count

头文件algorithm

template <class InputIterator, class T>
  typename iterator_traits<InputIterator>::difference_type
    count (InputIterator first, InputIterator last, const T& val);

计算范围内的值出现的次数

返回比较等于val的[first,last]范围内的元素数。

该函数使用operator ==将各个元素与val进行比较。

此函数模板的行为等效于:

template <class InputIterator, class T>
  typename iterator_traits<InputIterator>::difference_type
    count (InputIterator first, InputIterator last, const T& val)
{
  typename iterator_traits<InputIterator>::difference_type ret = 0;
  while (first!=last) {
    if (*first == val) ++ret;
    ++first;
  }
  return ret;
}

参数

  1. first,last
    将迭代器输入到元素序列的初始位置和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
  2. val
    要匹配的价值。
    T应该是一种支持与InputIterator指向的元素进行比较的类型,使用operator ==(元素为左侧操作数,val为右侧)。

返回值

比较等于val的[first,last]范围内的元素数。返回类型(iterator_traits :: difference_type)是有符号整数类型。

// count algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::count
#include <vector>       // std::vector

int main () {
  // counting elements in array:
  int myints[] = {10,20,30,30,20,10,10,20};   // 8 elements
  int mycount = std::count (myints, myints+8, 10);
  std::cout << "10 appears " << mycount << " times.\n";

  // counting elements in container:
  std::vector<int> myvector (myints, myints+8);
  mycount = std::count (myvector.begin(), myvector.end(), 20);
  std::cout << "20 appears " << mycount  << " times.\n";

  return 0;
}

在这里插入图片描述

二、count_if

头文件algorithm

template <class InputIterator, class UnaryPredicate>
  typename iterator_traits<InputIterator>::difference_type
    count_if (InputIterator first, InputIterator last, UnaryPredicate pred);

返回满足条件的范围内的元素数

返回pred为true的范围[first,last]中的元素数。

此函数模板的行为等效于:

template <class InputIterator, class UnaryPredicate>
  typename iterator_traits<InputIterator>::difference_type
    count_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  typename iterator_traits<InputIterator>::difference_type ret = 0;
  while (first!=last) {
    if (pred(*first)) ++ret;
    ++first;
  }
  return ret;
}

参数

  1. first,last
    将迭代器输入到元素序列的初始位置和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
  2. pred
    一元函数接受范围内的元素作为参数,并返回可转换为bool的值。 返回的值表示此函数是否计算元素。

该函数不得修改其参数。这可以是函数指针或函数对象。

返回值

pred [first,last]范围内的元素数量,其中pred不返回false。返回类型(iterator_traits :: difference_type)是有符号整数类型。

// count_if example
#include <iostream>     // std::cout
#include <algorithm>    // std::count_if
#include <vector>       // std::vector

bool IsOdd (int i) { return ((i%2)==1); }

int main () {
  std::vector<int> myvector;
  for (int i=1; i<10; i++) myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9

  int mycount = count_if (myvector.begin(), myvector.end(), IsOdd);
  std::cout << "myvector contains " << mycount  << " odd values.\n";

  return 0;
}

在这里插入图片描述

三、mismatch

头文件algorithm

equality (1)	
template <class InputIterator1, class InputIterator2>
  pair<InputIterator1, InputIterator2>
    mismatch (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2);
predicate (2)	
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  pair<InputIterator1, InputIterator2>
    mismatch (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2, BinaryPredicate pred);

返回两个范围中相互不同的第一个位置

将[first1,last1]范围内的元素与first2开始范围内的元素进行比较,并返回两个不匹配的序列的第一个元素。

使用operator ==(或版本(2)中的pred)比较元素。

该函数将一对迭代器返回到每个范围中不匹配的第一个元素。

此函数模板的行为等效于:

template <class InputIterator1, class InputIterator2>
  pair<InputIterator1, InputIterator2>
    mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
  while ( (first1!=last1) && (*first1==*first2) )  // or: pred(*first1,*first2), for version 2
  { ++first1; ++first2; }
  return std::make_pair(first1,first2);
}

参数

  1. first1,last1
    将迭代器输入到第一个序列的初始和最终位置。 使用的范围是[first1,last1],它包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。
  2. first2
    将迭代器输入到第二个序列的初始位置。 该函数最多可以访问[first1,last1]范围内的元素。
  3. pred
    二进制函数,接受两个元素作为参数(两个序列中的每个序列中的一个,顺序相同),并返回一个可转换为bool的值。 返回的值指示在此函数的上下文中是否认为元素匹配。

该函数不得修改其任何参数。这可以是函数指针或函数对象。

返回值

一对,其成员的第一个和第二个指向两个序列中的第一个元素,它们彼此不相等。如果在两个序列中比较的元素都匹配,则该函数返回一对,其中第一个设置为last1,第二个设置为第二个序列中相同相对位置的元素。如果没有匹配,则返回make_pair(first1,first2)。

// mismatch algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::mismatch
#include <vector>       // std::vector
#include <utility>      // std::pair

bool mypredicate (int i, int j) {
  return (i==j);
}

int main () {
  std::vector<int> myvector;
  for (int i=1; i<6; i++) myvector.push_back (i*10); // myvector: 10 20 30 40 50

  int myints[] = {10,20,80,320,1024};                //   myints: 10 20 80 320 1024

  std::pair<std::vector<int>::iterator,int*> mypair;

  // using default comparison:
  mypair = std::mismatch (myvector.begin(), myvector.end(), myints);
  std::cout << "First mismatching elements: " << *mypair.first;
  std::cout << " and " << *mypair.second << '\n';

  ++mypair.first; ++mypair.second;

  // using predicate comparison:
  mypair = std::mismatch (mypair.first, myvector.end(), mypair.second, mypredicate);
  std::cout << "Second mismatching elements: " << *mypair.first;
  std::cout << " and " << *mypair.second << '\n';

  return 0;
}

在这里插入图片描述

例外

如果任何元素比较(或pred)抛出或迭代器上的任何操作抛出,则抛出。
请注意,无效参数会导致未定义的行为。

四、equal

头文件algorithm

equality (1)	
template <class InputIterator1, class InputIterator2>
  bool equal (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2);
predicate (2)	
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  bool equal (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2, BinaryPredicate pred);

测试两个范围内的元素是否相等

将[first1,last1]范围内的元素与first2开始范围内的元素进行比较,如果两个范围中的所有元素都匹配,则返回true。

使用operator ==(或版本(2)中的pred)比较元素。

此函数模板的行为等效于:

template <class InputIterator1, class InputIterator2>
  bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
{
  while (first1!=last1) {
    if (!(*first1 == *first2))   // or: if (!pred(*first1,*first2)), for version 2
      return false;
    ++first1; ++first2;
  }
  return true;
}

参数

  1. first1,last1
    将迭代器输入到第一个序列的初始和最终位置。 使用的范围是[first1,last1],它包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。
  2. first2
    将迭代器输入到第二个序列的初始位置。 该比较包括该序列中与[first1,last1]范围内的元素一样多的元素。
  3. pred
    二进制函数,接受两个元素作为参数(两个序列中的每个序列中的一个,顺序相同),并返回一个可转换为bool的值。 返回的值指示在此函数的上下文中是否认为元素匹配。

该函数不得修改其任何参数。这可以是函数指针或函数对象。

返回值

如果[first1,last1]范围内的所有元素与从first2开始的范围中的所有元素进行比较,则为true,否则为false。

// equal algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::equal
#include <vector>       // std::vector

bool mypredicate (int i, int j) {
  return (i==j);
}

int main () {
  int myints[] = {20,40,60,80,100};               //   myints: 20 40 60 80 100
  std::vector<int>myvector (myints,myints+5);     // myvector: 20 40 60 80 100

  // using default comparison:
  if ( std::equal (myvector.begin(), myvector.end(), myints) )
    std::cout << "The contents of both sequences are equal.\n";
  else
    std::cout << "The contents of both sequences differ.\n";

  myvector[3]=81;                                 // myvector: 20 40 60 81 100

  // using predicate comparison:
  if ( std::equal (myvector.begin(), myvector.end(), myints, mypredicate) )
    std::cout << "The contents of both sequences are equal.\n";
  else
    std::cout << "The contents of both sequences differ.\n";

  return 0;
}

在这里插入图片描述

例外

如果任何元素比较(或pred)抛出,或者迭代器上的任何操作抛出,则抛出。
请注意,无效参数会导致未定义的行为。

五、is_permutation

头文件algorithm

equality (1)	
template <class ForwardIterator1, class ForwardIterator2>
   bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,
                        ForwardIterator2 first2);
predicate (2)	
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,
                        ForwardIterator2 first2, BinaryPredicate pred);

测试范围是否是另一个的排列

将[first1,last1]范围内的元素与first2开始范围内的元素进行比较,如果两个范围中的所有元素都匹配,则返回true,即使顺序不同。

使用operator ==(或版本(2)中的pred)比较元素。

此函数模板的行为等效于:

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;
}

参数

  1. first1,last1
    将迭代器输入到第一个序列的初始和最终位置。使用的范围是[first1,last1],它包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。
  2. first2
    将迭代器输入到第二个序列的初始位置。
    该函数考虑该序列中与[first1,last1]范围内的元素一样多的元素。
    如果此序列较短,则会导致未定义的行为。
  3. pred
    二进制函数,接受两个元素作为参数(两个序列中的每个序列中的一个,顺序相同),并返回一个可转换为bool的值。返回的值指示在此函数的上下文中是否认为元素匹配。

该函数不得修改其任何参数。这可以是函数指针或函数对象。

InputIterator1和InputIterator2应指向相同的类型。

返回值

如果[first1,last1]范围内的所有元素与以任何顺序从first2开始的范围中的所有元素进行比较,则为true,否则为false。

// is_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::is_permutation
#include <array>        // 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;
}

在这里插入图片描述

六、next_permutation

头文件algorithm

default (1)	
template <class BidirectionalIterator>
  bool next_permutation (BidirectionalIterator first,
                         BidirectionalIterator last);
custom (2)	
template <class BidirectionalIterator, class Compare>
  bool next_permutation (BidirectionalIterator first,
                         BidirectionalIterator last, Compare comp);

将范围转换为下一个排列

将[first,last]范围内的元素重新排列为下一个按字典顺序排列的更大排列。

排列是N中的每一个!元素可以采取的可能安排(其中N是范围内元素的数量)。可以根据他们如何比较词典相互比较来排序不同的排列;第一个这样排序的可能排列(将字典比较小于所有其他排列的那个排列)是使其所有元素按升序排序的排序,并且最大排列的所有元素按降序排序。

各个元素的比较使用operator <用于第一个版本,或comp用于第二个版本。

如果函数可以确定下一个更高的排列,它会重新排列元素并返回true。如果那是不可能的(因为它已经处于最大可能的排列),它会根据第一个排列重新排列元素(按升序排序)并返回false。

参数

  1. first,last
    双向迭代器到序列的初始和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
    BidirectionalIterator应指向正确定义交换的类型。
  2. comp
    二进制函数,接受BidirectionalIterator指向的类型的两个参数,并返回一个可转换为bool的值。 返回的值指示第一个参数是否在其定义的特定严格弱顺序中被认为是在第二个参数之前。

该函数不得修改其任何参数。这可以是函数指针或函数对象。

返回值

如果函数可以将对象重新排列为lexicographicaly更大的排列,则为true。否则,该函数返回false以指示排列不大于前一个,但最低可能(按升序排序)。

// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort

int main () {
  int myints[] = {1,2,3};

  std::sort (myints,myints+3);

  std::cout << "The 3! possible permutations with 3 elements:\n";
  do {
    std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
  } while ( std::next_permutation(myints,myints+3) );

  std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';

  return 0;
}
// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort

int main () {
  int myints[] = {1,2,3};

  std::sort (myints,myints+3);

  std::cout << "The 3! possible permutations with 3 elements:\n";
  do {
    std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
  } while ( std::next_permutation(myints,myints+3) );

  std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';

  return 0;
}

在这里插入图片描述

例外

如果任何元素交换抛出或迭代器上的任何操作抛出,则抛出。请注意,无效参数会导致未定义的行为。

七、prev_permutation

头文件algorithm

default (1)	
template <class BidirectionalIterator>
  bool prev_permutation (BidirectionalIterator first,
                         BidirectionalIterator last );
custom (2)	
template <class BidirectionalIterator, class Compare>
  bool prev_permutation (BidirectionalIterator first,
                         BidirectionalIterator last, Compare comp);

将范围转换为先前的排列

将[first,last]范围内的元素重新排列为上一个按字典顺序排列的排列。

排列是N中的每一个!元素可以采取的可能安排(其中N是范围内元素的数量)。可以根据他们如何比较词典相互比较来排序不同的排列;第一个这样排序的可能排列(将字典比较小于所有其他排列的那个排列)是使其所有元素按升序排序的排序,并且最大排列的所有元素按降序排序。

各个元素的比较使用operator <用于第一个版本,或comp用于第二个版本。

如果函数可以确定先前的排列,则它会重新排列元素并返回true。如果那是不可能的(因为它已经处于最低可能的排列),它会根据最后的排列重新排列元素(按降序排序)并返回false。

参数

  1. first,last
    双向迭代器到序列的初始和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
    BidirectionalIterator应指向正确定义交换的类型。
  2. comp
    二进制函数,接受BidirectionalIterator指向的类型的两个参数,并返回一个可转换为bool的值。 返回的值指示第一个参数是否在其定义的特定严格弱顺序中被认为是在第二个参数之前。

该函数不得修改其任何参数。这可以是函数指针或函数对象。

返回值

如果函数可以将对象重新排列为lexicographicaly较小的排列,则为true。否则,该函数返回false以指示排列不小于前一个,但最大可能(按降序排序)。

// prev_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort, std::reverse

int main () {
  int myints[] = {1,2,3};

  std::sort (myints,myints+3);
  std::reverse (myints,myints+3);

  std::cout << "The 3! possible permutations with 3 elements:\n";
  do {
    std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
  } while ( std::prev_permutation(myints,myints+3) );

  std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';

  return 0;
}

在这里插入图片描述

八、search

头文件algorithm

equality (1)	
template <class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
                            ForwardIterator2 first2, ForwardIterator2 last2);
predicate (2)	
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
                            ForwardIterator2 first2, ForwardIterator2 last2,
                            BinaryPredicate pred);

子序列的搜索范围

搜索范围[first1,last1],查找[first2,last2]定义的序列的第一次出现,并将迭代器返回到其第一个元素,如果没有找到则出现,则返回last1。

使用operator ==(或者版本(2)中的pred)顺序比较两个范围中的元素:[first1,last1]的子序列仅在[first2,last2]的所有元素都为真时才被视为匹配。

此函数返回第一个此类事件。 对于返回最后一个的算法,请参阅find_end。

此函数模板的行为等效于:

template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1,
                            ForwardIterator2 first2, ForwardIterator2 last2)
{
  if (first2==last2) return first1;  // specified in C++11
  
  while (first1!=last1)
  {
    ForwardIterator1 it1 = first1;
    ForwardIterator2 it2 = first2;
    while (*it1==*it2) {    // or: while (pred(*it1,*it2)) for version 2
        if (it2==last2) return first1;
        if (it1==last1) return last1;
        ++it1; ++it2;
    }
    ++first1;
  }
  return last1;
}

参数

  1. first1,last1
    将迭代器转发到搜索序列的初始和最终位置。使用的范围是[first1,last1],它包含first1和last1之间的所有元素,包括first1指向的元素,但不包括last1指向的元素。
  2. first2,last2
    将迭代器转发到要搜索的序列的初始位置和最终位置。使用的范围是[first2,last2)。
    对于(1),两个范围中的元素应具有可比较的类型,使用operator ==(第一个范围的元素作为左侧操作数,第二个元素的元素作为右侧操作数)。
  3. pred
    二进制函数,接受两个元素作为参数(两个序列中的每个序列中的一个,顺序相同),并返回一个可转换为bool的值。返回值指示在此函数的上下文中是否认为元素匹配。

该函数不得修改其任何参数。这可以是函数指针或函数对象。

返回值

[first1,last1]中第一次出现[first2,last2]的第一个元素的迭代器。如果未找到序列,则函数返回last1。
【C++ 98】如果[first2,last2)是空范围,则结果未指定。
【C++ 11】如果[first2,last2)是空范围,则该函数返回first1。

// search algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::search
#include <vector>       // std::vector

bool mypredicate (int i, int j) {
  return (i==j);
}

int main () {
  std::vector<int> haystack;

  // set some values:        haystack: 10 20 30 40 50 60 70 80 90
  for (int i=1; i<10; i++) haystack.push_back(i*10);

  // using default comparison:
  int needle1[] = {40,50,60,70};
  std::vector<int>::iterator it;
  it = std::search (haystack.begin(), haystack.end(), needle1, needle1+4);

  if (it!=haystack.end())
    std::cout << "needle1 found at position " << (it-haystack.begin()) << '\n';
  else
    std::cout << "needle1 not found\n";

  // using predicate comparison:
  int needle2[] = {20,30,50};
  it = std::search (haystack.begin(), haystack.end(), needle2, needle2+3, mypredicate);

  if (it!=haystack.end())
    std::cout << "needle2 found at position " << (it-haystack.begin()) << '\n';
  else
    std::cout << "needle2 not found\n";

  return 0;
}

在这里插入图片描述

例外

如果任何元素比较(或pred)抛出或迭代器上的任何操作抛出,则抛出。请注意,无效参数会导致未定义的行为。

九、search_n

头文件algorithm

equality (1)	
template <class ForwardIterator, class Size, class T>
   ForwardIterator search_n (ForwardIterator first, ForwardIterator last,
                             Size count, const T& val);
predicate (2)	
template <class ForwardIterator, class Size, class T, class BinaryPredicate>
   ForwardIterator search_n ( ForwardIterator first, ForwardIterator last,
                              Size count, const T& val, BinaryPredicate pred );

搜索元素的范围

在[first,last]范围内搜索计数元素序列,每个计数元素的比较等于val(或者pred返回true)。

该函数返回第一个这样的元素的迭代器,如果没有找到这样的序列,则返回last。

此函数模板的行为等效于:

template<class ForwardIterator, class Size, class T>
  ForwardIterator search_n (ForwardIterator first, ForwardIterator last,
                            Size count, const T& val)
{
  ForwardIterator it, limit;
  Size i;

  limit=first; std::advance(limit,std::distance(first,last)-count);

  while (first!=limit)
  {
    it = first; i=0;
    while (*it==val)       // or: while (pred(*it,val)) for the pred version
      { ++it; if (++i==count) return first; }
    ++first;
  }
  return last;
}

参数

  1. first,last
    将迭代器转发到搜索序列的初始和最终位置。使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
  2. count
    要匹配的最小连续元素数。尺寸应为(可转换为)整体类型。
  3. val
    要比较的单个值,或用作pred的参数(在第二个版本中)。
    对于第一个版本,T应该是一种类型,支持使用operator ==与InputIterator指向的元素进行比较(元素为左侧大小操作数,val为右侧)。
  4. pred
    二进制函数接受两个参数(序列中的一个元素作为第一个,val作为第二个),并返回一个可转换为bool的值。返回的值指示元素是否在此函数的上下文中被视为匹配。

该函数不得修改其任何参数。这可以是函数指针或函数对象。

返回值

序列的第一个元素的迭代器。如果未找到此类序列,则该函数最后返回。

// search_n example
#include <iostream>     // std::cout
#include <algorithm>    // std::search_n
#include <vector>       // std::vector

bool mypredicate (int i, int j) {
  return (i==j);
}

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

  std::vector<int>::iterator it;

  // using default comparison:
  it = std::search_n (myvector.begin(), myvector.end(), 2, 30);

  if (it!=myvector.end())
    std::cout << "two 30s found at position " << (it-myvector.begin()) << '\n';
  else
    std::cout << "match not found\n";

  // using predicate comparison:
  it = std::search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate);

  if (it!=myvector.end())
    std::cout << "two 10s found at position " << int(it-myvector.begin()) << '\n';
  else
    std::cout << "match not found\n";

  return 0;
}

在这里插入图片描述

例外

如果任何元素比较(或pred)抛出或迭代器上的任何操作抛出,则抛出。请注意,无效参数会导致未定义的行为。

猜你喜欢

转载自blog.csdn.net/baidu_34884208/article/details/87888211