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

Manuale di base di C++

C++ 流程控制

Funzione in C++

C++ 数组 & 字符串

C++ 数据结构

C++ 类 & 对象

Puntatore in C++

Ereditarietà in C++

Manuale di STL di C++

C++ 参考手册

C++ map cend() 函数使用方法及示例

map (contenitore) in STL di C++

C ++ map cend()函数用于返回常量迭代器,该常量迭代器位于map中的最后一个元素旁边。

注意:- 这是一个占位符。此位置没有元素,尝试访问是未定义的行为。

语法

const_iterator cend() const noexcept; // 从 C++ 11 开始

注意:const_iterator是指向常量内容的迭代器。

参数

没有

返回值

它返回一个常量迭代器,指向map的最后一个元素。

实例1

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

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  map<char, int> mymap;
  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;
  // 打印内容:
  cout << "mymap contains:";
  for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
    cout << " [" << (*it).first << ':' << (*it).second << "]";
  cout << '\n';
  return 0;
}

Output:

打印内容:[a:200] [b:100] [c:300]

在上面的示例中,cend()函数用于返回指向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<string, int>::iterator it = m.begin();
	// 对每个和Lambda函数使用std::迭代map
	for_each(m.cbegin(), m.cend(),
		[](pair<string, int> element){
				    
		// 从元素访问KEY
		 string word = element.first;
		// Accesso al VALUE dell'elemento.
		int count = element.second;
		cout << word << " = " << count << endl;
	});
 
	return 0;
}

Output:

Room1 = 100
Room2 = 200
Room3 = 300

Nel esempio sopra, abbiamo utilizzato l'algoritmo STL std::for_each per esplorare la mappa. Itererà su ogni elemento della mappa e chiamerà il callback fornito.

Esempio 3

Lasciate che vediamo un esempio semplice di iterazione di una mappa utilizzando un ciclo 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" }};
 
    map<int, string>::const_iterator it; // Dichiarazione di un iteratore
    it = mymap.cbegin(); // Lo assegna all'inizio della coda
    while (it != mymap.cend())
    {
        cout << it->first << " = " << it->second << "\n"; 
        // Stampa il valore dell'elemento puntato
        ++it; // Esegue l'iterazione al successivo elemento
    }
    cout << endl;
}

Output:

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

Nell'esempio sopra, la funzione cend() viene utilizzata per restituire un iteratore costante che punta al elemento successivo dell'ultimo elemento del contenitore mymap.

Esempio 4

Lasciate che vediamo un esempio semplice.

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  map<int, int> mymap = {
                { 10, 10 },
                { 20, 20 },
                { 30, 30 } };
          
                
  cout << "elemento è:" << endl;
 
    for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
    cout << it->first 
    << " + " 
    << it->second 
    << " = "
    << it->first + it->second
    << ''\n'';
    auto ite = mymap.cend();
 
    cout << "Elemento finale (punta all'ultimo): ";
    cout << "{" << ite->first << ", "
         << ite->second << "}\n";
  return 0;
  }

Output:

Elemento è:
10 + 10 = 20
20 + 20 = 40
30 + 30 = 60
Elemento finale (punta all'ultimo): {3, 0}

Nell'esempio sopra, la funzione cend() viene utilizzata per restituire un iteratore costante che punta al elemento successivo dell'ultimo elemento del contenitore mymap.

map (contenitore) in STL di C++