我刚刚开始在C ++中使用Boost,我只是想问一些与UUID相关的问题。

我在一个文件中加载,需要我知道uuids,所以我可以将一些对象链接在一起。出于这个原因,我正在尝试编写自己的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());
    }
}
.

有帮助吗?

解决方案

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

的样本

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

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

看它 live在coliru

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top