快问。如果我尝试致电,确切的行为是什么:
std::unordered_map<int, std::unordered_map<int, double>> myUMap;
myUMap[5].insert(......);
Run Code Online (Sandbox Code Playgroud)
其中 ...... 是一对,并假设 myUMap[5] 还不存在?
我听说尝试使用不存在的键访问 unordered_map 会以这种方式在无序映射中创建该键,但在这种情况下,如果 myUMap[5] 不存在,或者如果确实如此,更新 myUMap[5] 中的地图?
所以我知道map1.insert(map2.begin(), map2.end());将把所有元素插入map2到 中map1。
但其中可能map2已经存在一些元素map1。这些元素将不会更新。
e.g. map1 has { 3 : 4, 6 : 7 }
map2 has { 11: 5, 6 : 0 }
Now if I do map1.insert(map2.begin(), map2.end()), I will get
map1 = { 3: 4, 6 : 7, 11 : 5 }
But what I want is
map1 = { 3: 4, 6 : 0, 11 : 5 }
Run Code Online (Sandbox Code Playgroud)
我想知道是否有任何函数map1.insert(map2.begin(), map2.end());可以强制更新已经存在的密钥?
更新:我知道可以使用以下方法完成:map1[k] = v …
我正在基于 LeetCode 练习实现 LRU 缓存,但以下代码无法编译
using namespace std;
class LRUCache {
private:
list<int> data;
unordered_map<int, list<int>::iterator&> keys_to_data;
void update_recency(int key, list<int>::iterator& it) {
data.erase(it);
data.push_front(key);
keys_to_data[key]; // issue here
}
public:
LRUCache(int capacity) {
}
int get(int key) {
int value = -1;
auto value_it = keys_to_data.find(key);
if(value_it != keys_to_data.end()) {
value = *(value_it->second);
update_recency(key, value_it->second);
}
return value;
}
void put(int key, int value) {
}
};
Run Code Online (Sandbox Code Playgroud)
/Library/Developer/CommandLineTools/usr/include/c++/v1/tuple:1360:7: 错误:对类型 'std::__1::__list_iterator' 的引用需要一个初始化器 Second(_VSTD::forward<_Args2>(_VSTD: :get<_I2>(__second_args))...) ^
...巨大的堆栈跟踪...
/Users/Paul/Desktop/int/main.cpp:17:21:注意:在成员函数 'std::__1::unordered_map &、std::__1::hash、std::__1::equal_to 的实例化中, …
我有一个unordered_map<char, mystruct> h。
h如果不包含密钥,会返回什么x?
mystruct ms = h['x'];
Run Code Online (Sandbox Code Playgroud) 如何使用变量作为 unordered_map 中的键?
例如,我想让下面的代码工作。
using VariantType = std::variant<int, std::string, unsigned int>;
std::unordered_map<VariantType, int, $some_hash_function$> m;
Run Code Online (Sandbox Code Playgroud)
如何实现 $some_hash_function$?
我正在研究五法则及其近亲(四法则和 1/2、复制和交换习语、朋友交换函数)。
我在测试课上实施了四和 1/2 规则。它编译得很好。我的实现中是否存在任何隐藏的错误?
我特别关注存储在m_unoreredd_map属性中的 unique_ptrs,我将其移动到复制构造函数中,因为它们无法复制。这是处理类中 unique_ptr 的正确方法吗?
#ifndef SOMECLASS_H
#define SOMECLASS_H
#include "someotherclass.h"
#include <QString>
#include <QStringList>
#include <memory>
#include <string>
#include <unordered_map>
class SomeClass
{
QString m_qstring;
std::unordered_map<std::string, std::unique_ptr<SomeOtherClass>> m_unordered_map;
int m_int;
std::string m_string;
QStringList m_qstringlist;
public:
SomeClass() = default;
// Rule of 5 (or Rule of 4 and 1/2)
// From https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom#3279550
~SomeClass() = default; // Destructor
SomeClass(SomeClass &other); // Copy constructor
SomeClass(SomeClass &&other); // Move constructor
SomeClass &operator=(SomeClass other); // Copy/Move assignment …Run Code Online (Sandbox Code Playgroud) 检查值是否存在于中的最快方法是什么std::map<int, int>?我应该使用吗unordered map?在这个任务中,我不能使用任何库来代替 std。
现在,我不知道有什么方法可以在不检查所有值的情况下做到这一点。
我试图在全局级别定义两个无序映射,我用循环定义它们,并且我不想将循环放入main().
更具体地说,我试图定义一个基于 36 的字母表,我使用阿拉伯数字表示数字 0-9,然后使用基本拉丁字母表表示数字 10-35,一个映射用于将字符串转换为整数,并且它应该支持大写和小写,另一个用于将整数转换为给定基数的字符串表示形式,并且它只能是小写。
这是我的代码,显然它不起作用:
#include <unordered_map>
using namespace std;
const unordered_map<string, int> digits;
const unordered_map<int, string> alphabet;
for (int i = 0; i < 10; i++) {
string d = char(i + 48);
digits[d] = i;
alphabet[i] = d;
}
for (int i = 10; i < 36; i++) {
string upper = char(i + 55);
string lower = char(i + 87);
digits[upper] = i;
digits[lower] = i;
alphabet[i] = lower;
}
Run Code Online (Sandbox Code Playgroud)
我知道如果我将循环放入函数中main(),它应该可以工作。但我不想那样做。 …
我正在尝试创建一个unordered_map,其键将是Gdiplus :: Color类的成员和浮点数,但我无法理解为什么我不能这样做.这是我的声明
std::unordered_map<std::pair(Gdiplus::Color, float), std::shared_ptr<Gdiplus::Pen>> mymap;
Run Code Online (Sandbox Code Playgroud) 我有一个无序的地图,使用指向自定义对象的指针作为键.出于某种原因,只有在键不是const的情况下才能使用键查找值.
这是一个示例(std::string作为自定义对象的替代):
std::unordered_map<std::string*, int> my_map;
std::string key {"test"};
const std::string const_key {"test2"};
auto value = my_map.at(&key); // this works as expected
auto other_value = my_map.at(&const_key); // this doesn't compile
error: invalid conversion from 'const string* {aka const std::__cxx11::basic_string<char>*}'
to 'std::unordered_map<std::__cxx11::basic_string<char>*, int>::key_type
{aka std::__cxx11::basic_string<char>*}' [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
为什么查找要求指针为非const?
c++ ×10
unordered-map ×10
algorithm ×1
c++11 ×1
c++20 ×1
dictionary ×1
performance ×1
qt ×1
rule-of-five ×1
std-pair ×1
stl ×1
unordered ×1
variant ×1