C++ Multimap 中的 cend函数

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

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

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

cend - 语法

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

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

cend - 返回值

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

cend - 例子1

让我们看一下cend()函数的简单示例:

#include <iostream>
#include <map>
using namespace std;
int main ()
{
  multimap<char,string> mymultimap;
  mymultimap = { 
               {'a',"Java"},
               {'b', "C++"},
               {'b', "Python"},
               {'a', "Android"}
               };
 //print content:
  cout << "mymultimap contains:";
  for (auto it = mymultimap.cbegin(); it != mymultimap.cend(); ++it)
    cout << " [" << (*it).first << ':' << (*it).second << ']';
  cout << '\n';
  return 0;
}

输出:

mymultimap contains: [a:Java] [a:Android] [b:C++] [b:Python]

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

cend - 例子2

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

#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm> 
using namespace std;
int main() { 
	  multimap<string, int> m;
  m= { 
     {"Room1", 100},
     {"Room2", 200},
     {"Room1", 300},
     {"Room1", 100} 
     }; 
	// Create a multimap iterator and point to beginning of multimap
	multimap<string, int>::iterator it = m.begin();
	// Iterate over a multimap 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
Room1 = 300
Room1 = 100
Room2 = 200

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

cend - 例子3

让我们看一个简单的示例,使用while循环遍历multimap:

#include <iostream>
#include <map>
#include <string>
int main()
{
    using namespace std; 
      multimap<int,string> mymultimap = {
                { 100, "Nikita"},
                { 100, "Deep"  },
                { 200, "Priya" },
                { 300, "Suman" },
                { 200, "Aman"  }}; 
    multimap<int, string>::const_iterator it;//declare an iterator
    it = mymultimap.cbegin();//assign it to the start of the vector
    while (it != mymultimap.cend())//while it hasn't reach the end
    {
cout << it->first << " = " << it->second << "\n"; 
// prin the value of the element it points to
++it;//and iterate to the next element
    }
    cout << endl;
}

输出:

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

在上面的示例中,cend()函数用于返回指向mymultimap多重映射中最后一个元素旁边的const_iterator。

无涯教程网

cend - 例子4

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

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
  multimap<int,int> mymultimap = {
                { 10, 10},
                { 20, 20 },
                { 10, 30 } };          
  cout<<"Elements are:" <<endl;
    for (auto it = mymultimap.cbegin(); it != mymultimap.cend(); ++it)
    cout << it->first 
    << " + " 
    << it->second 
    << " = "
    <<it->first + it->second
    << '\n';
    auto ite = mymultimap.cend();
    cout << "end element (point next to the last): ";
    cout << "{" << ite->first << ", "
         << ite->second << "}\n";
  return 0;
  }

输出:

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

在上面的示例中,cend()函数用于返回指向mymultimap多重映射中最后一个元素旁边的const_iterator。

无涯教程网

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

技术教程推荐

邱岳的产品手记 -〔邱岳〕

人工智能基础课 -〔王天一〕

Service Mesh实践指南 -〔周晶〕

设计模式之美 -〔王争〕

接口测试入门课 -〔陈磊〕

架构实战案例解析 -〔王庆友〕

Serverless入门课 -〔蒲松洋(秦粤)〕

说透区块链 -〔自游〕

玩转Vue 3全家桶 -〔大圣〕

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