我想为我的应用程序生成 UUID,以区分我的应用程序的每次安装。我想在不支持 boost 库的情况下使用 C++ 生成这个 UUID。如何使用其他一些开源库生成 UUID?
注意:我的平台是windows
最佳答案
如果您使用的是现代 C++,就可以了。
#include <random>
#include <sstream>
namespace uuid {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(0, 15);
static std::uniform_int_distribution<> dis2(8, 11);
std::string generate_uuid_v4() {
std::stringstream ss;
int i;
ss << std::hex;
for (i = 0; i < 8; i++) {
ss << dis(gen);
}
ss << "-";
for (i = 0; i < 4; i++) {
ss << dis(gen);
}
ss << "-4";
for (i = 0; i < 3; i++) {
ss << dis(gen);
}
ss << "-";
ss << dis2(gen);
for (i = 0; i < 3; i++) {
ss << dis(gen);
}
ss << "-";
for (i = 0; i < 12; i++) {
ss << dis(gen);
};
return ss.str();
}
}
关于c++ - 如何在 C++ 中生成 UUID,而不使用 boost 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24365331/