在矢量中查找映射键的最快方法

Fastest method to find map keys in a vector

本文关键字:方法 映射 查找      更新时间:2023-10-16

我正在寻找更快的方法来提取由矢量键过滤的地图值。

低于标准代码:

std::unordered_map<uint32, user> map;
std::vector<uint32> to_find; 
std::vector<user> results;
auto it = map.begin();
while (it != map.end())
{
if (std::find(to_find.begin(), to_find.end(), it->first) == to_find.end())
results.push_back(it->second);
it++;
}

您可以使用std::binary_search((,它的时间复杂度为O(log n(,其中n是向量to_find的大小。这可能比使用具有线性时间复杂度的std::find()要好得多。

演示

#include <algorithm>
#include <iostream>
#include <vector>
#include <unordered_map>
using user = int;
using uint32 = unsigned long int;
int main()
{
std::unordered_map<uint32, user> myMap = {{1,2},{3,5},{2,9},{4,7}};
std::vector<uint32> to_find = {1,3};
std::vector<user> results;
if(to_find.size() == 0)  // if you have to_find vec size = 0
std::for_each(myMap.cbegin(), myMap.cend(), [&results](const auto& ele)->void
{
results.emplace_back(ele.second);
});
else
{
for(const auto& it: myMap)// binary_search; if not found add the value
if(!std::binary_search(to_find.begin(), to_find.end(), it.first))
results.emplace_back(it.second);
}
for(const auto& it: results) std::cout << it << std::endl;
}

如果两个容器都已排序,则线性方法是使用类似于std::merge的算法:

template <typename KEY, typename VALUE>
std::vector<VALUE>
collect_absents(const std::map<KEY, VALUE>& map, const std::vector<KEY>& present)
{
std::vector<VALUE> res;
auto mit = map.begin();
auto vit = present.begin();
while (mit != map.end() && vit != present.end())
{
if (*vit < mit->first) {
++vit;   
} else if (mit->first < *vit) {
res.push_back(mit->second);
++mit;   
} else { // equal
++vit;
++mit;
}
}
for (; mit != map.end(); ++mit) {
res.push_back(mit->second);
}
return res;
}

演示