在c++中使用map
using Maps in C++
如果我有一组地址(可重复的),其中l1或l2中有命中或缺失。如何在L1和L2中找到该地址的命中/未命中次数?
map<int, pair<int, int> myMap;
while(!EOF){
bool hit = test_bar(address); # L1
if(!hit)
bool hit1= test_bar(address); #L2
myMap.insert(address,make_pair(hit, hit1));
}
这样对吗?
如果我理解得很好,建议的解决方案将不起作用,因为没有办法计算命中或未命中的数量。也许您应该考虑以下内容:counters
结构体跟踪未命中和命中的l1/l2数量。c
映射将每个地址映射到它的计数器。
struct counters {
int l1_hits, l1_misses, l2_hits, l2_misses;
};
map<int, counters> c;
while (!EOF) {
auto it = c.find(adress);
if (it == c.end()) it = c.insert(address, {0,0,0,0});
bool hit1 = test_bar(address); # L1
if (hit1) ++it->second.l1_hits;
else {
++it->second.l1_misses;
bool hit2 = test_bar(address); # L2
if (hit2) ++it->second.l2_hits;
else ++it->second.l2_misses;
}
}
// print
for (auto it : c) {
cout << it.first
<< " " << it.second.l1_hits
<< " " << it.second.l1_misses
<< " " << it.second.l2_hits
<< " " << it.second.l2_misses << endl;
}
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 使用一个考虑到std::map中键值的滚动或换行的键
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 允许从 std::map 的密钥窃取资源?
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用通用值初始化 std::map,不重复
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++:当所有条目都保证是唯一时,替代 std::map
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- 使用字符数组作为 Map 中的键
- C++如何创建 std::map
- C++ equivalent to Java Map getOrDefault?
- 从其他容器中移动构造"std::map"
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?
- C++中 std::map 的运行时复杂度是多少?
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射