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