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

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of map::begin() and map::end() functions in C++ STL.

What is a Map in C++ STL?

Maps are the associative container, which facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in the map container are accessed by its unique keys.

What is a map::begin()?

map::begin() function is an inbuilt function in C++ STL, which is defined in  header file. begin() is used to access the element which is at the very beginning of the associated map container.

This function returns an iterator which points to the first element of the container. When the container has no values in it the iterator cannot be dereferenced

Syntax

map_name.begin();

Parameters

The function accepts no parameter.

Return value

This function returns an iterator which is pointing to the first value of the map container.

Example

Input

std::map<int> mymap;
mymap.insert({‘a’, 10});
mymap.insert({‘b’, 20});
mymap.insert({‘c’, 30});
mymap.begin();

Output

a:10

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP_1;
   TP_1[1] = 10;
   TP_1[2] = 20;
   TP_1[3] = 30;
   TP_1[4] = 40;
   cout<<"Elements of TP_1 after swap:\n"<< "\tKEY\tELEMENT\n";
   for (auto i = TP_1.begin(); i!= TP_1.end(); i++) {
      cout << "\t" << i->first << "\t" << i->second << '\n';
   }
   return 0;
}

Output

Elements of TP_1 after swap:
KEY    ELEMENT
1       10
2       20
3       30
4       40

What is a map::end()?

map::end() function is an inbuilt function in C++ STL, which is defined in <map> header file. end() is used to access the element which is at after the last element in the container, or the past the last element.

This function returns an iterator which points to the element which is next to the last element of the container. When the container has no values in it the iterator cannot be dereferenced

Usually begin() and end() are used to iterate through the map container by giving them range.

Syntax

map_name.end();

Parameters

The function accepts no parameter.

Return value

This function returns an iterator which is pointing to the past the last value of the map container.

Example

Input

std::map<int> mymap;
mymap.insert({‘a’, 10});
mymap.insert({‘b’, 20});
mymap.insert({‘c’, 30});
mymap.end();

Output

error

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   map<int, int> TP_1;
   TP_1[1] = 10;
   TP_1[2] = 20;
   TP_1[3] = 30;
   TP_1[4] = 40;
   cout<<"Elements of TP_1 after swap:\n"<< "\tKEY\tELEMENT\n";
   for (auto i = TP_1.begin(); i!= TP_1.end(); i++) {
      cout << "\t" << i->first << "\t" << i->second << '\n';
   }
   return 0;
}

Output

Elements of TP_1 after swap:
KEY    ELEMENT
1       10
2       20
3       30
4       40
raja
Published on 15-Apr-2020 12:47:05
Advertisements