lower_boundとupper_bound
連想コンテナである std::multimap において、特定のキーの範囲を検索するための関数です
upper_bound
が指定キーより大きい最初の要素イテレータを見つける
lower_bound
指定されたキーより大きい最初の要素を指すイテレータ
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
std::multimap<int, std::string> myMultimap;
myMultimap.insert({1, "apple"});
myMultimap.insert({3, "banana"});
myMultimap.insert({2, "blueberry"});
myMultimap.insert({2, "cherry"});
int keyToFind = 2;
// {2, "blueberry"};
auto upperBoundIt = myMultimap.upper_bound(keyToFind);
std::cout << "Elements with key " << keyToFind << ":" << std::endl;
auto iter = myMultimap.lower_bound(keyToFind);
while (iter!=upperBoundIt)
{
std::cout << iter->second << std::endl;
++iter;
}
return 0;
}
出力
Elements with key 2:
blueberry
cherry