unordered_map仅查找先前找到的项目之后的项目
unordered_map find only items after the previously found item
我有一个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
相关文章:
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 有没有一种简单的方法可以在C++中获取特定索引之后向量中的所有项目?
- 移动了 QT 项目,重命名文件夹和项目名称,Moc'ing 返回"'-I'之后缺少值"
- 为什么应该在每个项目包配置之后包含(CPack)?
- 我正在尝试在Eclipse中构建一个C 项目,但是即使在使用MINGW路径设置路径变量之后,也会获得构建错误
- 链接列表中最后一项之后的项目是否为0
- 在x元素之后,从QT中删除x元素之后的文件夹中的项目
- unordered_map仅查找先前找到的项目之后的项目
- 在 Items.ItemAdd 事件之后获取邮件项目
- 项目.exe在系统("pause")之后触发了断点
- 欧拉项目任务#8,代码在某个点之后开始返回错误的答案
- 在C++推送到队列后,如果我在之后编辑该项目数据,它会影响队列中的数据