[英]string to boost::uuid conversion
我剛剛開始在c ++中使用boost,我只想問一些與uuids有關的問題。
我正在加載一個文件,要求我知道uuids所以我可以將一些對象鏈接在一起。 出於這個原因,我正在嘗試編寫自己的uuids,但我不確定字符串是否有任何特殊條件,因為我一直在使用的字符串(通常是基本字符串)不起作用。 誰能指出我正確的方向? 我已經嘗試過使用字符串生成器,但到目前為止無濟於事,所以我假設我的字符串有問題(目前只是隨機字)。
這是一個簡短的例子,不能給出真正的代碼:
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.