使用boost序列化、std::tr1::unordered_map和自定义键的奇怪行为
Odd behavior using boost serialization, std::tr1::unordered_map, and a custom key
我一直在研究一些关于使用自定义键对std::tr1::unordered_map
进行增强序列化的奇怪行为。在序列化密钥和序列化包含密钥的unordered_map
之间,四个成员有4种不同的情况:反序列化密钥、反序列化unordered_map
、原始密钥、原始unordered_map
- 使用原始密钥和原始
unordered_map
- 使用反序列化的密钥和反序列化的
unordered_map
- 使用具有原始
unordered_map
的反序列化密钥 - 使用具有反序列化
unordered_map
的原始密钥
前两个案例正如您所期望的那样工作,但后两个案例没有正确映射。我在下面创建了一个最低限度的工作示例。请注意,unordered_map
需要boost标头才能序列化。我已经把它附在底部了。
#include <cstdlib>
#include <unordered_map>
#include <string>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include "unordered_map.hpp"
class HashKey {
public:
HashKey() = default;
HashKey(const HashKey& orig) = default;
virtual ~HashKey() = default;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & const_cast<unsigned long &>(id);
}
inline bool operator==(const HashKey& key) const {
return this->id == key.id;
}
struct KeyHasher {
std::size_t operator()(const HashKey* key) const {
return boost::hash<unsigned long>()(key->id);
}
};
private:
static unsigned long int idCounter;
const unsigned long int id = HashKey::idCounter;
};
unsigned long int HashKey::idCounter = 0;
int main(int argc, char** argv) {
std::tr1::unordered_map<HashKey*,std::string,HashKey::KeyHasher> map;
HashKey key;
map[&key]="works!";
{
std::ofstream ofs("key.save");
boost::archive::text_oarchive oa(ofs);
oa << key;
oa << map;
}
HashKey newKey;
std::tr1::unordered_map<HashKey*,std::string,HashKey::KeyHasher> newMap;
{
std::ifstream ifs("key.save");
boost::archive::text_iarchive ia(ifs);
ia >> newKey;
ia >> newMap;
}
std::cout<<"Result: "<<map[&key]<<"n";
std::cout<<"Result: "<<newMap[&newKey]<<"n";
std::cout<<"Result: "<<map[&newKey]<<"n";
std::cout<<"Result: "<<newMap[&key]<<"n";
return 0;
}
运行时此代码的输出为:
Result: works!
Result: works!
Result:
Result:
我不明白为什么最后两个案例不起作用。我检查了散列函数输出的值,它们是正确的。我怀疑这与用作键的指针的operator()==
有关,但我不确定如何检查。我希望能够在我的代码中使用所有4种情况。有没有说明为什么这不起作用?谢谢
这是用于序列化哈希映射的unordered_map.hpp
。这来自于这张助推票。将其包含在MWE:中
#ifndef BOOST_SERIALIZATION_UNORDERED_MAP_HPP
#define BOOST_SERIALIZATION_UNORDERED_MAP_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// serialization/unordered_map.hpp:
// serialization for stl unordered_map templates
// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// See http://www.boost.org for updates, documentation, and revision history.
#include <boost/tr1/unordered_map.hpp>
#include <boost/config.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/unordered_collections_save_imp.hpp>
#include <boost/serialization/unordered_collections_load_imp.hpp>
#include <boost/serialization/split_free.hpp>
namespace boost {
namespace serialization {
namespace stl {
// map input
template<class Archive, class Container>
struct archive_input_unordered_map
{
inline void operator()(
Archive &ar,
Container &s,
const unsigned int v
){
typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
detail::stack_construct<Archive, type> t(ar, v);
// borland fails silently w/o full namespace
ar >> boost::serialization::make_nvp("item", t.reference());
std::pair<BOOST_DEDUCED_TYPENAME Container::const_iterator, bool> result =
s.insert(t.reference());
// note: the following presumes that the map::value_type was NOT tracked
// in the archive. This is the usual case, but here there is no way
// to determine that.
if(result.second){
ar.reset_object_address(
& (result.first->second),
& t.reference().second
);
}
}
};
// multimap input
template<class Archive, class Container>
struct archive_input_unordered_multimap
{
inline void operator()(
Archive &ar,
Container &s,
const unsigned int v
){
typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
detail::stack_construct<Archive, type> t(ar, v);
// borland fails silently w/o full namespace
ar >> boost::serialization::make_nvp("item", t.reference());
BOOST_DEDUCED_TYPENAME Container::const_iterator result
= s.insert(t.reference());
// note: the following presumes that the map::value_type was NOT tracked
// in the archive. This is the usual case, but here there is no way
// to determine that.
ar.reset_object_address(
& result->second,
& t.reference()
);
}
};
} // stl
template<
class Archive,
class Key,
class HashFcn,
class EqualKey,
class Allocator
>
inline void save(
Archive & ar,
const std::tr1::unordered_map<
Key, HashFcn, EqualKey, Allocator
> &t,
const unsigned int /*file_version*/
){
boost::serialization::stl::save_unordered_collection<
Archive,
std::tr1::unordered_map<
Key, HashFcn, EqualKey, Allocator
>
>(ar, t);
}
template<
class Archive,
class Key,
class HashFcn,
class EqualKey,
class Allocator
>
inline void load(
Archive & ar,
std::tr1::unordered_map<
Key, HashFcn, EqualKey, Allocator
> &t,
const unsigned int /*file_version*/
){
boost::serialization::stl::load_unordered_collection<
Archive,
std::tr1::unordered_map<
Key, HashFcn, EqualKey, Allocator
>,
boost::serialization::stl::archive_input_unordered_map<
Archive,
std::tr1::unordered_map<
Key, HashFcn, EqualKey, Allocator
>
>
>(ar, t);
}
// split non-intrusive serialization function member into separate
// non intrusive save/load member functions
template<
class Archive,
class Key,
class HashFcn,
class EqualKey,
class Allocator
>
inline void serialize(
Archive & ar,
std::tr1::unordered_map<
Key, HashFcn, EqualKey, Allocator
> &t,
const unsigned int file_version
){
boost::serialization::split_free(ar, t, file_version);
}
// unordered_multimap
template<
class Archive,
class Key,
class HashFcn,
class EqualKey,
class Allocator
>
inline void save(
Archive & ar,
const std::tr1::unordered_multimap<
Key, HashFcn, EqualKey, Allocator
> &t,
const unsigned int /*file_version*/
){
boost::serialization::stl::save_unordered_collection<
Archive,
std::tr1::unordered_multimap<
Key, HashFcn, EqualKey, Allocator
>
>(ar, t);
}
template<
class Archive,
class Key,
class HashFcn,
class EqualKey,
class Allocator
>
inline void load(
Archive & ar,
std::tr1::unordered_multimap<
Key, HashFcn, EqualKey, Allocator
> &t,
const unsigned int /*file_version*/
){
boost::serialization::stl::load_unordered_collection<
Archive,
std::tr1::unordered_multimap<
Key, HashFcn, EqualKey, Allocator
>,
boost::serialization::stl::archive_input_unordered_multimap<
Archive,
std::tr1::unordered_multimap<
Key, HashFcn, EqualKey, Allocator
>
>
>(ar, t);
}
// split non-intrusive serialization function member into separate
// non intrusive save/load member functions
template<
class Archive,
class Key,
class HashFcn,
class EqualKey,
class Allocator
>
inline void serialize(
Archive & ar,
std::tr1::unordered_multimap<
Key, HashFcn, EqualKey, Allocator
> &t,
const unsigned int file_version
){
boost::serialization::split_free(ar, t, file_version);
}
} // namespace serialization
} // namespace boost
#endif // BOOST_SERIALIZATION_UNORDERED_MAP_HPP
所以说实话,我不完全理解发生了什么,因为我不知道如何:
newMap.contains(&newKey)
可能是真的。由于您将指针存储到键,所以在反序列化newKey
时,它的地址不应该更改,因此它不可能在新映射中。也就是说,为了让一切变得更简单,你可能想做的只是。。。不使用指针:
typedef std::tr1::unordered_map<HashKey,
std::string,
HashKey::KeyHasher> MapType;
HashKey key;
MapType map;
map[key] = "works";
// serialize stuff...
HashKey newKey;
MapType newMap;
// deserialize stuff...
assert(map.contains(key));
assert(map.contains(newKey));
assert(newMap.contains(key));
assert(newMap.contains(newKey));
这需要将KeyHasher
固定为使用HashKey&
而不是HashKey*
。如果两把钥匙相等,它们应该是可互换的。如果使用指针,则最终依赖于指针相等,这与值相等完全无关(p1 == p2
表示*p1 == *p2
,但不是相反)。
如果你真的坚持让KeyType是HashKey*
,那么你可以做:
struct Pred {
bool operator()(const HashKey* a, const HashKey* b) const {
return (*a) == (*b);
}
};
typedef std::tr1::unordered_map<HashKey*,
std::string,
HashKey::KeyHasher,
Pred> MapType;
但我建议第一种方式。
相关文章:
- 自定义对象的dlib序列化在gcc中失败
- 如何使用自定义对象的序列化在 c++ 中编写自定义二进制文件处理程序
- 自定义初始化数组与 std::make_unique
- 更改命名空间以自定义 Boost XML 的标记名称后的反序列化问题
- C++具有自定义初始化的静态调度
- 如何为自定义模板化迭代器实现 std::d istance()
- C 添加自定义XML标签,用于序列化对向量
- 使用自定义模式进行序列化,并使用Boost进行随机访问
- 使用自定义序列点绘制等高线
- 如何过载<<用于YAML::Emitter的运算符,以序列化包含另一个自定义类的向量的自定义类
- 自定义结构化存储 IPropertySetStorage
- C++ - 如何在自定义模板化数据容器中的迭代器上使用 advance() 启用 ADL
- 使用boost序列化、std::tr1::unordered_map和自定义键的奇怪行为
- 使用 QDataStream 序列化自定义类会导致 C2679 错误
- 如何使用boost序列化轻松地序列化包含另一个自定义类的STL容器的自定义类
- 序列化包含std::string的自定义Struct
- c++的提升.使用自定义对象作为键的hash_map序列化错误
- 使用自定义类的Qlist作为成员序列化类(使用QDataStream)
- 尝试使用QDatastream序列化自定义类的QList时出现错误C2679
- 跟进:提升通过 ZeroMQ 拉套接字传递的序列化自定义C++对象