简体   繁体   English

我可以创建一个字符串和原子的unordered_map吗 <int> 键值对?

[英]Can I create an unordered_map of string and atomic<int> key-value pairs?

I would like to create an unordered_map of <string, atomic<int>> . 我想创建一个<string, atomic<int>>unordered_map I would use this to increment (fetch_add), store or load the values of the atomic based on a string (the key). 我将使用它来增加(fetch_add),基于字符串(键)存储或加载原子的值。 For example, say I have 10 atomic integer counters, but I want to only get the values of 4 or them. 例如,假设我有10个原子整数计数器,但我只想获取4个或它们的值。 I would like a unordered_map that looked like this: 我想要一个看起来像这样的unordered_map

unordered_map<string, atomic<int>> myValues = {
    {"first", atomic<int>(0)},
    {"second", atomic<int>(0)}, ... the last key being "tenth"}
};

Then say I had a vector of strings like 然后说我有一个向量字符串

vector<string> wanted = {"first", "third", "tenth"};

The I would like to do the following: 我想执行以下操作:

for (auto want: wanted) {
    cout <<  myValues[want].load() << endl;

This should print out the values for the keys in wanted. 这应该打印出想要的键的值。

Can I do this? 我可以这样做吗? If I try to create a map as above, I get the error message that the assignment operator for atomic is deleted? 如果尝试如上所述创建地图,则会收到错误消息,即删除了原子的赋值运算符? Is there a way to do this? 有没有办法做到这一点?

Sure, creating an unordered_map<string, atomic<int>> is possible, but you cannot initialize it using the initializer_list constructor because atomic<T> objects are neither moveable nor copyable. 当然,可以创建unordered_map<string, atomic<int>> ,但是您不能使用initializer_list构造函数对其进行initializer_list因为atomic<T>对象既不可移动也不可复制。

Since the values in your map can be constructed using a single argument, you can use the emplace member function. 由于可以使用单个参数构造映射中的值,因此可以使用emplace成员函数。

std::unordered_map<std::string, std::atomic<int>> m;

for(auto const& key : {"first", "second", "third", "fourth"})
    m.emplace(key, 0);

If your key/value type constructor takes more than one argument, to avoid copies and moves, you must use std::pair 's piecewise construction constructor and the emplace member function. 如果您的键/值类型构造函数使用多个参数,为避免复制和移动,必须使用std::pair分段构造构造函数emplace成员函数。

for(auto const& key : {"first", "second", "third", "fourth"})
    m.emplace(std::piecewise_construct,
              std::forward_as_tuple(key),
              std::forward_as_tuple(0));

If your compiler has C++17 support, and the key type can be constructed using a single argument, then you can also use the less verbose try_emplace member function 如果您的编译器具有C ++ 17支持,并且可以使用单个参数来构造键类型,则还可以使用不太冗长的try_emplace成员函数

for(auto const& key : {"fifth", "sixth", "seventh", "eight"})
    m.try_emplace(key, 0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将键值对插入到新的 unordered_map<int, int> *(指针)在 C++ 中?</int,> - How to insert key value pairs to new unordered_map<int, int>* (pointer) in C++? C++ 更新unordered_map中的值,其中键值对的数据类型为int-unordered_set - C++ Updating values in a unordered_map, where the data type of key-value pair is int-unordered_set 对于C ++ unordered_map,如果它是新的,我如何添加键值对,如果它的键已经存在,则将一个函数应用于该值? - For a C++ unordered_map, how can I add a key-value pair if it's new, and apply a function to the value if it's key already exists? 当wordCount中不存在key时,我应该将++ wordCount [key]用于unordered_map <string,int>吗? - should I use ++wordCount[key] for unordered_map<string, int> when key doesn't exist in wordCount? unordered_map 迭代器指向 end(),如何从 unordered_map 中检索键? - unordered_map iterator points to end(), how can I retrieve a key from unordered_map? 排序std :: unordered_map <std::string, std::atomic<unsigned int> &gt;按值 - Sort std::unordered_map<std::string, std::atomic<unsigned int>> by values 在 unordered_map 中插入字符串键、值时出错 - Error inserting string key,value in unordered_map 如何为 unordered_map 编写一个哈希函数,它以一对为键,但如果我切换对成员的顺序,则返回相同的值? - How do I write a hash function for an unordered_map that takes a pair as key, but return the same value if I switch the order of the pairs members? 无法从分离的线程访问C ++ unordered_map中的键/值对 - Cannot access key-value pair in a C++ unordered_map from a detached thread 使用键值对为零的 k 个存储桶初始化 c++14 unordered_map - Initialize c++14 unordered_map with k buckets with key-value pair of zero
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM