C++ flat_map::begin() Function



The std::flat_map::begin() function in C++, is used to return an iterator pointing to the first element of the flat_map container. It allows iteration from the start, making it useful for traversal and modifications.

This function comes in both const and non-const versions, ensuring the compatibility with read-only and modifiable access.

Syntax

Following is the syntax for std::flat_map::begin() function.

 iterator begin() noexcept; or const_iterator begin() const noexcept; 

Parameters

It does not accepts any parameter.

Return Value

This function returns the iterator to the first element.

Example 1

Let's look at the following example, where we are going to consider the basic usage of the begin() function.

 #include <iostream> #include <vector> #include <boost/container/flat_map.hpp> int main() { boost::container::flat_map < int, std::string > a = { {1,"Hi"}, {2,"Hello"}, {3,"Welcome"} }; auto x = a.begin(); std::cout << "Result : " << x -> first << " -> " << x -> second << std::endl; return 0; } 

Output

Output of the above code is as follows −

 Result : 1 -> Hi 
This example modifies the first element using the iterator returned by begin(). cpp Copy Edit

Example 2

Consider the following example, where we are going to modify the first element using the iterator returned by the begin().

 #include <iostream> #include <vector> #include <boost/container/flat_map.hpp> int main() { boost::container::flat_map < int, std::string > x = { {1,"RX100"}, {2,"CIAZ"}, {3,"CRUZE"} }; auto a = x.begin(); a -> second = "AUDI"; std::cout << "Result : " << a -> first << " -> " << a -> second << std::endl; return 0; } 

Output

Following is the output of the above code −

 Result : 1 -> AUDI 
cpp_flat_map.htm
Advertisements