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


C++ multimap cbegin()用法及代碼示例

C++ multimap cbegin() 函數用於返回一個常量迭代器,指向 multimap 容器的第一個元素。

用法

const_iterator cbegin() const noexcept;  //since C++ 11

const_iterator 是一個指向常量內容的迭代器。

參數

返回值

它返回一個 const_iterator 指向多重映射的第一個元素。

複雜度

持續的

迭代器有效性

沒有變化。

數據競爭

容器被訪問。

異常安全

此成員函數從不拋出異常。

例子1

讓我們看看 cbegin() 函數的簡單示例:

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  multimap<char,string> mymultimap;
  mymultimap = { 
               {'a',"Java"},
               {'b', "C++"},
               {'b', "Python"},
               {'a', "Android"}
               };
  // show content:
  for (auto it = mymultimap.cbegin(); it != mymultimap.cend(); ++it)
    cout <<(*it).first << " => " << (*it).second << '\n';
  return 0;
}

輸出:

a => Java
a => Android
b => C++
b => Python

在上麵的例子中, cbegin() 函數用於返回一個 const_iterator 指向 mymultimap 多圖中的第一個元素。

例子2

讓我們看一個使用 for-each 循環遍曆多重映射的簡單示例:

#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std; 
int main() { 
      multimap<string, int> m;
  m= { 
     {"Room1", 100},
     {"Room2", 200},
     {"Room1", 300},
     {"Room1", 100} 
     };
    // Iterate over a multimap using std::for_each and Lambda function	
        for_each(m.cbegin(), m.cend(),
                [](pair<string, int> element){				  
                    // Accessing KEY from element
                    string word = element.first;
                    // Accessing VALUE from element.
                    int count = element.second;
                    cout<<word<<" = "<<count<<endl;
        });
    return 0;
}

輸出:

Room1 = 100
Room1 = 300
Room1 = 100
Room2 = 200

在上麵的例子中,我們使用 STL 算法 std::for-each 來迭代多重映射。它將迭代每個 multimap 元素並調用我們提供的回調。

例子3

讓我們看一個使用 while 循環迭代 multimap 的簡單示例:

#include <iostream>
#include <map>
#include <string>
int main()
{
    using namespace std;
      multimap<int,string> mymultimap = {
                { 100, "Nikita"},
                { 200, "Deep"  },
                { 300, "Priya" },
                { 200, "Suman" },
                { 400, "Aman"  }};
    multimap<int, string>::const_iterator it; // declare an iterator
    it = mymultimap.cbegin(); // assign it to the start of the multimap
    while (it != mymultimap.cend()) // while it hasn't reach the end
    {
cout << it->first << " = " << it->second << "\n"; 
// print the value of the element it points to
++it; // and iterate to the next element
    }
    cout << endl;
}

輸出:

100 = Nikita
200 = Deep
200 = Suman
300 = Priya
400 = Aman

在上麵的例子中, cbegin() 函數用於返回一個 const_iterator 指向 mymultimap 多圖中的第一個元素。

示例 4

讓我們看另一個簡單的例子:

#include <map>  
#include <iostream>  
int main()  
{  
   using namespace std;     
   multimap <int, int> m1;  
   multimap <int, int>::iterator m1_Iter;  
   multimap <int, int>::const_iterator m1_cIter;  
   typedef pair <int, int> Int_Pair;  
   m1.insert ( Int_Pair ( 0, 0 ) );  
   m1.insert ( Int_Pair ( 1, 1 ) );  
   m1.insert ( Int_Pair ( 2, 4 ) );  
   m1_cIter = m1.cbegin ();  
   cout << "The first element of m1 is " << m1_cIter -> first << endl;  
   m1_Iter = m1.begin ();  
   m1.erase ( m1_Iter );  
   // The following 2 lines would err as the iterator is const  
   // m1_cIter = m1.begin ();  
   // m1.erase ( m1_cIter );  
   m1_cIter = m1.cbegin();  
   cout << "First element of m1 is now " << m1_cIter -> first << endl;  
}

輸出:

The first element of m1 is 0
First element of m1 is now 1

在上麵的例子中, cbegin() 函數用於返回一個 const_iterator 指向 mymultimap 多圖中的第一個元素。





相關用法


注:本文由純淨天空篩選整理自 C++ multimap cbegin() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。