當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ unordered_multiset begin()用法及代碼示例


unordered_multiset::begin()是C++ STL中的內置函數,該函數返回指向容器中第一個元素或其存儲桶中的第一個元素的迭代器。

用法:

unordered_multiset_name.begin(n)

參數:該函數接受一個參數。如果傳遞了參數,則返回指向存儲桶中第一個元素的迭代器。如果未傳遞任何參數,則它返回指向unordered_multiset容器中第一個元素的迭代器。


返回值:它返回一個迭代器。

以下示例程序旨在說明上述函數:

示例1:

// C++ program to illustrate the 
// unordered_multiset::begin() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<int> sample; 
  
    // inserts element 
    sample.insert(10); 
    sample.insert(11); 
    sample.insert(15); 
    sample.insert(13); 
    sample.insert(14); 
  
    // print the first element 
    cout << "The first element: " << *sample.begin(); 
  
    cout << "\nElements: "; 
  
    // prints all element 
    for (auto it = sample.begin(); it != sample.end(); it++) 
        cout << *it << " "; 
    return 0; 
}
輸出:
The first element: 14
Elements: 14 13 15 10 11

示例2:

// C++ program to illustrate the 
// unordered_multiset::begin() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<char> sample; 
  
    // inserts element 
    sample.insert('a'); 
    sample.insert('b'); 
    sample.insert('c'); 
    sample.insert('x'); 
    sample.insert('z'); 
  
    // print the first element 
  
    auto it = sample.begin(); 
    cout << "The first element: " << *it; 
  
    it++; 
    cout << "\nThe second element: " << *it; 
  
    cout << "\nElements: "; 
  
    // prints all element 
    for (auto it = sample.begin(); it != sample.end(); it++) 
        cout << *it << " "; 
    return 0; 
}
輸出:
The first element: z
The second element: x
Elements: z x c a b

示例3:

// C++ program to illustrate the 
// unordered_multiset::begin() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
  
    // declaration 
    unordered_multiset<char> sample; 
  
    // inserts element 
    sample.insert('a'); 
    sample.insert('b'); 
    sample.insert('c'); 
    sample.insert('x'); 
    sample.insert('z'); 
  
    // print the first element 
    cout << "The first element in first bucket : " << *sample.begin(1); 
  
    cout << "\nElements in first bucket: "; 
  
    // prints all element 
    for (auto it = sample.begin(1); it != sample.end(1); it++) 
        cout << *it << " "; 
    return 0; 
}
輸出:
The first element in first bucket : x
Elements in first bucket: x c


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 unordered_multiset begin() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。