stout代码分析之五:UUID类

  UUID全称通用唯一识别码,被广泛应用于分布式系统中,让所有的元素具有唯一的标识。

  stout中UUID类继承自boost::uuids::uuid。api如下:

  • random, 产生一个UUID对象。
  • fromBytes,从字节数组生成UUID对象 
  • fromString, 从字符串生成UUID对象
  • toBytes, 从UUID对象生成字节数组
  • toString, 从UUID对象生成字符

  废话不多说,上示例代码:

#include "stout/uuid.hpp"
#include <iostream>
int main()
{
  UUID a = UUID::random();
  UUID b = UUID::fromBytes(a.toBytes());
  UUID c = UUID::fromString(a.toString());
  std::cout << a.toString()
    << "," << b.toString()
    << "," << c.toString() << std::endl;
  return 0;
}

 

posted @ 2016-09-19 22:52  后端技术小屋  阅读(298)  评论(0编辑  收藏  举报