c:if equal
equal()作為STL函數 (equal() as a STL function)
Syntax:
句法:
bool equal(
InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2);
Where,
哪里,
InputIterator1 first = iterator to start of the first sequence range
InputIterator1 first =迭代器,開始第一個序列范圍
InputIterator1 last1 = iterator to end of the first sequence range
InputIterator1 last1 =迭代到第一個序列范圍的結尾
InputIterator2 first2 = iterator to start of the second sequence range
InputIterator2 first2 =迭代器開始第二個序列范圍
Return type: bool
返回類型: bool
True - if all elements are the same in both the ranges
True-如果兩個范圍內的所有元素都相同
False - if all elements are not the same in both the ranges
False-如果兩個范圍內的所有元素都不相同
The above syntax is used to compare elements using standard == operator.
上面的語法用于使用標準==運算符比較元素。
We can also define our user-defined binary predicate (instead of '==') for checking whether equal or not. The syntax with user-defined binary predicate is like below:
我們還可以定義用戶定義的二進制謂詞(而不是'==')來檢查是否相等。 用戶定義的二進制謂詞的語法如下:
(Binary predicate is a function which takes two arguments and returns true or false only.)
(二元謂詞是一個帶有兩個參數并且僅返回true或false的函數。)
bool equal(
InputIterator1 first1,
InputIterator1 last1,
InputIterator2 first2,
BinaryPredicate pred);
Using the above syntax the elements of the corresponding ranges are checked via the predicate.
使用上述語法,可以通過謂詞檢查相應范圍的元素。
So, as we found out the last iterator for the second range not being shared as it will compare only the same number of elements as of range 1. Also, it will check sequentially, which means [1,3,5] and [5,3,1] are not the same. So it has to be in the same order for both the range.
因此,我們發現第二個范圍的最后一個迭代器沒有共享,因為它只會比較范圍1相同數量的元素。而且,它將順序檢查,這意味著[1、3、5]和[5] ,, 3,1]不一樣。 因此,兩個范圍的順序必須相同。
1)使用默認的'=='/'!='運算符 (1) Using default '==' / '!=' operator)
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> arr1{ 3, 2, 1, 4, 5, 6, 7 };
vector<int> arr2{ 3, 2, 1, 4, 5 };
if (equal(arr1.begin(), arr1.begin() + 5, arr2.begin())) {
cout << "both ranges are exactly equal \n";
}
else
cout << "both ranges are not exactly equal \n";
vector<int> arr3{ 1, 2, 3, 4, 5, 6, 7 };
vector<int> arr4{ 1, 2, 3, 4, 5 };
if (equal(arr3.begin(), arr3.end(), arr4.begin())) {
cout << "both ranges are exactly equal \n";
}
else
cout << "both ranges are not exactly equal \n";
return 0;
}
Output:
輸出:
both ranges are exactly equal
both ranges are not exactly equal
In the above program, we have checked two cases and have used the default comparator. In the first case,
在上面的程序中,我們檢查了兩種情況,并使用了默認的比較器。 在第一種情況下,
The first range is arr1.begin() to arr1.begin()+5, i.e., only first five elements of arr1. The second range starts from arr2.begin() and it will check the first five elements only from the starting of range2. Since both are the same thus it's a match.
第一個范圍是arr1.begin()到arr1.begin()+ 5 ,即arr1的僅前五個元素。 第二個范圍從arr2.begin()開始, 它將僅從 range2的開始檢查前五個元素。 由于兩者相同,因此是匹配項。
[3,2,1,4,5]
[3,2,1,4,5]
In the second case since we went for the total range of arr3, thus it was a mismatch.
在第二種情況下,由于我們使用了arr3的總范圍,因此這是不匹配的。
2)使用用戶定義的比較器功能 (2) Using user-defined comparator function)
Here we have taken a use case where we have two vectors for student details with five students each. By using our user-defined predict we are going to check whether both of the lists are equal or not. Both list are said to be equal if each of the student's details matches. To have a match for student details, all the details (roll, name, score) need to be the same.
在這里,我們以一個用例為例,我們有兩個向量用于學生詳細信息,每個向量有五個學生。 通過使用用戶定義的預測,我們將檢查兩個列表是否相等。 如果每個學生的詳細信息都匹配,則兩個列表都相等。 要使學生的詳細信息匹配,所有詳細信息(名次,姓名,分數)必須相同。
#include <bits/stdc++.h>
using namespace std;
class student {
int score;
int roll;
string name;
public:
student()
{
score = 0;
roll = 0;
name = "";
}
student(int sc, int ro, string nm)
{
score = sc;
roll = ro;
name = nm;
}
int get_score()
{
return score;
}
int get_roll()
{
return roll;
}
string get_name()
{
return name;
}
};
bool pred(student a, student b)
{
//if all details are same return true else false
if (a.get_name() == b.get_name() && a.get_score() == b.get_score() && a.get_roll() == b.get_roll())
return true;
return false;
}
int main()
{
//1st list
vector<student> arr1(5);
//1st student
arr1[0] = student(80, 5, "XYZ"); //roll 5, marks 80
//2nd student
arr1[1] = student(70, 10, "INC"); //roll 10, marks 70
//3rd student
arr1[2] = student(85, 7, "HYU"); //roll 7, marks 85
//4th student
arr1[3] = student(83, 1, "EFG"); //roll 1, marks 83
//5th student
arr1[4] = student(81, 11, "ABC"); //roll 11, marks 81
//2nd list
vector<student> arr2(5);
//1st student
arr2[0] = student(80, 5, "XYZ"); //roll 5, marks 80
//2nd student
arr2[1] = student(70, 10, "INC"); //roll 10, marks 70
//3rd student
arr2[2] = student(85, 7, "HYU"); //roll 7, marks 85
//4th student
arr2[3] = student(83, 1, "EFG"); //roll 1, marks 83
//5th student
arr2[4] = student(81, 11, "ABC"); //roll 11, marks 81
//find check both lists are equal or not
//based on user-defined predicate
if (equal(arr1.begin(), arr1.end(), arr2.begin(), pred))
cout << "Both lists arr1,arr2 are equal\n";
else
cout << "Both lists arr1,arr2 are not equal\n";
//3rd list
vector<student> arr3(5);
//1st student
arr3[0] = student(89, 5, "PVR"); //roll 5, marks 89
//2nd student
arr3[1] = student(70, 10, "INC"); //roll 10, marks 70
//3rd student
arr3[2] = student(85, 7, "HYU"); //roll 7, marks 85
//4th student
arr3[3] = student(83, 1, "EFG"); //roll 1, marks 83
//5th student
arr3[4] = student(81, 11, "ABC"); //roll 11, marks 81
//find check both lists are equal or not based
//on user-defined predicate
if (equal(arr1.begin(), arr1.end(), arr3.begin(), pred))
cout << "Both lists arr1,arr3 are equal\n";
else
cout << "Both lists arr1,arr3 are not equal\n";
return 0;
}
Output:
輸出:
Both lists arr1,arr2 are equal
Both lists arr1,arr3 are not equal
Here we have first created two lists with the same elements. Since both lists are of equal size that's why we found equal to return true. For the second case, we have altered an element in arr3 to make unequal and the same reflected in the output. In our user-defined predicate, we have returned true all if all details matched for two students.
在這里,我們首先創建了兩個具有相同元素的列表。 由于兩個列表的大小相等,因此我們發現等于返回true。 對于第二種情況,我們更改了arr3中的元素以使其不相等,并且在輸出中反映出相同的內容。 在我們用戶定義的謂詞中,如果所有細節都匹配兩個學生,則我們返回true。
So in this article, you saw how efficiently we can use equal to check two ranges are equal or not. An application of this can be checking whether an array is subarray of the other one or not.
因此,在本文中,您看到了我們可以使用equal來檢查兩個范圍是否相等的效率。 這種方法的應用可以是檢查一個數組是否是另一個數組的子數組 。
翻譯自: https://www.includehelp.com/stl/std-equal-in-cpp.aspx
c:if equal