首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

在Multimap中检索特定值

在Multimap中检索特定值,您可以使用以下方法:

  1. 使用find函数:
代码语言:cpp
复制
#include<iostream>
#include <map>
#include<vector>

int main() {
    std::multimap<int, std::string> my_multimap;
    my_multimap.insert(std::make_pair(1, "one"));
    my_multimap.insert(std::make_pair(2, "two"));
    my_multimap.insert(std::make_pair(3, "three"));
    my_multimap.insert(std::make_pair(4, "four"));
    my_multimap.insert(std::make_pair(5, "five"));
    my_multimap.insert(std::make_pair(6, "six"));
    my_multimap.insert(std::make_pair(7, "seven"));
    my_multimap.insert(std::make_pair(8, "eight"));
    my_multimap.insert(std::make_pair(9, "nine"));
    my_multimap.insert(std::make_pair(10, "ten"));

    int key = 5;
    auto range = my_multimap.equal_range(key);

    for (auto it = range.first; it != range.second; ++it) {
        std::cout << "Key: " << it->first << ", Value: " << it->second<< std::endl;
    }

    return 0;
}
  1. 使用lower_boundupper_bound函数:
代码语言:cpp
复制
#include<iostream>
#include <map>
#include<vector>

int main() {
    std::multimap<int, std::string> my_multimap;
    my_multimap.insert(std::make_pair(1, "one"));
    my_multimap.insert(std::make_pair(2, "two"));
    my_multimap.insert(std::make_pair(3, "three"));
    my_multimap.insert(std::make_pair(4, "four"));
    my_multimap.insert(std::make_pair(5, "five"));
    my_multimap.insert(std::make_pair(6, "six"));
    my_multimap.insert(std::make_pair(7, "seven"));
    my_multimap.insert(std::make_pair(8, "eight"));
    my_multimap.insert(std::make_pair(9, "nine"));
    my_multimap.insert(std::make_pair(10, "ten"));

    int key = 5;
    auto lower_bound = my_multimap.lower_bound(key);
    auto upper_bound = my_multimap.upper_bound(key);

    for (auto it = lower_bound; it != upper_bound; ++it) {
        std::cout << "Key: " << it->first << ", Value: " << it->second<< std::endl;
    }

    return 0;
}

这两种方法都可以在Multimap中检索特定值。find函数返回的是一个指向找到的元素的迭代器,如果没有找到,则返回一个指向end()的迭代器。equal_range函数返回一个包含两个迭代器的pair,第一个迭代器指向特定值的第一个元素,第二个迭代器指向特定值的最后一个元素之后的位置。lower_boundupper_bound函数返回的是一个指向特定值的第一个元素和最后一个元素之后的位置的迭代器。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

6分33秒

048.go的空接口

2分11秒

2038年MySQL timestamp时间戳溢出

4分41秒

076.slices库求最大值Max

2分58秒

043.go中用结构体还是结构体指针

6分33秒

088.sync.Map的比较相关方法

2分25秒

090.sync.Map的Swap方法

6分52秒

1.2.有限域的相关运算

10分30秒

053.go的error入门

7分13秒

049.go接口的nil判断

22分30秒

Game Tech 腾讯游戏云线上沙龙--中东专场

9分19秒

036.go的结构体定义

26分24秒

Game Tech 腾讯游戏云线上沙龙--英国/欧盟专场

领券