STL algorithm算法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

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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL(标准模板库)提供了许多有用的算法函数,包括以下内容: 1. 非修改性序列操作: `std::all_of`, `std::any_of`, `std::none_of`, `std::for_each`, `std::count`, `std::count_if`, `std::mismatch`, `std::find`, `std::find_if`, `std::find_if_not`, `std::adjacent_find`, `std::search`, `std::search_n`, `std::equal`, `std::is_permutation`, `std::lexicographical_compare`. 2. 修改序列操作:`std::copy`, `std::copy_if`, `std::copy_n`, `std::copy_backward`, `std::move`, `std::move_backward`, `std::fill`, `std::fill_n`, `std::transform`, `std::generate`, `std::generate_n`, `std::replace`, `std::replace_if`, `std::replace_copy`, `std::replace_copy_if`, `std::swap`, `std::swap_ranges`, `std::iter_swap`, `std::reverse`, `std::reverse_copy`, `std::rotate`, `std::rotate_copy`, `std::unique`, `std::unique_copy`. 3. 排序和相关操作:`std::sort`, `std::stable_sort`, `std::partial_sort`, `std::partial_sort_copy`, `std::nth_element`. 4. 二分法操作:`std::lower_bound`, `std::upper_bound`, `std::binary_search`, `std::equal_range`. 5. 堆操作:`std::make_heap`, `std::push_heap`, `std::pop_heap`, `std::sort_heap`. 6. 集合操作:`std::merge`, `std::inplace_merge`, `std::includes`, `std::set_union`, `std::set_intersection`, `std::set_difference`, `std::set_symmetric_difference`. 7. 其他操作:`std::accumulate`, `std::iota`, `std::max`, `std::max_element`, `std::min`, `std::min_element`, `std::next_permutation`, `std::prev_permutation`. 这里只是列举了一些常见的算法函数,还有很多其他函数没有列举出来。这些函数可以在 `<algorithm>` 头文件中找到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值