unique_ptr unordered_map:无法从迭代器获取价值
unordered_map of unique_ptr: can't get value from iterator
我正在尝试将unique_ptr存储在一个无序映射中。我使用以下代码:
#include <unordered_map>
#include <memory>
int *function()
{
std::unordered_map< int, std::unique_ptr<int> > hash;
auto iterator=hash.find(5);
return iterator->second().get();
}
当我试图编译这个(gcc 4.7.2)时,我得到了以下错误:
test.cpp: In function ‘int* function()’:
test.cpp:9:29: error: no match for call to ‘(std::unique_ptr<int>) ()’
我不明白这个代码出了什么问题。就好像我需要使用另一种方法从迭代器中提取引用,但我不知道怎么做
Shachar
此行:
return iterator->second().get();
应该是这样的:
return iterator->second.get();
CCD_ 1不是函数,而是映射中包含的CCD_。您现在拥有的代码尝试调用成员变量上的()
运算符。但是由于std::unique_ptr
(存储在second
中)没有定义这样的运算符,编译器无法找到它
second
是std::pair
的成员变量,但您试图像函数一样调用它。请使用以下内容。
return iterator->second.get();
相关文章:
- Boost Spirit,获取迭代器内部语义动作
- 对于set上的循环-获取next元素迭代器
- 如何在C++中仅获取容器内类成员的迭代器
- C++如何获取传递给函数(STL 迭代器)的参数的名称
- 从 QMap<K、T> 获取 Java 样式迭代器,而不指定 T、K
- 获取 c++ 中具有恒定长度的数组的迭代器
- 使用列表迭代器获取所有值
- 如何从迭代器获取列表
- 使用迭代器获取字符串中的短语
- 从数组列表中的字符串迭代器获取 int dat<struct>
- 通过迭代器获取 set 元素的"index"
- 从迭代器获取索引处的元素
- 从boost::property_tree::ptree::迭代器获取ptree
- 从输入迭代器获取最后一项
- Boost:: Dijkstra 最短路径,如何从路径迭代器获取顶点索引
- 使用迭代器获取映射中与条件匹配的每个项
- 使用临时程序将迭代器获取到STL容器时的细微错误:如何避免
- unique_ptr unordered_map:无法从迭代器获取价值
- 防止自定义迭代器获取nullptr
- 无法使用自定义迭代器获取std::find_if进行编译