C++ Multimap 中的 begin函数

首页 / C++入门教程 / C++ Multimap 中的 begin函数

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

begin - 语法

      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

begin - 返回值

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

begin - 例子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 multimap中第一个元素的迭代器。

begin - 例子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元素上进行迭代,并调用我们提供的回调。

begin - 例子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多重映射中第一个元素的迭代器。

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-multimap-begin-function.html

来源:LearnFk无涯教程网

begin - 例子4

让我们看一个简单的例子:

#include   
#include   
int main()  
{  
   using namespace std;     
   multimap  m1;  
   multimap  ::iterator m1_Iter;  
   typedef pair  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  first  first 

输出:

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

在上面的示例中,begin()函数用于返回指向mymultimap multimap中第一个元素的迭代器。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

高并发系统设计40问 -〔唐扬〕

设计模式之美 -〔王争〕

Serverless入门课 -〔蒲松洋(秦粤)〕

说透数字化转型 -〔付晓岩〕

零基础实战机器学习 -〔黄佳〕

Tony Bai · Go语言第一课 -〔Tony Bai〕

李智慧 · 高并发架构实战课 -〔李智慧〕

云计算的必修小课 -〔吕蕴偲〕

手把手带你写一个 MiniTomcat -〔郭屹〕

好记忆不如烂笔头。留下您的足迹吧 :)