unordered_map仅查找先前找到的项目之后的项目

unordered_map find only items after the previously found item

本文关键字:项目 之后 map 查找 unordered      更新时间:2023-10-16

我有一个unordered_map,包含一个枚举和一个字符串作为第二个。第一个值可能会以不同的顺序出现多次。下面是一个示例:

enum SomeType
{
    TYPE1,
    TYPE2,
};
static std::unordered_map<SomeType, std::string> value_map =
{
    { TYPE2, "Value that shouldn't be found" },
    { TYPE1, "Value that gets found first" },
    { TYPE2, "Value that also never gets found" },
    { TYPE1, "Value that gets found second" },
    { TYPE2, "Value that gets found last" },
};

我想按如下方式遍历地图:例如,首先我想找到 TYPE1 的对,这将为我带来第一个TYPE1值。在那之后,再次搜索一个TYPE1值不会让我获得第一个值,而是在它之后可以找到下一个值。在此之后搜索TYPE2值只会净出最后一个值。
基本上,我只想找到下一个匹配值,但在最后一个找到的值之前没有。

我多次尝试实现执行此操作的东西,但我不太确定如何实现这样的算法。
如何实现这样的算法?

尝试演示我想要的内容的完整代码示例:https://godbolt.org/g/CgNZnj

你可能想要一个 std::unordered_multimap(多映射)并使用equal_range:

#include <algorithm>
#include <iostream>
#include <unordered_map>
enum SomeType
{
    TYPE1,
    TYPE2,
};
static std::unordered_multimap<unsigned, std::string> value_map =
{
    { TYPE2, "Value that shouldn't be found" },
    { TYPE1, "Value that gets found first" },
    { TYPE2, "Value that also never gets found" },
    { TYPE1, "Value that gets found second" },
    { TYPE2, "Value that gets found last" },
};
int main() {
    auto range = value_map.equal_range(TYPE1);
    for( auto i = range.first; i != range.second; ++i )
        std::cout << i->second << 'n';
}

然而:

  • 结果范围未排序(保持无序)。
  • 如果要保留初始化中使用的相等键的相对顺序,请将 unordered_multimap 替换为 multimap
  • 如果要完全保留初始化顺序,请使用键/值对的序列容器 (std::vector)。关联容器集、映射(及其无序和/或多变体)不保留顺序。

结论:

在关联容器上运行的算法中实现搜索要求是不可能的。

#include <iostream>
#include <string>
#include <vector>
#include <utility>
enum SomeType
{
    TYPE1,
    TYPE2,
    TYPE3,
};
using value_map = std::vector<std::pair<SomeType, std::string>>;
class ValueMap
{
public:
    ValueMap(value_map& map) : map(map), index(0) {}
    value_map& map;
    std::string find_next(SomeType type)
    {
        while ((index != map.size()) && (map[index].first != type))
        {
            ++index;
        }
        if (index != map.size())
        {
            std::string result = map[index].second;
            ++index;
            return result;
        }
        else
        {
            return "-";
        }
    }
private:
    unsigned index;
};
static value_map some_value_map =
{
    { TYPE2, "Value that shouldn't be found" },
    { TYPE1, "Value that gets found first" },
    { TYPE2, "Value that also never gets found" },
    { TYPE1, "Value that gets found second" },
    { TYPE2, "Value that gets found last" },
};
static ValueMap value_mapper(some_value_map);
int main()
{
    std::cout << value_mapper.find_next(TYPE1) << std::endl;
    std::cout << value_mapper.find_next(TYPE1) << std::endl;
    std::cout << value_mapper.find_next(TYPE2) << std::endl;
    std::string some_string = value_mapper.find_next(TYPE1);
    if (some_string == "-")
    {
        std::cout << "Didn't find a match" << std::endl;
    }
    value_mapper.map.push_back({ TYPE3, "Value that gets after the last as new" });
    std::cout << value_mapper.find_next(TYPE3) << std::endl;
    return 0;
}

生产

Value that gets found first
Value that gets found second
Value that gets found last
Didn't find a match
Value that gets after the last as new