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

C++ Tutoriel de base

C++ Contrôle de flux

C++ Fonction

C++ Tableau & chaîne

C++ Structure de données

C++ Classe & objet

C++ Pointeur

C++ Héritage

C++ Tutoriel STL

C++ Manuel de référence

C++ Utilisation et exemple de la fonction end() de map

C++ STL map (conteneur)

C ++ map end()La fonction retourne un itérateur qui se trouve à côté du dernier élément de la map.

Syntaxe

 iterator end(); // de C++ 11 avant
const_iterator end() const; // de C++ 11 avant
 iterator end() noexcept; //de C++ 11 Début
const_iterator end() const noexcept;  //de C++ 11 Début

Paramètre

Aucun

Valeur de retour

Elle retourne un itérateur pointant sur l'élément suivant du dernier élément de la map.

实例1

Voyons un exemple simple de la fonction end().

#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;
}

Sortie :

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

Dans cela, la fonction end() est utilisée pour retourner un itérateur pointant sur l'élément suivant du conteneur mymap.

实例2

Voyons un exemple simple, utilisant for-La boucle each parcoure la 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<string, int>::iterator it = m.begin();
	//对每个和Lambda函数使用std::迭代map
	for_each(m.begin(), m.end(), [](pair<string, int> element){
		string word = element.first;
		int count = element.second;
		cout << word << " = " << count << endl;
	});
	return 0;
}

Sortie :

Room1 = 100
Room2 = 200
Room3 = 300

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

实例3

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

#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" }};
    map<int, string>::const_iterator it; //声明一个迭代器
    it = mymap.begin(); //把它分配到向量的起始点
    while (it != mymap.end())
    {
            cout << it->first << " = " << it->second << "\n"; 
            // 打印它所指向的元素的值
            ++it; //并迭代到下一个元素
    }
 
    cout << endl;
}

Sortie :

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

Dans l'exemple ci-dessus, la fonction end() est utilisée pour retourner un itérateur pointant sur l'élément suivant du conteneur 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 (map<int,int>::iterator it=mymap.begin(); it!=mymap.end();) ++it){
            cout << it-first 
            " * " 
            <<it-second 
            << " = "
            <<it-first * it-second
            << '\n';    
    }
  return 0;
  
  }

Sortie :

L'élément est :
10 * 10 = 100
20 * 20 = 400
30 * 30 = 900

Dans l'exemple ci-dessus, la fonction end() est utilisée pour retourner un itérateur pointant sur l'élément suivant du conteneur mymap.

C++ STL map (conteneur)