Created
February 25, 2013 03:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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