Quantcast
Channel: How to find if a given key exists in a std::map - Stack Overflow
Viewing all articles
Browse latest Browse all 16

Answer by Hope for How to find if a given key exists in a std::map

$
0
0

Comparing the code of std::map::find and std::map::count, I'd say the first may yield some performance advantage:

const_iterator find(const key_type& _Keyval) const    {   // find an element in nonmutable sequence that matches _Keyval    const_iterator _Where = lower_bound(_Keyval); // Here one looks only for lower bound    return (_Where == end()        || _DEBUG_LT_PRED(this->_Getcomp(),            _Keyval, this->_Key(_Where._Mynode()))                ? end() : _Where);    }size_type count(const key_type& _Keyval) const    {   // count all elements that match _Keyval    _Paircc _Ans = equal_range(_Keyval); // Here both lower and upper bounds are to be found, which is presumably slower.    size_type _Num = 0;    _Distance(_Ans.first, _Ans.second, _Num);    return (_Num);    }

Viewing all articles
Browse latest Browse all 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>