当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。