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

C++ multimap begin()

C++ multimap begin()函数

C++ multimap begin()函数用于返回引用 multimap 容器第一个元素的迭代器。

语法

      iterator begin();                            //until C++ 11
const_iterator begin() const;                //until C++ 11
      iterator begin() noexcept;              //since C++ 11
const_iterator begin() const noexcept;  //since C++ 11

参数

返回值

它返回指向迭代器第一个元素的迭代器

复杂度

常量。

迭代器有效性

没有变化。

数据争用

已访问容器。

异常安全

此函数从不抛出异常。

示例1

让我们看一下begin()函数的简单示例:
#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 (multimap<char,string>::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
    cout << it->first << " => " << it->second << '\n';
  return 0;
}
输出:
a => Java
a => Android
b => C++
b => Python
在上面的示例中,begin()函数用于返回指向mymultimap set 映射中第一个元素的迭代器。

示例2

让我们看一个简单的示例,使用for-each循环遍历multimap:
#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} 
     };
  // Create a multimap iterator and point to beginning of multimap
  multimap<string, int>::iterator it = m.begin();
  // Iterate over a multimap using std::for_each and Lambda function
      for_each(m.begin(), m.end(),
        [](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算法来迭代 multimap 。它将在每个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" },
                { 100, "Aman"  }};
 cout<<"Elements are: "<<endl;
    multimap<int, string>::const_iterator it; // declare an iterator
    it = mymultimap.begin(); // assign it to the start of the vector
    while (it != mymultimap.end()) // 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;
}
输出:
Elements are: 
100 = Nikita
100 = Aman
200 = Deep
200 = Suman
300 = Priya
在上面,begin()函数用于返回指向mymultimap set 映射中第一个元素的迭代器。

示例4

让我们看一个简单的例子:
#include <map>  
#include <iostream>  
int main()  
{  
   using namespace std;     
   multimap <int, int> m1;  
   multimap <int, int> :: iterator m1_Iter;  
   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_Iter = m1.begin ();  
   cout << "The first element of m1 is " << m1_Iter-> first << endl;  
   m1_Iter = m1.begin ();  
   m1.erase ( m1_Iter );  
   m1_Iter = m1.begin();  
   cout << "First element of m1 is now " << m1_Iter-> first << endl;  
}  
输出:
The first element of m1 is 0
First element of m1 is now 1
在上面的示例中,begin()函数用于返回指向mymultimap set 映射中第一个元素的迭代器。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4