C++ map at() FunctionC++ map at() function is used to access the elements in the map with the given key value. It throws an exception out_of _range, if the accessed key is not present in the map. SyntaxConsider the key value k, syntax would be: Parameterk: Key value of the element whose mapped value is to be accessed. Return valueIt returns a reference to the mapped value of the element with a key value. Example 1Let's see a simple example for accessing the elements. Output: A: 10 B: 20 C: 30 In the above, at() function is used to access the elements of map. Example 2Let's see a simple example to add the elements using their key values. Output: JavaTPoint In the above example, at() function is used to add the elements after initialization using the associated key values. Example 3Let's see a simple example to change the value associated with the key value. Output: Elements are: 100: Nikita 200: Deep 300: Priya 400: Suman 500: Aman Elements after make changes are: 100: Nidhi 200: Deep 300: Pinku 400: Suman 500: Arohi In the above example, at() function is used to change the values associated with their key values. Example 4Let's see a simple example to handle the ?out of range? exception. Output: Java C++ Python Out of Range Exception at map::at The above example throws an out_of_range Exception since there is no key with value z in the map. Next TopicC++ Map |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India