代码之家  ›  专栏  ›  技术社区  ›  city

如何在C++映射中提取具有相同前缀的记录?

  •  0
  • city  · 技术社区  · 11 年前

    现在,我只知道在一张地图中有几个前缀为“head_”的记录。 如果我不知道这些记录在地图中的确切键,我如何提取这些记录? 知道吗?

    1 回复  |  直到 11 年前
        1
  •  2
  •   Xarn    11 年前

    您必须遍历地图。然而,您可以通过拨打 std::map::lower_bound 关键字等于所需前缀。

    void extract_keys(const std::map<std::string, int>& some_map){
        auto iter = some_map.lower_bound("head_");
    
        while (iter->first.find("head_") == 0){
            //do things with key, value pair
            ++iter;
        }
    }
    

    上面的代码应该可以工作,但我承认我没有测试它