我正在尝试使用标准库优先级队列来对自定义类的对象进行排序Foo。但是,比较元素取决于它们在 unordered_map 中映射到的值map。
我正在尝试构建这样的东西:
std::unordered_map<Foo,double> map;
struct Compare {
bool operator()(const Foo& a, const Foo& b) {
return map[a]<map[b];
}
}
std::priority_queue<Foo,std::vector<Foo>,Compare> queue;
Run Code Online (Sandbox Code Playgroud)
然而,看起来我不允许引用封闭函数的局部变量。
实现这一目标的标准方法是什么?
我偶然发现了 unordered_map 的一个奇怪问题。
\n\n首先,我生成了一条unordered_map<string, Person>记录(“Bob”,Person(1,“Bob”))并将其插入表中。然后我尝试使用带有键“Bob”的 [] 运算符来访问记录,但发生了错误。
这是代码:
\n\n#include<iostream>\n#include<unordered_map>\nusing namespace std;\n\nclass Person\n{\n public:\n int play;\n string name;\n Person(int p, string n):play(p), name(n) {}\n};\n\nint main()\n{\n unordered_map<string,Person> test;\n test.insert(std::make_pair("haha",Person(1,"haha")));\n cout<<test["haha"].name<<endl;\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n\n当我使用“g++ -S hash.cpp”编译代码时出现错误\n输出:
\n\nIn file included from /usr/include/c++/7/unordered_map:41:0,\n from hash.cpp:2:\n/usr/include/c++/7/tuple: In instantiation of \xe2\x80\x98std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long unsigned int ..._Indexes2 = …Run Code Online (Sandbox Code Playgroud) 我试图找到地图中具有最小值的元素。例如,如果我的地图有
{ (1, 12.3),(2, 6.51), (3, 1.24)}
Run Code Online (Sandbox Code Playgroud)
我想找到这个元素(3, 1.24)。
我编写了以下代码,它尝试以 lambda 格式编写比较器
std::pair<int, double> min = *std::min_element(
my_map.begin(), my_map.end(),
[](std::unordered_map<int, double> a, std::unordered_map<int, double> b) { return a.second < b.second; });
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
error: no matching function for call to object of type '(lambda at my_code.cpp:118:9)'
if (__comp(*__i, *__first))
^~~~~~
my_code.cpp:116:40: note: in instantiation of function template specialization 'std::__1::min_element<std::__1::__hash_map_iterator<std::__1::__hash_iterator<std::__1::__hash_node<std::__1::__hash_value_type<int, double>, void *> *> >, (lambda at my_code.cpp:118:9)>' requested here
std::pair<int, double> min = *std::min_element(
^
my_code.cpp:118:9: note: candidate function …Run Code Online (Sandbox Code Playgroud) 尝试使用以下代码片段更新无序映射,使其仅包含小写字母,但它似乎在删除一个键值对 { [33 '!']: 3 } 后停止,并退出循环,使映射的其余部分未被访问并打印部分更新的地图。
for (auto &i : m)
if (!(i.first >= 'a' && i.first <= 'z'))
m.erase(i.first);
Run Code Online (Sandbox Code Playgroud)
以下调试图像揭示了上述内容
完整代码如下:
#include <iostream>
#include <unordered_map>
#include <algorithm>
using namespace std;
int main()
{
string line = "Try! Try! Try! until you succeed";
//getline(cin, line);
unordered_map<char, int> m;
for (int i = 0; line[i]; i++)
{
char lower = (char)tolower(line[i]);
if (m.find(lower) == m.end())
m.insert(make_pair(lower, 1));
else
m[lower]++;
}
for (auto &i : m) //only updates until !
if …Run Code Online (Sandbox Code Playgroud) 我有一个std::unordered_map<id, town_data> data,其中town_data 是struct不同的信息 - 名称(字符串)、征收的税款(整数)和距首都城镇的距离(整数)。我应该构建一个std::vector<id>,它按前面提到的距离从低到高排序。我很难弄清楚如何才能有效地完成这项工作。我想我可以简单地循环data,通过该循环/插入创建std::map<distance, id>,按距离排序,除非默认情况下对地图进行排序,然后将其逐键复制到新向量std::vector<id>。但这看起来确实是一种浪费的做法。我在这里缺少一些捷径或更有效的解决方案吗?
阅读节点句柄的文档,我注意到节点句柄类型的许多功能可以简单地通过std::unique_ptr. 事实上,节点句柄类型的功能与std::unique_ptr. 它只有一个更符合关联容器特性的接口,例如key_type和mapped_type别名以及获取键/映射值引用的函数。
std::unique_ptr因此,与as的简单特化相比,该标准引入了节点句柄类型,还有其他优点吗node_type?
好的,所以这是我的情况 - 非常简单,但我不确定它是如何工作的(我找不到任何文件......):
我有一个Unordered_map:
typedef unsigned long long U64;
typedef boost::unordered_map<U64, U64> HASH;
Run Code Online (Sandbox Code Playgroud)
我想循环遍历元素(主要是键),非常像使用PHP foreach,但这次使用BOOST_FOREACH,我怀疑像:
HASH myMap;
// .. assignment, etc...
BOOST_FOREACH (U64 key, myMap)
{
// do sth with the Key-Value pair
U64 val = myMap[key];
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
似乎我的程序性能受到以下功能的瓶颈:
inline bool isKeyInMap(const std::string key, std::unordered_map<std::string, MyClass>map)
{
auto t = map.find(key);
if (t == map.end()) return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法加快速度?
编辑:代码是一个更大的功能的一部分.传递价值不是故意的 - 我写了上面的一个删除任何与瓶颈无关的代码.我试图看看是否有更快的方法来执行以下操作:
auto t = map.find(key);
if (t == map.end()) return false;
return true;
Run Code Online (Sandbox Code Playgroud)
如果我和其他人一样好的C++程序员,我不会在这里发布.XD
7号线和8号线为什么不好?为什么输入/减少一些迭代器是不好的?
#include <unordered_map>
int main()
{
std::unordered_multimap<int,int> myumm({{1,3},{3,2},{5,5},{0,9}});
auto first = myumm.begin();
auto second = first+1; // bad
auto third = --myumm.end(); // bad too
auto fourth = myumm.end();
}
Run Code Online (Sandbox Code Playgroud) 我需要在unordered_map不改变顺序的情况下合并两个s.例如,
unordered_map<int,int> map1 ,map2, map3;
Run Code Online (Sandbox Code Playgroud)
map1包含:<4,4> <2,2>
map2包含:<3,3> <1,1>
map1和map2将与map3合并.
所以我的map3应该包含<4,4> <2,2> <3,3> <1,1>
map<int,int>::iterator it = map3.begin();
std::merge(map1.begin(),map1.end(),map2.begin(),map2.end(),inserter(map3,it));
Run Code Online (Sandbox Code Playgroud)
仍然map3订单正在改变.我试过std :: merge和insert,但没有按照上面的要求工作.有人可以帮我这个.或者我在合并和插入时犯了一些错误?
c++ ×10
unordered-map ×10
c++11 ×2
std ×2
stl ×2
boost ×1
c++17 ×1
dictionary ×1
foreach ×1
g++ ×1
lambda ×1
merge ×1
stdmap ×1
unique-ptr ×1
vector ×1