English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Manuale di base di C++

Controllo di flusso C++

Funzione in C++

Array & Stringhe C++

Strutture dati C++

Classe & Oggetto C++

Puntatore in C++

Ereditarietà in C++

Manuale di STL di C++

Manuale di riferimento C++

Metodo e esempio di utilizzo della funzione begin() di C++ map

Map (contenitore) di STL in C++

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

语法

 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

参数

没有

返回值

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

实例1

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

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  map<char, string> mymap;
  mymap['b'] = "Java";
  mymap['a'] = "C++";
  mymap['c'] = "SQL";
  // 展示内容
  for (map<char, string>::iterator it = mymap.begin(); it != mymap.end(); ++it)
    cout << it->first << " => " << it->second << '\n';
  return 0;
}

Output:

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

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

实例2

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

#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;
 
	// 创建一个map迭代器并指向map的开头
	map<string, int>::iterator it = m.begin();
 
	// 使用std::for each和Lambda函数遍历一个Map
		for_each(m.begin(), m.end(), [](pair<string, int> element){
				    
			// 从元素访问KEY
			string word = element.first;
			// 从元素访问VALUE。
			int count = element.second;
			cout << word << " = " << count << endl;
		);
 
	return 0;
}

Output:

Room1 = 100
Room2 = 200
Room3 = 300

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

实例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; // 声明一个迭代器
    it = mymap.begin(); // 把它赋给向量的起点
    while (it != mymap.end()) 
    {
       cout << it->first << " = " << it->second << "\n"; 
      // 打印它所指向的元素的值
       ++it; // 并迭代到下一个元素
    }
 
    cout << endl;
}

Output:

Elements are:
100: Nikita
200: Deep
300: Priya
400: Suman
500: Aman

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

实例4

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

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  map<int,int> mymap = {
                { 10, 10 },
                { 20, 20 },
                { 30, 30 } };
          
                
  cout << "元素是:" << endl;
 
    for (auto it = mymap.begin(); it != mymap.end(); ++it)
    cout << it->first 
    << " + " 
    << it->second 
    << " = "
    << it->first + it->second
    << '\n';
auto it = mymap.begin();
   	 cout << "The first element is: ";
    	cout << "{" << ite->first << ", "
         << ite->second << "}\n";
  return 0;
  }

Output:

L'elemento è:
10 + 10 = 20
20 + 20 = 40
30 + 30 = 60
Il primo elemento è: {10, 10}

Nel esempio sopra, la funzione begin() viene utilizzata per restituire l'iteratore che punta al primo elemento del contenitore mymap.

Map (contenitore) di STL in C++