0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C++ mulitmapのlower_boundとupper_bound

Posted at

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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?