How to use std::lower_bound when the key is part of the data and i want to search for the key?

  Kiến thức lập trình

I have a struct as follows:

struct chunk_offset {
    chunk_id chunk_id;
    unsigned int offset;
    unsigned int offset_size;
};
unsigned int chunk_count;
std::unique_ptr<chunk_offset[]> offsets;

I have a an array of these, sorted by chunk_offset::chunk_id.
since the data is sorted, I want to perform a binary search using std::lower_bound.
However, std::lower_bound expects a parameter that represents the object itself:

chunk read_chunk(chunk_id id) {
    // error, since id is not a chunk_offset
    auto itr = std::lower_bound(offsets.get(), offsets.get()+chunk_count, id); 
}

How can I use std::lower_bound with only my key?

LEAVE A COMMENT