C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ multiset cbegin()

C++ multiset cbegin()

C++ multiset cbegin()函数用于返回指向 multiset 容器第一个元素的常量迭代器。

语法

const_iterator cbegin() const noexcept;  //since C++ 11
const_iterator 是指向常量内容的迭代器。

参数

返回值

cbegin()函数返回一个指向 multiset 第一个元素的const_iterator。

复杂度

常量

迭代器有效期

没有更改。

数据竞争

访问容器。
同时访问 multiset 容器的元素是安全的。

异常安全

此成员函数从不抛出异常。

示例1

让我们看一下cbegin的简单示例()函数:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  multiset<string> mymultiset= {"Java", "C","SQL","C++" };
  // show content
  for (auto it = mymultiset.cbegin(); it != mymultiset.cend(); ++it)
    cout <<*it << '\n';
    
  return 0;
} 
输出:
C
C++
Java
SQL
在上面的示例中,cbegin()函数用于返回指向mymultiset multiset 中的第一个元素的常量迭代器。

示例2

让我们看一个简单的示例:
#include <set>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   multiset <int> s1;  
   multiset <int>::iterator s1_Iter;  
   multiset <int>::const_iterator s1_cIter;  
  
   s1.insert( 1 );  
   s1.insert( 2 );  
   s1.insert( 3 );  
   s1.insert( 2 );
   
   s1_Iter = s1.begin( );  
   cout << "The first element of s1 is " << *s1_Iter << endl;  
  
   s1_Iter = s1.begin( );  
   s1.erase( s1_Iter );  
  
   // The following 2 lines would err because the iterator is const  
   // s1_cIter = s1.begin( );  
   // s1.erase( s1_cIter );  
  
   s1_cIter = s1.begin( );  
   cout << "The first element of s1 is now " << *s1_cIter << endl;  
}  
输出:
The first element of s1 is 1
The first element of s1 is now 2

示例3

让我们看一个使用while循环遍历 multiset 的简单示例:
#include <iostream>
#include <set>
#include <string>
int main()
{
    using namespace std;
 
      multiset<string> mymultiset = {"Robin","Dolly", "John","Nikita","Nikita"};
    multiset<string>::const_iterator it; // declare an iterator
    it = mymultiset.cbegin(); // assign it to the start of the vector
    while (it != mymultiset.cend()) // while it hasn't reach the end
    {
        cout << *it<< "\n"; 
    // print the value of the element it points to
        ++it; // and iterate to the next element
    }
    cout << endl;
}
输出:
Dolly
John
Nikita
Nikita
Robin
在上面的示例中,cbegin()函数用于返回指向mymultiset multiset 中第一个元素的迭代器。

示例4

我们来看另一个简单的例子:
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main ()
{
  multiset<int> number = {400, 350, 465, 290, 410, 400};
   cout << "Increasing order: " << '\n';
   cout<<"______________________\n";
   
  multiset<int>::const_iterator cit;
  for (cit=number.cbegin(); cit!=number.cend(); ++cit)
    cout << *cit<< '\n';
    auto low = number.cbegin();
    auto high = number.rbegin();
    
    cout << "\nSmallest Number is: "<< *low <<endl;
    cout<< "Biggest Number is: "<<*high <<endl;
  return 0;
  }  
输出:
Increasing order: 
______________________
290
350
400
400
410
465
Smallest Number is: 290
Biggest Number is: 465
在上面的示例中,cbegin()函数用于返回指向mymultiset multiset 中的第一个元素的迭代器。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4