Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I should use std::map::at to retrieve map element

Tags:

c++

c++11

stdmap

I have read different articles on web and questions at stackoverflow, but for me it is not clear is there any exclusive case when it is better to use std::map::at to retrieve map element.

According to definition, std::map::at

Returns a reference to the mapped value of the element identified with key k.

If k does not match the key of any element in the container, the function throws an out_of_range exception.

For me only case when it is worth to use std::map::at when you 100% sure that element with particular key exist, otherwise you should consider exception handling.

  1. Is there any case where std::map::at considered as most efficient and elegant way to do? In what cases you will recommend to use std::map::at ?
  2. Am I right that it is better to use map::find() when there is a possibility to not have element with such a key? And map::find() it is faster and more elegant approach?
if ( map.find("key") != map.end() ) {     // found   } else {     // not found } 

p.s

map::operator[] sometimes can be dangerous, because if an element doesn't exist then it will inserts it.

EDITED: links somehow related link 1 link 2 link 3 link 4 link 5 link 6

like image 268
T M Avatar asked Oct 20 '15 11:10

T M


People also ask

What does map at return?

map::at() at() function is used to reference the element mapped to the key value given as the parameter to the function. For example, if we have a string “hi” mapped to an integer 1, then passing the integer 1 as the parameter of at() function will return the string “hi”.

What is std::map used for?

If you mean std::map , it stores pairs of values. In each pair, the first value is called the key, and can be used to quickly look up the associated other value.

What is the complexity of std::map :: insert () method?

Time complexity: k*log(n) where n is size of map, k is no. of elements inserted.

What is std::map in C++?

std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare . Search, removal, and insertion operations have logarithmic complexity. Maps are usually implemented as red-black trees.


2 Answers

Contrary to most existing answers here, note that there are actually 4 methods related to finding an element in a map (ignoring lower_bound, upper_bound and equal_range, which are less precise):

  • operator[] only exist in non-const version, as noted it will create the element if it does not exist
  • at(), introduced in C++11, returns a reference to the element if it exists and throws an exception otherwise
  • find() returns an iterator to the element if it exists or an iterator to map::end() if it does not
  • count() returns the number of such elements, in a map, this is 0 or 1

Now that the semantics are clear, let us review when to use which:

  • if you only wish to know whether an element is present in the map (or not), then use count().
  • if you wish to access the element, and it shall be in the map, then use at().
  • if you wish to access the element, and do not know whether it is in the map or not, then use find(); do not forget to check that the resulting iterator is not equal to the result of end().
  • finally, if you wish to access the element if it exists or create it (and access it) if it does not, use operator[]; if you do not wish to call the type default constructor to create it, then use either insert or emplace appropriately
like image 74
Matthieu M. Avatar answered Sep 30 '22 18:09

Matthieu M.


std::map::at() throws an out_of_range exception if the element could not be found. This exception is a kind of logic_error exception which for me is a kind of synonym of assert() from the usage standpoint: it should be used to report errors in the internal logic of the program, like violation of logical preconditions or class invariants.

Also, you can use at() to access const maps.

So, for your questions:

  1. I will recommend using at() instead of [] when accessing const maps and when element absence is a logic error.
  2. Yes, it's better to use map::find() when you're not sure element is here: in this case it's not a logic error and so throwing and catching std::logic_error exception will not be very elegant way of programming, even if we don't think about performance.
like image 21
alexeykuzmin0 Avatar answered Sep 30 '22 18:09

alexeykuzmin0