C++ Map 中的 begin函数

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

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

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 - 返回值

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

无涯教程网

begin - 例子1

我们来看一个begin()函数的简单示例。

#include <iostream>
#include <map>

using namespace std;

int main ()
{
  map<char,string> mymap;

  mymap['b'] = "Java";
  mymap['a'] = "C++";
  mymap['c'] = "SQL";

 //show content:
  for (map<char,string>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    cout << it->first << " => " << it->second << '\n';

  return 0;
}

输出:

a => C++
b => Java
c => SQL

在上面的代码中,begin()函数用于返回指向mymap映射中第一个元素的迭代器。

begin - 例子2

让我们看一个简单的示例,使用for-each循环遍历映射。

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

来源:LearnFk无涯教程网

#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
 
using namespace std;
 
int main() {
 
	  map<string, int> m;

  m["Room1"] = 100;
  m["Room2"] = 200;
  m["Room3"] = 300;

 
	// Create a map iterator and point to beginning of map
	map<string, int>::iterator it = m.begin();
 
	// Iterate over a map 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
Room2 = 200
Room3 = 300 

在上面的示例中,我们使用STL算法std ::for-each遍历映射。它将在每个map元素上进行迭代,并调用我们提供的回调。

begin - 例子3

让我们看一个使用while循环迭代映射的简单示例。

#include <iostream>
#include <map>
#include <string>
int main()
{
    using namespace std;
 
      map<int,string> mymap = {
                { 100, "Nikita"},
                { 200, "Deep"  },
                { 300, "Priya" },
                { 400, "Suman" },
                { 500, "Aman"  }};

 cout<<"Elements are:" <<endl;
    map<int, string>::const_iterator it;//declare an iterator

    it = mymap.begin();//assign it to the start of the vector

    while (it != mymap.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
200: Deep
300: Priya
400: Suman
500: Aman

在上面的代码中,begin()函数用于返回指向mymap映射中第一个元素的迭代器。

begin - 例子4

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

#include <iostream>
#include <string>
#include <map>


using namespace std;

int main ()
{
  map<int,int> mymap = {
                { 10, 10},
                { 20, 20 },
                { 30, 30 } };
          
                
  cout<<"Elements are:" <<endl;
 
    for (auto it = mymap.begin(); it != mymap.end(); ++it)
    cout << it->first 
    << " + " 
    << it->second 
    << " = "
    <<it->first + it->second
    << '\n';

auto ite = mymap.begin();
   	 cout << "The first element is: ";
    	cout << "{" << ite->first << ", "
         << ite->second << "}\n";

  return 0;
  }

输出:

Elements are:
10 + 10 = 20
20 + 20 = 40
30 + 30 = 60
The first element is: {10, 10}

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

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

技术教程推荐

Java核心技术面试精讲 -〔杨晓峰〕

深入剖析Kubernetes -〔张磊〕

Go语言从入门到实战 -〔蔡超〕

透视HTTP协议 -〔罗剑锋(Chrono)〕

分布式技术原理与算法解析 -〔聂鹏程〕

性能测试实战30讲 -〔高楼〕

深度学习推荐系统实战 -〔王喆〕

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

Dubbo源码剖析与实战 -〔何辉〕

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