原文地址:http://www.cplusplus.com/reference/algorithm/equal/
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);
|
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:
|
|
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
|
|
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
——————————————————————————————————————————————————————————————————