我在unordered_set中插入一个对象。 我想在插入/之后得到实际的对象拷贝。std :: unordered_set insert获取对象
#include <iostream>
#include <unordered_set>
#include <string>
int main (void)
{
std::unordered_set<std::string> sentence;
auto res = sentence.insert("alex");
if (res.second)
std::cout << *res.first << std::endl;
return 0;
}
以上简单的例子,工作正常。 当我尝试它与我的自定义类:
std::unordered_set<Policy> _policies;
...
...
if (std::shared_ptr<Policy> policy = calculatePolicy (state, action, 0.f))
{
auto res = _policies.insert (*policy);
if (res.second)
return std::make_shared<Policy>(*res.first);
我得到:
no known conversion for argument 1 from ‘std::__detail::_Node_const_iterator<Policy, true, true>’ to ‘const Policy&’
为什么我会得到一个const MyClass的&的的std :: _详细:: _ Node_const_iterator isntead?
如何获取刚刚插入的对象的引用?
将'_policies' var的设置声明添加到发布的问题代码中。这一点很重要。我有一种感觉,如果你没有在你的设置中存储'std :: shared_ptr',你应该。 (它看起来不像你)。 –
WhozCraig
2014-09-26 22:10:18
是R Sahu,对不起,错字 – 2014-09-26 22:15:47
@WhozCraig不,我不是我正在使用Policy对象。我已经将它更改为std :: unordered_set>,我没有收到编译错误。谢谢,谨慎地阐述为什么会发生这种情况? –
2014-09-26 22:17:58