NoSeatbelts NoSeatbelts - 9 months ago 41
C++ Question

std::map's find fails to find, but element in map with manual scan

I am using std::map and a list to keep track of windowing over elements and associated scores. When a window is full, I want to pop an element off the windows queue and remove it from the map. Because there can be duplicates, the map keeps track of how many times each element in the window was encountered. I'm also using an ordered map so that I can keep getting the minimum values in a given window.

My problem is that find() is returning end() when it is not expected to.
And when I iterate through the map, I find the element to be present. I don't want to sacrifice the logarithmic complexity of using map.

tl;dr: std::map says an element isn't in the map. A manual scan says it is.

[Edit: Bryan Chen's suggestion fixed the map. Thank you!]

#include <cstdint>
#include <cstdio>
#include <cinttypes>
#include <map>
#include <list>
#include <vector>

#include "util.h"
#include "kmerutil.h"

namespace kpg {

struct elscore_t {
uint64_t el_, score_;
INLINE elscore_t(uint64_t el, uint64_t score): el_(el), score_(score) {
LOG_ASSERT(el == el_);
LOG_ASSERT(score == score_);
INLINE elscore_t(): el_(0), score_(0) {}
inline bool operator <(const elscore_t &other) const {
return score_ < other.score_ || el_ < other.el_; // Lexicographic is tie-breaker.
inline bool operator ==(const elscore_t &other) const {
return score_ == other.score_ && el_ == other.el_; // Lexicographic is tie-breaker.
std::string to_string() const {
return std::to_string(el_) + "," + std::to_string(score_);

struct esq_t: public std::list<elscore_t> {

typedef std::map<elscore_t, unsigned> esmap_t;

class qmap_t {
// I could make this more efficient by using pointers instead of
// elscore_t structs.
// *maybe* TODO
// Could also easily templatify this module for other windowing tasks.
esq_t list_;
esmap_t map_;
esmap_t map_;
const size_t wsz_; // window size to keep
void add(const elscore_t &el) {
auto it(map_.upper_bound(el));
if(it->first == el) ++it->second;
else map_.emplace(el, 1);
void del(const elscore_t &el) {
auto f(map_.find(el));
if(f == map_.end()) {
LOG_DEBUG("map failed :(\n");
for(f = map_.begin(); f != map_.end(); ++f)
if(f->first == el)
LOG_ASSERT(f != map_.end());
if(--f->second <= 0)
uint64_t next_value(const uint64_t el, const uint64_t score) {
list_.emplace_back(el, score);
LOG_ASSERT(list_.back().el_ == el);
LOG_ASSERT(list_.back().score_ == score);
if(list_.size() > wsz_) {
//fprintf(stderr, "list size: %zu. wsz: %zu\n", list_.size(), wsz_);
LOG_ASSERT(list_.size() <= wsz_);
return list_.size() == wsz_ ? map_.begin()->first.el_: BF;
// Signal a window that is not filled by 0xFFFFFFFFFFFFFFFF
qmap_t(size_t wsz): wsz_(wsz) {
void reset() {


Answer Source

As T.C. pointed out in his answer, your operator< is not correct.

You can use std::tie to do lexicographical comparison

return std::tie(score_, el_) < std::tie(other.score_, other.el_);

Otherwise you can do

if (score_ == other.score_) {
  return el_ < other.el_; // use el_ to compare only if score_ are same
return score_ < other.score_;