C++ STL 中的 multiset crbegin() 和 crend() 函式
在本文中,我們將討論 C++ STL 中 multiset::crbegin() 和 multiset::crend() 函式的工作原理、語法和示例。
什麼是 C++ STL 中的 multiset?
Multiset 是與 set 容器類似的容器,這意味著它們以與 set 相同的方式儲存鍵形式的值,並以特定的順序儲存。
在 multiset 中,值與 set 一樣被識別為鍵。multiset 和 set 之間的主要區別在於,set 具有不同的鍵,這意味著沒有兩個鍵是相同的,而在 multiset 中可以存在相同的鍵值。
Multiset 鍵用於實現二叉搜尋樹。
什麼是 multiset::crbegin()?
multiset::crbegin() 函式是 C++ STL 中的內建函式,它在標頭檔案中定義。crbegin() 表示常量反向開始函式,這意味著此函式返回指向 multiset 容器最後一個元素的常量迭代器。此函式是 multiset::cbegin() 的反向版本。
常量迭代器只能用於遍歷 multiset 容器,它不能對 multiset 容器進行修改。
語法
ms_name.crbegin();
引數
該函式不接受任何引數。
返回值
此函式返回一個常量迭代器,該迭代器指向容器的最後一個元素。
示例
輸入
std::multiset<int> mymultiset = {1, 2, 2, 3, 4};
mymultiset.crbegin();輸出
4
示例
#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[] = {10, 20, 30, 40, 50, 60};
multiset<int> check(arr, arr + 6);
cout<<"First element fetched using crbegin() function: "<<*(check.crbegin()) << endl;
for(auto i = check.crbegin(); i!= check.crend(); i++)
cout << *i << " ";
return 0;
}輸出
First element fetched using crbegin() function: 60 60 50 40 30 20 10
什麼是 multiset::crend()?
multiset::crend() 函式是 C++ STL 中的內建函式,它在 <set> 標頭檔案中定義。crend() 表示常量結束函式,這意味著此函式返回指向 multiset 容器第一個元素之前元素的常量迭代器。這是 cend() 的反向版本。
常量迭代器只能用於遍歷 multiset 容器,它不能對 multiset 容器進行修改。
語法
ms_name.crend();
引數
該函式不接受任何引數。
返回值
此函式返回一個常量迭代器,該迭代器指向容器第一個元素之前的元素。
示例
輸入
std::multiset<int> mymultiset = {1, 2, 2, 3, 4};
mymultiset.crend();輸出
error
示例
#include <bits/stdc++.h>
using namespace std;
int main(){
int arr[] = {10, 20, 30, 40, 50, 60};
multiset<int> check(arr, arr + 6);
cout<<"Elements in the list are: ";
for(auto i = check.crbegin(); i!= check.crend(); i++)
cout << *i << " ";
return 0;
}輸出
Elements in the list are: 60 50 40 30 20 10
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP