Skip to content

Instantly share code, notes, and snippets.

@jogojapan
Created February 25, 2013 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jogojapan/5027365 to your computer and use it in GitHub Desktop.
Save jogojapan/5027365 to your computer and use it in GitHub Desktop.
Example for the use of map::lower_bound and map::upper_bound in a map of pairs
#include <iostream>
#include <utility>
#include <limits>
#include <map>
using mymap = std::map<std::pair<int, int>, int>;
std::ptrdiff_t num_per_node(const mymap &map, const int node_id)
{
using std::distance;
static constexpr int min = std::numeric_limits<int>::min();
static constexpr int max = std::numeric_limits<int>::max();
auto lo = map.lower_bound({node_id,min});
auto hi = map.upper_bound({node_id,max});
return distance(lo,hi);
}
int main() {
mymap m
{ {{1,3},1} , {{3,4},2} , {{3,5},3} , {{3,9},4} , {{4,2},5} , {{4,3},6} , {{5,1},7} , {{8,2},8} };
for (int node_id = 0 ; node_id < 10 ; ++node_id)
std::cout << "node-id " << node_id << ": " << num_per_node(m,node_id) << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment