STLalgorithm算法equal(12)


STLalgorithm算法equal(12)

原文地址:http://www.cplusplus.com/reference/algorithm/equal/

function template
<algorithm>

std::equal

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);
Test whether the elements in two ranges are equal

Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if all of the elements in both ranges match.
比较范围内的元素和first2开始的范围的元素是否相等,如果范围内全部匹配,则返回true,否则返回false.
例子:

#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
using namespace std;

void equal2(){
    vector<int> vi{1,5,7,8,9,9,8,5,9};
    array<int,4> ai{1,5,7,8};
    vector<double> v2{5,7,8,9};
    cout<<"vi=";
    for(int &i:vi)
        cout<<i<<" ";
    cout<<endl;

    cout<<"ai=";
    for(int &i:ai)
        cout<<i<<" ";
    cout<<endl;

    if(equal(ai.begin(),ai.end(),vi.begin()))
        cout<<"equal(ai.begin(),ai.end(),vi.begin() return ture!"<<endl;
    else
        cout<<"equal(ai.begin(),ai.end(),vi.begin() return false!"<<endl;

    cout<<"v2=";
    for(double &i:v2)
        cout<<i<<" ";
    cout<<endl;
    if(equal(v2.begin(),v2.end(),vi.begin()+1))
        cout<<"equal(v2.begin(),v2.end(),vi.begin()+1) return ture!"<<endl;
    else
        cout<<"equal(v2.begin(),v2.end(),vi.begin()+1) return false!"<<endl;


}

运行截图:


The elements are compared using operator== (or pred, in version (2)).
元素使用operator==进行比较(或者pred)

The behavior of this function template is equivalent to:

1
2
3
4
5
6
7
8
9
10
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;
}




Parameters

first1, last1
Input iterators to the initial and final positions of the first sequence. The range
used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
比较的范围。
first2
Input iterator to the initial position of the second sequence. The comparison includes
up to as many elements of this sequence as those in the range [first1,last1).
第二个范围的开头。
pred
Binary function that accepts two elements as argument (one of each of the two sequences, in the same order), and returns a value convertible to bool. The value returned indicates whether
the elements are considered to match in the context of this function.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
一个二元函数符。(接受两个参数并返回一个bool值。)



Return value

true if all the elements in the range [first1,last1) compare equal to those of the range starting at first2, and false otherwise.
如果范围内所有元素均匹配,则返回ture,否则返回false.


Example

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

Output:

The contents of both sequences are equal.
The contents of both sequence differ.



Complexity

Up to linear in the distance between first1 and last1: Compares elements until a mismatch is
found.


Data races

Some (or all) of the objects in both ranges are accessed (once at most).


Exceptions

Throws if any of the element comparisons (or pred) throws, or if any of the operations on iterators throws.
Note that invalid parameters cause undefined behavior.

——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-11

于GDUT

——————————————————————————————————————————————————————————————————


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注