字符串的boost :: UUID转换 [英] string to boost::uuid conversion

查看:358
本文介绍了字符串的boost :: UUID转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用C ++升压和我只是想问几个有关的UUID的问题。

我加载在需要我知道的UUID,所以我可以一些物体连接在一起的文件。出于这个原因,我试图写我自己的UUID,但我不知道是否有对等的字符串作为字符串我一直在使用(通常是基本的东西)的任何特殊情况不能工作。任何人都可以点我在正确的方向?我使用的是串产生试过,但无济于事迄今所以我假设有什么毛病我的琴弦(已现刚刚被随机单词)。

下面是一个简单的例子类的话,不能给真正的code:

 无效loadFiles(标准::字符串XMLFILE);无效linkObjects(custObj网)
{
    的for(int i = 0; I<&网络 - GT;的getLength();我++)
    {
        网络[I] - > SETID([提振:: UUID]);
        如果(I 0)
            网络由[i] - > addObj(网络[I-1] - >的getId());
    }
}


解决方案

我把你的问题是我需要一个样本。下面是一个示例,演示


  • 阅读


  • 生成

  • 比较

用的UUID UUID升压

 的#include<升压/ UUID / uuid.hpp>
#包括LT&;升压/ UUID / uuid_io.hpp>
#包括LT&;升压/ UUID / random_generator.hpp>
#包括LT&;升压/ lexical_cast.hpp>使用空间boost :: UUID的;诠释的main()
{
    random_generator根;    对(INT I = 0; I&小于10 ++ⅰ)
    {
        UUID new_one =根(); //这里是你如何生成一个        性病::法院LT&;< 你可以只打印:<< new_one<< ;;        //或指定给一个字符串
        标准::字符串as_text =的boost :: lexical_cast的<标准::字符串>(new_one);        性病::法院LT&;< as_text:'<< as_text<< '\\ n;        //现在,读回:
        UUID往返=的boost :: lexical_cast的<&UUID GT;(as_text);        断言(往返== new_one);
    }
}

看它的 住在Coliru

I've just started using boost in c++ and I just wanted to ask a couple of questions relating to uuids.

I am loading in a file which requires I know the uuids so I can link some objects together. 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. 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.

#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

这篇关于字符串的boost :: UUID转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆