Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

map::begin() and end() in C++ STL

  • Difficulty Level : Easy
  • Last Updated : 12 May, 2019

Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values.

map::begin()

begin() function is used to return an iterator pointing to the first element of the map container. begin() function returns a bidirectional iterator to the first element of the container.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Syntax :

mapname.begin()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to the first element.

Examples:



Input  : mymap['a'] = 1;
         mymap['b'] = 2;
         mymap['c'] = 3;
         mymap.begin();
Output : returns an iterator to the element 'a' = 1

Input  : mymap['d'] = 1;
         mymap.begin();
Output : returns an iterator to the element 'd' = 1

Errors and Exceptions

1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.




// Demonstrates begin() and end()
#include <iostream>
#include <map>
using namespace std;
  
int main()
{
    // declaration of map container
    map<char, int> mymap;
    mymap['a'] = 1;
    mymap['b'] = 2;
    mymap['c'] = 3;
  
    // using begin() to print map
    for (auto it = mymap.begin();
         it != mymap.end(); ++it)
        cout << it->first << " = "
             << it->second << '\n';
    return 0;
}

Output:

a = 1
b = 2
c = 3
map::end()

end() function is used to return an iterator pointing to past the last element of the map container. Since it does not refer to a valid element, it cannot de-referenced end() function returns a bidirectional iterator.

Syntax :

mapname.end()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to the next of last element.

Examples:

Input  : mymap['a'] = 1;
         mymap['b'] = 2;
         mymap['c'] = 3;
         mymap.end();
Output : returns an iterator to next to c 
(after last element)

Errors and Exceptions

1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.




// CPP program to illustrate
// Demonstrates begin() and end() 
#include <iostream>
#include <map>
using namespace std;
  
int main()
{
    // declaration of map container
    map<char, int> mymap;
    mymap['a'] = 1;
    mymap['b'] = 2;
    mymap['c'] = 3;
  
    // using begin() to print map
    for (auto it = mymap.begin();
         it != mymap.end(); ++it)
        cout << it->first << " = "
             << it->second << '\n';
    return 0;
}

Output:

a = 1
b = 2
c = 3



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!