c:if equal_C ++中的std :: equal()

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377062.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377062.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377062.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《DBNotes:Buffer Pool刷臟頁細節以及改進》

本筆記知識沿用之前DBNotes: Buffer Pool對于緩沖頁的鏈表式管理的部分知識 目錄獲取一個空閑頁的源碼邏輯Page_Cleaner_ThreadLRU_Manager_ThreadHazard Pointer作為驅逐算法改進參考獲取一個空閑頁的源碼邏輯 任何一個讀寫請求都需要從Buffer pool來獲取所需頁面。如果需要的…

WordPress刪除數據中標題重復文章的方法

一種是刪除重復的方法是&#xff1a;使用插件,大家可以去官網上下載 二種刪除重復的方法是&#xff1a;登錄數據庫&#xff0c;使用sql語句刪除&#xff0c;具體的語句為如下代碼&#xff1a; CREATE TABLE my_tmp AS SELECT MIN(ID) AS col1 FROM wp_posts GROUP BY post_titl…

hibernate配置

hibernate.cfg.xml <?xml version"1.0" encoding"UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quo…

html中表單元素_HTML中的表單元素

html中表單元素1)<input>元素 (1) The <input> Element) The <input> element is used to get input from the user in an HTML form. <input>元素用于以HTML形式從用戶獲取輸入。 <input> tag is used to get input using input element, the …

《搜索算法——DFS、BFS、回溯》

目錄深搜200. 島嶼數量695. 島嶼的最大面積130. 被圍繞的區域547. 省份數量417. 太平洋大西洋水流問題回溯廣搜111. 二叉樹的最小深度752. 打開轉盤鎖深搜與廣搜結合934. 最短的橋深搜 深搜DFS&#xff0c;在搜索到一個新節點時&#xff0c;立即對該新節點進行遍歷&#xff0c…

AP in R

AP聚類算法是目前十分火的一種聚類算法&#xff0c;它解決了傳統的聚類算法的很多問題。不僅簡單&#xff0c;而且聚類效果還不錯。這里&#xff0c;把前兩天學習的AP算法在R語言上面的模擬&#xff0c;將個人筆記拿出來與大家分享一下&#xff0c;不談AP算法的原理&#xff0c…

nginx 模塊解析

nginx的模塊非常之多&#xff0c;可以認為所有代碼都是以模塊的形式組織&#xff0c;這包括核心模塊和功能模塊&#xff0c;針對不同的應用場合&#xff0c;并非所有的功能模塊都要被用到&#xff0c;附錄A給出的是默認configure&#xff08;即簡單的http服務器應用&#xff09…

python關鍵字和保留字_Python關鍵字

python關鍵字和保留字關鍵詞 (Keywords) Keywords are the reserved words in Python programming language (and, any other programming languages like C, C, Java, etc) whose meanings are defined and we cannot change their meanings. In python programming languages…

《LeetcodeHot100非困難題補錄》

最近比較閑&#xff0c;也比較焦慮&#xff0c;刷刷題吧 目錄11. 盛最多水的容器22. 括號生成31. 下一個排列48. 旋轉圖像49. 字母異位詞分組56. 合并區間75. 顏色分類79. 單詞搜索114. 二叉樹展開為鏈表141. 環形鏈表148. 排序鏈表152. 乘積最大子數組169. 多數元素207. 課程表…

Java里String.split需要注意的用法

我們常常用String的split()方法去分割字符串&#xff0c;有兩個地方值得注意&#xff1a; 1. 當分隔符是句號時(".")&#xff0c;需要轉義&#xff1a; 由于String.split是基于正則表達式來分割字符串&#xff0c;而句號在正則表達式里表示任意字符。 //Wrong: //Str…

C# Socket 例子(控制臺程序)

服務器代碼 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; using System.IO;namespace TCPListener {class Program{static void Main(string[] args){const int BufferSize 1024;Con…

Scala中的值類

Value classes are a special mechanism in Scala that is used to help the compiler to avoid allocating run time objects. 值類是Scala中的一種特殊機制&#xff0c;用于幫助編譯器避免分配運行時對象。 This is done by defining a subclass of AnyVal. The only parame…

《MySQL8.0.22:Lock(鎖)知識總結以及源碼分析》

目錄1、關于鎖的一些零碎知識&#xff0c;需要熟知事務加鎖方式&#xff1a;Innodb事務隔離MVCC多版本并發控制常用語句 與 鎖的關系意向鎖行級鎖2、鎖的內存結構以及一些解釋3、InnoDB的鎖代碼實現鎖系統結構lock_sys_tlock_t 、lock_rec_t 、lock_table_tbitmap鎖的基本模式的…

關于ORA-04021解決辦法(timeout occurred while waiting to lock object)

某個應用正在鎖定該表或者包 表為 select b.SID,b.SERIAL#,c.SQL_TEXT from v$locked_object a, v$session b, v$sqlarea c where a.SESSION_ID b.SID and b.SQL_ADDRESS c.ADDRESS and c.sql_text like %table_name% 包為 select B.SID,b.USERNAME,b.MACHINE FROM V$ACCESS …

HtmlAutoTestFrameWork

前段時間做的自動化測試的是Silverlight的&#xff0c;框架都已經搭好。突然測試發現這里還有一個要發送郵件的html頁面&#xff0c;并且將另外啟動瀏覽器&#xff0c;于是今天下午把這個html的也寫出來。用法 &#xff1a; HtmlAutoTestFrameWork htf new HtmlAutoTestFrameW…

L8ER的完整形式是什么?

L8ER&#xff1a;稍后 (L8ER: Later) L8ER is an abbreviation of "Later". L8ER是“ Later”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Yahoo Messenger, and Gmail, etc…

Randomize select algorithm 隨機選擇算法

從一個序列里面選擇第k大的數在沒有學習算法導論之前我想最通用的想法是給這個數組排序&#xff0c;然后按照排序結果返回第k大的數值。如果使用排序方法來做的話時間復雜度肯定至少為O&#xff08;nlgn&#xff09;。 問題是從序列中選擇第k大的數完全沒有必要來排序&#xff…

《Linux雜記:一》

目錄CPU負載和CPU利用率CPU負載很高,利用率卻很低的情況負載很低,利用率卻很高常用linux命令常用的文件、目錄命令常用的權限命令常用的壓縮命令CPU負載和CPU利用率 可以通過 uptime , w 或者 top 命令看到CPU的平均負載。 Load Average :負載的3個數字,比如上圖的0.57、0.4…

IOS Plist操作

代碼&#xff1a;copy BUNDLE下的plist文件 到 library下面。 bundle下不支持些&#xff0c;library&#xff0c;doc路徑支持讀與寫。 (void)copyUserpigListToLibrary {NSFileManager *fileManager [NSFileManager defaultManager];NSArray *paths NSSearchPathForDirector…

《線程管理:線程基本操作》

目錄線程管理啟動線程與&#xff08;不&#xff09;等待線程完成特殊情況下的等待&#xff08;使用trycath或rall&#xff09;后臺運行線程線程管理 啟動線程與&#xff08;不&#xff09;等待線程完成 提供的函數對象被復制到新的線程的存儲空間中&#xff0c;函數對象的執行…