c++ - 从 boost.path 字符串生成 boost.uuid

标签 c++ boost boost-uuid

我在管理器类中工作,它包含表示路径的字符串。从这个字符串中,我想使用如下代码获得一个 boost.uuid:

m_log->addMessage("Generating UUID from path",ZEL_APPENDER,LOGLEVEL_DEBUG);
boost::uuids::string_generator str_gen;
boost::uuids::uuid generatedUUID = str_gen(full_path);

assert(generatedUUID.is_nil() == false);
char msg[500];
snprintf(msg,500,"Successfully generated UUID %s from path",boost::uuids::to_string(generatedUUID).c_str());
m_log->addMessage(msg,ZEL_APPENDER,LOGLEVEL_DEBUG);

但不幸的是,我发现生成的字符串总是相同的,即使 full_path 不同。

此外,当我尝试使用像这样的超简单示例时:

 string s1("helloworld");
 boost::uuids::string_generator str_gen;
 boost::uuids::uuid generatedUUID = str_gen(s1);
 cout << "s1: " << boost::uuids::to_string(generatedUUID) << endl;

Boost 抛出一个运行时异常,指出该字符串无效。你可以帮帮我吗?我找到的唯一文档来源是 here

提前致谢。

最佳答案

基于code in the header该代码不会生成哈希,而是解析UUID-as-string并将其转换为uuid。

听起来您正在寻找基于路径的哈希,这与 UUID 不同。 UUID 旨在是唯一的,这意味着例如可以将具有相同值的路径存储在不同 UUID 下的关联容器中。

你最好还是看看 Boost.Hash .

关于c++ - 从 boost.path 字符串生成 boost.uuid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4475338/

相关文章:

c++ - 使用 boost 检查 std::string 是否是有效的 uuid

c++ - 公共(public)头文件中的外部类以及该类的头文件中的外部类

c++ - 插入适用于 set 但不适用于 unordered_set

c++ - 无法使用 64 位 gcc 在 32 位中构建 boost 库

c++ - shared_ptr.get() 可以被多个线程调用,而另一个线程锁定并调用 shared_ptr.swap() 吗?

c++ - 使用默认 mt19937 RNG 生成的 boost UUID 是否对 session ID 安全?

c++ - 如何用 Cereal 序列化 boost::uuid

c++ - 在 C++ 中,时钟稳定状态是否依赖于编译器?

c++ - 我=j=0;或 i=0; j=0;哪个更有效率?

c++ - 如何迭代 boost::fusion 关联结构并以通用方式访问键