简体   繁体   English

string来提升:: uuid转换

[英]string to boost::uuid conversion

I've just started using boost in c++ and I just wanted to ask a couple of questions relating to uuids. 我刚刚开始在c ++中使用boost,我只想问一些与uuids有关的问题。

I am loading in a file which requires I know the uuids so I can link some objects together. 我正在加载一个文件,要求我知道uuids所以我可以将一些对象链接在一起。 For this reason, I'm trying to write my own uuids but I'm not sure if there's any special conditions for the strings etc as the strings I've been using (usually something basic) are not working. 出于这个原因,我正在尝试编写自己的uuids,但我不确定字符串是否有任何特殊条件,因为我一直在使用的字符串(通常是基本字符串)不起作用。 Can anyone point me in the right direction? 谁能指出我正确的方向? I've tried using a string generator, but to no avail thus far so I'm assuming there's something wrong with my strings (which have currently just been random words). 我已经尝试过使用字符串生成器,但到目前为止无济于事,所以我假设我的字符串有问题(目前只是随机字)。

Here's a short example kind of thing, can't give the real code: 这是一个简短的例子,不能给出真正的代码:

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());
    }
}

I took your question as "I need a sample". 我把你的问题称为“我需要一个样本”。 Here's a sample that shows 这是一个展示的样本

  • reading
  • writing 写作
  • generating 发电
  • comparing 比较

uuids with Boost Uuid. uuids与Boost 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);
    }
}

See it Live On Coliru 看到Live On Coliru

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM