Map Cend Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Map Cend Function

Map Cend Function

BLUF: Mastering Map Cend Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Map Cend Function

C++ is renowned for its efficiency. Learn how Map Cend Function enables low-level control and high-performance computing in the tutorial below.

The cend function in C++ map is utilized to provide a constant iterator pointing to the element following the last element in the map.

Note:-This is a placeholder. No element exists in this location and attempting to access is undefined behavior.

Syntax

Example

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

Note: A const_iterator is an iterator thacpp tutorials to constant content.

Parameter

Return value

It provides a constant iterator pointing to the element following the final one in the map.

Example 1

Let's see a simple example for cend function.

Example

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

Output:

Output

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

In the example provided, the cend method is employed to retrieve an iterator pointing to the element following the last one in the map named mymap.

Example 2

Let's examine a basic illustration of iterating through a map using a for-each loop.

Example

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

Output:

Output

Room1 = 100
Room2 = 200
Room3 = 300

In the previous example, we employed an STL algorithm called std::for-each to sequentially go through the map. This process involves iterating over each element within the map and executing the designated callback function.

Example 3

Let's examine a basic illustration of iterating through a map utilizing a while loop.

Example

#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 icpp tutorials to

++it; // and iterate to the next element
    }
 
    cout << endl;
}

Output:

Output

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

In the provided instance, the cend method is employed to retrieve a constant iterator that points to the element following the final entry in the mymap map.

Example 4

Let's see a simple example.

Example

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

Output:

Output

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

In the example provided, the cend method is employed to retrieve a constant iterator that points to the element following the final entry in the map named mymap.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience