1. 程式人生 > 其它 >c++ std標準庫 演算法<algorithm> 比較是否相等 按條件是否相等 equal()

c++ std標準庫 演算法<algorithm> 比較是否相等 按條件是否相等 equal()

技術標籤:# 4.1 C++c++stl標準庫equal

std::equal

簡介:
如果兩個序列的長度相同,並且對應元素都相等,equal() 演算法會返回 true。

函式原型

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);

官方手冊
http://www.cplusplus.com/reference/algorithm/equal/

使用示例:

#include <iostream>
#include <algorithm>
#include <vector>

using
namespace std; void Origin(vector<int>& v) { v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); v.push_back(6); v.push_back(7); } void Origin2(vector<int>& v) { v.push_back(3); v.push_back(4); v.push_back(5); v.push_back(6); v.push_back
(7); v.push_back(8); v.push_back(9); } bool relationship(int e1, int e2) { int tmp = e1 + 2; return e2 == tmp; } void Print(vector<int>& v) { vector<int>::iterator it; for (it = v.begin(); it != v.end(); it++) cout << *it << ", "; cout << endl; } void main() { vector<int> v1, v2; Origin(v1); Origin2(v2); cout << "vector v1: " << endl; Print(v1); cout << "vector v2: " << endl; Print(v2); bool eq = equal(v1.begin(), v1.end(), v2.begin()); if (eq) { cout << "v1 == v2" << endl; } else { cout << "v1 != to v2" << endl; } bool eq2 = equal(v1.begin(), v1.end(), v2.begin(), relationship); if (eq2) { cout << "v2[i]==v1[i]+2" << endl; } else { cout << "v2[i]!=v1[i]+2" << endl; } }

image-20201217154026838