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

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.

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

a = 1
b = 2
c = 3


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.