【问题标题】:generate a boost.uuid from a boost.path string从 boost.path 字符串生成 boost.uuid
【发布时间】:2011-05-27 09:30:32
【问题描述】:

我在一个管理器类中工作,它包含表示路径的字符串。从这个字符串中,我想有一个 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

提前致谢。

【问题讨论】:

    标签: c++ boost boost-uuid


    【解决方案1】:

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

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

    你最好看看Boost.Hash

    【讨论】:

    • +1:UUID 不打算用作散列。 OP 可能想看看 git 对 SHA 哈希的作用作为一个很好的例子。
    • 是的,这就是我需要的。谢谢你fbrereto
    【解决方案2】:

    我认为你应该仔细阅读http://www.boost.org/doc/libs/1_59_0/libs/uuid/uuid.html#boost/uuid/string_generator.hpp。您使用String Generator,但在文章中boost::uuids::string_generator 类从字符串生成uuid。 如果你想使用boost::uuids::string_generator,你必须使用 gen() 中的正确格式字符串。

    【讨论】:

      猜你喜欢
      • 2015-03-05
      • 2014-01-15
      • 2022-01-22
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      • 2011-12-02
      相关资源
      最近更新 更多