带有自定义键的C++无序映射导致空值
C++ unordered_map with custom key resulting in null values
我使用的std::unordered_map
键为Currency
,值为货币价格的double
。Currency
是我创建的一个自定义类。这是我尝试过的一个版本:
#ifndef CURRENCY_H
#define CURRENCY_H
#include "Nameable.h"
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <boost/functional/hash.hpp>
#include "BigDecimal.h"
#include <iostream>
/**
* Represents a single currency. Can be used as keys in a map and as a general
* identifier for determining what unit a value of money is.
* @param name
*/
class Currency: public Nameable {
public:
Currency(std::string name) throw(NameAlreadyTakenException);
Currency(const Currency& orig);
virtual ~Currency();
virtual std::string getName();
virtual void setName(std::string name) throw(NameAlreadyTakenException);
inline bool operator==(const Currency& key) const {
return this->id == key.id;
}
// A custom hasher that I tried using.
struct currencyHasher
{
std::size_t operator()(const Currency& k) const
{
return boost::hash<boost::uuids::uuid>()(k.id);
}
};
boost::uuids::uuid id;
private:
};
// A template specialization for Currency.
namespace std {
template <>
struct hash<Currency> {
std::size_t operator()(const Currency& k) const {
cout<< boost::hash<boost::uuids::uuid>()(k.id)<<"n";
return boost::hash<boost::uuids::uuid>()(k.id);
}
};
}
#endif /* CURRENCY_H */
这里是实现:
#include "Currency.h"
Currency::Currency(std::string name) throw(NameAlreadyTakenException) {
this->setName(name);
this->id = boost::uuids::random_generator()();
}
Currency::Currency(const Currency& orig) {
}
Currency::~Currency() {
}
std::string Currency::getName() {
return this->name;
}
void Currency::setName(std::string name) throw(NameAlreadyTakenException) {
this->name = name;
}
我尝试通过使用自定义类类型作为密钥来实现对以下问题的回答给出的两个建议,使Currency
密钥兼容:C++unordered_map。正如您所看到的,我已经重写了运算符==,并提供了一个自定义哈希器以及专门化模板。
尽管如此,钥匙似乎正在失去价值。这里我指的是doubles,float和int变成0,string变成空字符串。当然,它会导致我使用的任何其他值出现其他问题。例如:
Currency dollar("Dollar")
std::unordered_map<Currency,int,Currency::currencyHasher> currenMap;
currenMap[dollar]=1337;
std::cout<<currenMap[dollar]<<"n";
控制台中的输出为0。利用模板专业化也不起作用:
std::unordered_map<Currency,int> currenMap;
currenMap[dollar]=1337;
std::cout<<currenMap[dollar]<<"n";
也会生成0。。。
Currency
是Nameable
的一个子类这一事实会引起问题吗?我使用boost::uuid作为哈希(使用boost::hash<boost::uuids::uuid>
将id转换为size_t
)我不确定我缺少了什么,我感谢您的帮助。
问题在于复制构造函数:
Currency::Currency(const Currency& orig) {
}
当您复制一个Currency
时,您会得到一个默认构造的id
。当您将Currency
插入到映射中时,它会被复制,并且该副本将具有与原始id不同的id。因此:
currenMap[dollar]=1337;
正在有效地将CCD_ 13添加到映射中。因此,当您查找为dollar
创建的任何id的id时,它都不会存在。这并不是说该值被"清零"。。。这就是你得到一个默认的构造值。
修复复制构造函数应该可以解决问题。
相关文章:
- 正在将无序映射设置为无序映射的值
- 如果我将嵌套映射作为多重映射的值,如何将值插入内部映射?
- 由于没有使用赋值运算符,映射的值是如何初始化的?
- 使用 const char* 键映射 C++ 检索空值
- 映射唯一值和重复值的有效方法.可以访问键或值的位置
- 访问包含矢量对的映射的值
- 返回空值的字符串值
- C++ 中集合和映射中值的lower_bound索引
- boost::any 如何检查空值/未定义的值
- C++ - 无法从基类继承 |提供空值
- C++空值和此指针
- 使用 cin 或 getline() 输入空值
- 如何将整数向量插入到键中,标准::映射的值
- 可变参数列表是否以空值终止?
- 如何将整数重新初始化/设置为空值
- 检查构造函数的空值
- 无法从Linux,C ++执行"my_script" python脚本"main(filename)"功能;pModule = PyImport_Import(pName);返回空值
- 使用Nlohmann JSON将JSON数据保存在文件中时获取空值
- C++空映射赋值:等于vs插入
- 带有自定义键的C++无序映射导致空值