C++ std::map end() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::map

Definition

In C++, std::map::end() returns an iterator to the position after the last pair. It is a marker, not a real pair, so you must never dereference it.

You use it to know when a loop is finished, and to test the result of find(), lower_bound(), and upper_bound(), which return end() when nothing matches. Include <map> to use it.

This is a member of std::map<Key, T>, a sorted container of key and value pairs. Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.

Syntax

Call end with empty brackets. Use it as the stop value in a loop or as a “not found” test.

C++
if (m.find("Zoe") == m.end()) {
    // not found
}
auto last = std::prev(m.end());      // the last pair
CallWhat it does
m.end()Returns an iterator one past the last pair. On a const map it returns a const_iterator.

To get the last pair, write std::prev(m.end()) or use m.rbegin(). Do not write *m.end() and do not use m.end() - 1, because map iterators do not support subtraction.

Library declarations
C++
iterator end() noexcept;
const_iterator end() const noexcept;

Parameters

ParameterDescription
Noneend() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
iteratorAn iterator to the position after the last pair. It is equal to begin() if the map is empty.

C++ Examples

Example 1 — end() Means Not Found

Compare the result of find() with end().

C++
#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<std::string, int> age = {{"Mia", 21}, {"Raj", 25}};

    for (const std::string name : {"Raj", "Zoe"}) {
        auto it = age.find(name);
        if (it == age.end()) {
            std::cout << name << ": not found\n";
        } else {
            std::cout << name << ": " << it->second << '\n';
        }
    }
    return 0;
}

How It Works

  1. find() returns an iterator. It cannot return nullptr, so end() is the agreed way to say “nothing found”.
  2. For "Raj", the iterator points at a real pair, so it is not equal to end() and the value 25 is printed.
  3. For "Zoe", find() returns end(), so the first branch runs. The code never reads a value from end().

Example 2 — end() and std::prev()

Step back one position from end() to reach the last pair, which has the biggest key.

C++
#include <iostream>
#include <iterator>
#include <map>
#include <string>

int main() {
    std::map<int, std::string> m = {{5, "five"}, {1, "one"}, {9, "nine"}};

    if (!m.empty()) {
        auto last = std::prev(m.end());
        std::cout << "last: " << last->first << " " << last->second << '\n';
    }
    return 0;
}

How It Works

  1. m.end() is one position after the last pair, so reading it directly would be an error.
  2. std::prev(m.end()) moves one position back, onto the last pair. That is the pair with the biggest key, 9.
  3. The !m.empty() check matters. On an empty map end() equals begin(), and stepping back would be undefined.

Example 3 — end() on an Empty Map

On an empty map begin() and end() are the same, so a loop does not run.

C++
#include <iostream>
#include <map>

int main() {
    std::map<int, int> m;
    std::cout << std::boolalpha;

    std::cout << "begin == end: " << (m.begin() == m.end()) << '\n';

    int loops = 0;
    for (auto it = m.begin(); it != m.end(); ++it) {
        ++loops;
    }
    std::cout << "loop ran " << loops << " times\n";

    m[1] = 100;
    std::cout << "begin == end now: " << (m.begin() == m.end()) << '\n';
    return 0;
}

How It Works

  1. An empty map has no first pair, so begin() and end() are the same position, and the comparison is true.
  2. The loop condition it != m.end() is false at once, so the body never runs and loops stays 0.
  3. After m[1] = 100, there is a real first pair. begin() now points at it, and end() is still one position after, so they differ.

Frequently Asked Questions

An iterator to the position after the last pair. It does not point at a real pair.
No. That is undefined behavior. Only compare it with other iterators.
Use std::prev(m.end()) or m.rbegin(), after checking that the map is not empty.
A search needs a way to say "not found". Because end() is never a real pair, it is a safe signal.
It equals begin(), so any loop from begin() to end() does nothing.

Next: equal_range()

Keep going through the std::map functions. Get the range of pairs whose key equals the given key.

equal_range() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful