jeudi 5 avril 2018

std::map get previous of lower_bound

I have a std::map as :

std::map<unsigned int, char> m_map
{
  std::make_pair (0, 'a'),
  std::make_pair (5, 'b'),
  std::make_pair (10, 'c'),
  std::make_pair (15, 'd'),
};

I am using std::map::lower_bound to get the iterator to the lower bound.

auto lower_bound = m_map.lower_bound(7); // points to  (10, 'c')

I want to get the previous element to lower_bound. Currently I am iterating over the whole std::map and storing the previous element. If lower bound reached then I break the loop.

I want to know, is there a smarter way to get the pointer pointing to the previous element to lower_bound.

Aucun commentaire:

Enregistrer un commentaire