带有自定义键的C++无序映射导致空值

C++ unordered_map with custom key resulting in null values

本文关键字:映射 空值 无序 C++ 自定义      更新时间:2023-10-16

我使用的std::unordered_map键为Currency,值为货币价格的doubleCurrency是我创建的一个自定义类。这是我尝试过的一个版本:

#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。。。

CurrencyNameable的一个子类这一事实会引起问题吗?我使用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时,它都不会存在。这并不是说该值被"清零"。。。这就是你得到一个默认的构造值。

修复复制构造函数应该可以解决问题。