C++ .end()
The .end() method is a built-in function in C++ Standard Template Library(STL), which is defined in the <map> header file. It returns an iterator pointing to the theoretical element just after the last element in the map container. This element is referred to as the “past-the-end” element and does not represent an actual element in the map container. It serves as a marker indicating the end of the map sequence and should not be dereferenced.
The .end() method is commonly used in combination with .begin() to specify a range for iterating through all elements in a map container. This iterator-based approach is a fundamental pattern in the C++ Standard Template Library (STL) and is used across various algorithms and container operations.
Syntax
map_name.end();
Parameters:
The .end() method does not accept any parameters.
Return value:
It returns an iterator pointing to the past-the-end element in the map container. If the map object is const-qualified, the function returns a const_iterator. Otherwise, it returns an iterator.
Example 1: Basic Map Iteration Using .end()
This example demonstrates how to use .end() with .begin() to iterate through all key-value pairs in a map:
#include <iostream>#include <map>int main() {// Create a map with some key-value pairsstd::map<char, int> charMap;// Insert elementscharMap['a'] = 10;charMap['b'] = 20;charMap['c'] = 30;// Iterate through the map using begin() and end()std::cout << "Map contents: " << std::endl;for (auto it = charMap.begin(); it != charMap.end(); ++it) {std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;}return 0;}
This code creates a map that associates characters with integers, then uses .begin() and .end() to establish the range for iteration. The loop continues until the iterator equals the value returned by .end(), which indicates we’ve gone through all elements.
The output of this code will be:
Map contents:Key: a, Value: 10Key: b, Value: 20Key: c, Value: 30
Example 2: Finding Elements in a Map with .end()
This example shows how to use .end() as a comparison point to determine if an element exists in a map:
#include <iostream>#include <map>#include <string>int main() {// Create a map for storing student gradesstd::map<std::string, char> studentGrades;// Add some student recordsstudentGrades["Alice"] = 'A';studentGrades["Bob"] = 'B';studentGrades["Carol"] = 'A';// Names to search forstd::string names[] = {"Alice", "David", "Bob"};// Search for each name in the mapfor (const auto& name : names) {// Use find() method which returns end() if element is not foundauto it = studentGrades.find(name);if (it != studentGrades.end()) {// Element was foundstd::cout << name << "'s grade: " << it->second << std::endl;} else {// Element was not found (iterator equals end())std::cout << name << " is not in the database." << std::endl;}}return 0;}
This example demonstrates a common pattern for checking if an element exists in a map. The .find() method returns an iterator to the element if found, or an iterator equal to .end() if not found. By comparing the returned iterator with .end(), we can determine if the search was successful.
The output for this code will be:
Alice's grade: ADavid is not in the database.Bob's grade: B
Codebyte Example: Using .end() with STL Algorithms
This example shows how to use .end() with STL algorithms to perform operations on map elements:
This example demonstrates how .end() is used with various STL algorithms. These algorithms require a range defined by two iterators, and the common pattern is to use .begin() and .end() to specify the entire container.
To explore more C++ concepts, check out our Learn C++ course to enhance your coding skills.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.
- Beginner Friendly.11 hours