目录

algorithm::equal()

描述 (Description)

C ++函数std::algorithm::equal()测试两组元素是否相等。 两组的大小不必相等。 它使用二元谓词进行比较。

声明 (Declaration)

以下是std :: algorithm :: equal()函数形式std :: algorithm头的声明。

C++98

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

参数 (Parameters)

  • first1 - 将迭代器输入到第一个序列的初始位置。

  • last1 - 将迭代器输入到第一个序列的最终位置。

  • first2 - 将迭代器输入到第二个序列的初始位置。

  • pred - 一个二元谓词,它接受两个参数并返回一个bool。

返回值

如果first1last1范围内的所有元素都等于从first2开始的范围的元素,则返回true,否则返回false。

异常 (Exceptions)

如果元素比较(或谓词)或迭代器上的操作抛出异常,则抛出异常。

请注意,无效参数会导致未定义的行为。

时间复杂

第一个最后一个之间的距离线性。

例子 (Example)

以下示例显示了std :: algorithm :: equal()函数的用法。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
/* Binary predicate which always returns true */
bool binary_pred(string s1, string s2) {
   return true;
}
int main(void) {
   vector<string> v1 = {"one", "two", "three"};
   vector<string> v2 = {"ONE", "THREE", "THREE"};
   bool result;
   result = equal(v1.begin(), v1.end(), v2.begin(), binary_pred);
   if (result == true)
      cout << "Vector range is equal." << endl;
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

Vector range is equal.
↑回到顶部↑
WIKI教程 @2018