当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ Algorithm equal()用法及代码示例



描述

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

声明

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

C++98

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

参数

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

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

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

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

返回值

如果范围内的所有元素都返回真第一个最后1等于开始于的范围的那些第2个否则返回false。

异常

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

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

时间复杂度

之间的距离呈线性第一的最后的

示例

下面的例子展示了 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.

相关用法


注:本文由纯净天空筛选整理自 C++ Algorithm Library - equal() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。