C++ Map 中的 cend函数

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

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

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

cend - 语法

const_iterator cend() const noexcept;  //since C++ 11

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

cend - 返回值

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

cend - 例子1

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

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

来源:LearnFk无涯教程网

#include <iostream>
#include <map>

using namespace std;

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

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

 //print content:
  cout << "mymap contains:";
  for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
    cout << " [" << (*it).first << ':' << (*it).second << ']';
  cout << '\n';

  return 0;
}

输出:

mymap contains: [a:200] [b:100] [c:300]

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

cend - 例子2

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

#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.cbegin(), m.cend(),
				[](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元素上进行迭代,并调用我们提供的回调。

cend - 例子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"  }};

 
    map<int, string>::const_iterator it;//declare an iterator

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

    while (it != mymap.cend())//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;
}

输出:

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

在上面的示例中,cend()函数用于返回指向mymap映射中最后一个元素旁边的常量迭代器。

cend - 例子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.cbegin(); it != mymap.cend(); ++it)
    cout << it->first 
    << " + " 
    << it->second 
    << " = "
    <<it->first + it->second
    << '\n';

    auto ite = mymap.cend();
 
    cout << "end element (point next to the last): ";
    cout << "{" << ite->first << ", "
         << ite->second << "}\n";

  return 0;
  }

输出:

Elements are:
10 + 10 = 20
20 + 20 = 40
30 + 30 = 60
end element (point next to the last): {3, 0}

在上面的示例中,cend()函数用于返回指向mymap映射中最后一个元素旁边的常量迭代器。

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

技术教程推荐

SQL必知必会 -〔陈旸〕

网络编程实战 -〔盛延敏〕

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

NLP实战高手课 -〔王然〕

罗剑锋的C++实战笔记 -〔罗剑锋〕

TensorFlow 2项目进阶实战 -〔彭靖田〕

Go 语言项目开发实战 -〔孔令飞〕

编程高手必学的内存知识 -〔海纳〕

技术领导力实战笔记 2022 -〔TGO 鲲鹏会〕

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