我有这个:
\n\n// static enum of supported HttpRequest to match requestToString\nstatic const enum HttpRequest {\n GET, \n POST,\n PUT,\n DELETE,\n OPTIONS,\n HEAD,\n TRACE\n};\n\n// typedef for the HttpRequests Map\ntypedef boost::unordered_map<enum HttpRequest, const char*> HttpRequests;\n\n// define the HttpRequest Map to get static list of supported requests\nstatic const HttpRequests requestToString = map_list_of\n (GET, "GET")\n (POST, "POST")\n (PUT, "PUT")\n (DELETE, "DELETE")\n (OPTIONS,"OPTIONS")\n (HEAD, "HEAD")\n (TRACE, "TRACE");\nRun Code Online (Sandbox Code Playgroud)\n\n现在如果我打电话
\n\nrequestToString.at(GET);\nRun Code Online (Sandbox Code Playgroud)\n\nit\xc2\xb4s 好的,但是如果我调用一个不存在的密钥,例如
\n\nrequestToString.at(THIS_IS_NO_KNOWN_KEY);\nRun Code Online (Sandbox Code Playgroud)\n\n它给出了运行时异常并且整个过程中止..
\n\n防止这种情况的最好方法是什么?是否有一个编译指示或什么,或者我应该“像java一样”用try/catch块或什么包围它?
\n\n亲切的亚历克斯
\n我有以下代码:
boost::unordered_map<std::string, int> map;
map["hello"]++;
map["world"]++;
for(boost::unordered_map<std::string, int>::iterator it = map.begin(); it < map.end(); it++){
cout << map[it->first];
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时出现以下错误但不知道为什么?
error: no match for ‘operator<’ in ‘it < map.boost::unordered::unordered_map<K, T, H, P, A>::end [with K = std::basic_string<char>, T = int, H = boost::hash<std::basic_string<char> >, P = std::equal_to<std::basic_string<char> >, A = std::allocator<std::pair<const std::basic_string<char>, int> >, boost::unordered::unordered_map<K, T, H, P, A>::iterator = boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<const std::basic_string<char>, int> >*, std::pair<const std::basic_string<char>, int> >]()
Run Code Online (Sandbox Code Playgroud) 我现在正在开发一个处理一些指数时间算法的程序。正因为如此,我的程序的一个主循环被多次运行,我试图尽可能地优化它。
分析表明,大部分时间都花在了 的查找和哈希计算上std::unordered_map。
我很好奇:
有没有办法缓存键的哈希值std::unordered_map,然后将其作为参数提供给稍后插入?
有没有一种方法可以在单个操作中执行以下操作:给定一个键和值{x,y},检查键x是否在地图中,如果不在,则插入并返回{x,y},否则返回地图中已有的{x,z}任何z内容。
我现在正在做这样的事情,但效率低下,因为我必须计算密钥的散列并检查它是否在地图中。但是如果它不在地图中,我会做一个完全独立的插入操作。理论上,检查它是否存在于地图中应该可以找到如果插入它会在地图中的位置。
std::map如果可以减少此操作的时间,我愿意尝试其他数据结构,例如Boost 或其他数据结构。
我有一个
std::unordered_map< std::string, std::unordered_map<std::string, std::string> >
Run Code Online (Sandbox Code Playgroud)
我只想插入键并稍后填写值。
例如像这样:
#include <string>
#include <unordered_map>
int main() {
std::unordered_map< std::string, std::unordered_map<std::string, std::string> > mymap;
mymap.insert("TestKey");
}
Run Code Online (Sandbox Code Playgroud)
这给出了错误:
source.cpp(6): error C2664: 'void std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>::insert(std::initializer_list<std::pair<const _Kty,_Ty>>)' : cannot convert argument 1 from 'const char [8]' to 'std::pair<const _Kty,_Ty> &&'
1> with
1> [
1> _Kty=std::string
1> , _Ty=std::unordered_map<std::string,std::string,std::hash<std::string>,std::equal_to<std::string>,std::allocator<std::pair<const std::string,std::string>>>
1> , _Hasher=std::hash<std::string>
1> , _Keyeq=std::equal_to<std::string>
1> , _Alloc=std::allocator<std::pair<const std::string,std::unordered_map<std::string,std::string,std::hash<std::string>,std::equal_to<std::string>,std::allocator<std::pair<const std::string,std::string>>>>>
1> ]
1> and
1> [
1> _Kty=std::string
1> , _Ty=std::unordered_map<std::string,std::string,std::hash<std::string>,std::equal_to<std::string>,std::allocator<std::pair<const std::string,std::string>>>
1> ]
1> Reason: …Run Code Online (Sandbox Code Playgroud) 在下面给出的示例程序中(来源:http ://www.cplusplus.com/reference/unordered_map/unordered_map/rehash/ )
// unordered_map::rehash
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap;
mymap.rehash(20);
mymap["house"] = "maison";
mymap["apple"] = "pomme";
mymap["tree"] = "arbre";
mymap["book"] = "livre";
mymap["door"] = "porte";
mymap["grapefruit"] = "pamplemousse";
std::cout << "current bucket_count: " << mymap.bucket_count() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出变为:
current bucket_count: 23
Run Code Online (Sandbox Code Playgroud)
为什么桶数变成23?对堆大小有什么影响?堆分配什么时候完成?在存储桶重新散列上还是在实际插入上?动态释放何时完成?何时clear()使用或erase()两者都使用?
这不应该太难,但我被卡住了。
我正在尝试将函数分配给变量,但我需要知道数据类型,以便将其分配给地图。
我成功地做到了这一点:
auto pfunc = Ext::SomeFunction
Run Code Online (Sandbox Code Playgroud)
这将允许我做:
pfunc(arg, arg2);
Run Code Online (Sandbox Code Playgroud)
但是我需要知道“auto”涵盖的数据类型,以便我可以将我的函数映射到字符串。
例如:
std::unordered_map<std::string, "datatype"> StringToFunc = {{"Duplicate", Ext::Duplicate}};
Run Code Online (Sandbox Code Playgroud)
这些函数中的大多数返回 void,但还有其他函数返回 double 和 int。
如果有更好的方法可以做到这一点,请告诉我,但我真的很想知道上面使用的 auto 背后的数据类型。
非常感谢您提供的任何帮助。
我定义了一个名为的类,该类Point将用作unordered_map. 所以,我operator==在类中提供了一个函数,我还提供了一个template specializationfor std::hash。根据我的研究,这是我认为必要的两件事。相关代码如图:
class Point
{
int x_cord = {0};
int y_cord = {0};
public:
Point()
{
}
Point(int x, int y):x_cord{x}, y_cord{y}
{
}
int x() const
{
return x_cord;
}
int y() const
{
return y_cord;
}
bool operator==(const Point& pt) const
{
return (x_cord == pt.x() && y_cord == pt.y());
}
};
namespace std
{
template<>
class hash<Point>
{
public:
size_t operator()(const Point& pt) const
{
return …Run Code Online (Sandbox Code Playgroud) 这是我正在处理的一个简单脚本,但我无法理解它为什么会出现意外行为。
基本上,我有一个带有重复的整数数组,我想将元素在数组中出现的次数以及元素的值存储在 unordered_map 中,
然后,对于映射 { k , v }中的每个条目,我需要确定数组中是否存在k + 1,如果存在,则对其进行处理。你可以在下面看到代码。
vector<int> A = {1, 1, 3, 2, 5, 3};
for (int i = 0; i < A.size(); ++i) m[A[i]]++;
int ans = 0;
for (const auto& e: m) {
if (m[e.first + 1] > 0) ans = max(ans, e.second + m[e.first + 1]);
}
Run Code Online (Sandbox Code Playgroud)
一切似乎都奏效了。但是,当unordered_map 中不存在k + 1时,循环就会终止,我不明白为什么。
根据 c++ 文档,运算符 [] 插入一个新元素,如果它不存在。但这并没有告诉我任何关于循环不起作用的信息。
我怀疑这与我正在修改循环内的 unordered_map 的事实有关。如果是这种情况,你们能详细说明一下吗?
我真的很感谢你的帮助。
我有两个 std::unordered_map
std::unordered_map<int, int> mp1;
std::unordered_map<int, int> mp2;
Run Code Online (Sandbox Code Playgroud)
我需要找到键值对的交集并将其存储在表单的另一个映射中。
std::unordered_map<int, int> mp;
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点??
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void fun(const unordered_map<int, vector<int>>& direct_paths) {
const int var = direct_paths[1][0];
cout << var;
}
int main()
{
unordered_map<int, vector<int>> a;
a[1] = vector<int> {1,2,3};
fun(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码输出如下错误:
error: passing ‘const std::unordered_map<int, std::vector<int> >’ as ‘this’ argument discards qualifiers [-fpermissive]
const int var = direct_paths[1][0];
^
Run Code Online (Sandbox Code Playgroud)
其中,以下代码不输出任何编译错误:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void fun(const vector<int>& direct_paths) {
const int var = direct_paths[1];
cout << var;
}
int main()
{ …Run Code Online (Sandbox Code Playgroud)