
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
map find() function in C++ STL
In this article we will be discussing the working, syntax and examples of map::find() function in C++ STL.
What is Map in C++ STL?
Maps are the associative container, which facilitates to store the elements formed by a combination on 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 map container is accessed by its unique keys.
What is map::find()?
The map::find( ) is a function which comes under <map> header file. This function returns an iterator which points to an element of a given key which we want to search.
Syntax
map_name.find(key_value k);
Parameters
This function accepts the following
Parameters
k: This is the key value which we want to search from the map container
Return value
It returns an iterator pointing to the element associated with key k.
Example
Input
map<char, int> newmap; newmap[‘a’] = 1; newmap[‘b’] = 2; newmap.find(b);
Output
2
Example
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> TP_Map; TP_Map.insert({3, 50}); TP_Map.insert({2, 30}); TP_Map.insert({1, 10}); TP_Map.insert({4, 70}); cout<<"TP Map is : \n"; cout << "MAP_KEY\tMAP_ELEMENT\n"; for (auto i = TP_Map.begin(); i!= TP_Map.end(); i++) { cout << i->first << "\t" << i->second << endl; } //to find the map values at position auto var = TP_Map.find(1); cout<<"Found element at position "<<var->first<<" is : "<<var->second; auto var_1 = TP_Map.find(2); cout<<"\nFound element at position "<<var_1->first<<" is : "<<var_1->second; return 0; }
Output
TP Map is: MAP_KEY MAP_ELEMENT 1 10 2 30 3 50 4 70 Found element at position 1 is : 10 Found element at position 2 is : 30
Example
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> TP_Map; TP_Map.insert({3, 50}); TP_Map.insert({2, 30}); TP_Map.insert({1, 10}); TP_Map.insert({4, 70}); cout<<"TP Map is : \n"; cout << "MAP_KEY\tMAP_ELEMENT\n"; for (auto i = TP_Map.find(2); i!= TP_Map.end(); i++) { cout << i->first << "\t" << i->second << endl; } return 0; }
Output
TP Map is: MAP_KEY MAP_ELEMENT 2 30 3 50 4 70
- Related Articles
- map emplace_hint() function in C++ STL
- map erase() function in C++ STL
- map key_comp() function in C++ STL
- map rbegin() function in C++ STL
- map rend() function in C++ STL
- map lower_bound() function in C++ STL
- map upper_bound() function in C++ STL
- map cbegin() and cend() function in C++ STL
- map crbegin() and crend() function in C++ STL
- map equal_range() in C++ STL
- map emplace() in C++ STL
- map get_allocator in C++ STL
- map value_comp() in C++ STL
- map::clear() in C++ STL
- map insert() in C++ STL
