【问题标题】:string to boost::uuid conversion字符串到 boost::uuid 转换
【发布时间】:2014-03-28 14:20:24
【问题描述】:

我刚刚开始在 c++ 中使用 boost,我只是想问几个与 uuid 相关的问题。

我正在加载一个文件,该文件需要我知道 uuid,以便我可以将一些对象链接在一起。出于这个原因,我正在尝试编写自己的 uuid,但我不确定字符串等是否有任何特殊条件,因为我一直在使用的字符串(通常是基本的)不起作用。谁能指出我正确的方向?我试过使用字符串生成器,但到目前为止无济于事,所以我假设我的字符串有问题(目前只是随机单词)。

这是一个简短的例子,不能给出真实的代码:

void loadFiles(std::string xmlFile);

void linkObjects(custObj network)
{
    for (int i = 0; i < network->getLength(); i++)
    {
        network[i]->setId([boost::uuid]);  
        if (i > 0)
            network[i]->addObj(network[i-1]->getId());
    }
}

【问题讨论】:

    标签: string boost boost-uuid


    【解决方案1】:

    我认为您的问题是“我需要一个样本”。这是一个显示的示例

    • 阅读
    • 写作
    • 生成
    • 比较

    带有 Boost Uuid 的 uuid。

    #include <boost/uuid/uuid.hpp>
    #include <boost/uuid/uuid_io.hpp>
    #include <boost/uuid/random_generator.hpp>
    #include <boost/lexical_cast.hpp>
    
    using namespace boost::uuids;
    
    int main()
    {
        random_generator gen;
    
        for (int i = 0; i < 10; ++i)
        {
            uuid new_one = gen(); // here's how you generate one
    
            std::cout << "You can just print it: " << new_one << "; ";
    
            // or assign it to a string
            std::string as_text = boost::lexical_cast<std::string>(new_one);
    
            std::cout << "as_text: '" << as_text << "'\n";
    
            // now, read it back in:
            uuid roundtrip = boost::lexical_cast<uuid>(as_text);
    
            assert(roundtrip == new_one);
        }
    }
    

    Live On Coliru

    【讨论】:

      猜你喜欢
      • 2015-06-20
      • 2019-01-01
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多