如何在不使用 boost 库的情况下在 C++ 中生成 UUID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24365331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I generate UUID in c++, without using boost library?
提问by PURE
I want to generate UUID for my application to distinguish each installation of my application. I want to generate this UUID using C++ without boost library support. How can I generate UUID using some other opensource library?
我想为我的应用程序生成 UUID 以区分我的应用程序的每个安装。我想在没有 boost 库支持的情况下使用 C++ 生成这个 UUID。如何使用其他一些开源库生成 UUID?
Note: My platform is windows
注意:我的平台是windows
回答by Yuval
As mentioned in the comments, you can use UuidCreate
如评论中所述,您可以使用UuidCreate
#pragma comment(lib, "rpcrt4.lib") // UuidCreate - Minimum supported OS Win 2000
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
UUID uuid;
UuidCreate(&uuid);
char *str;
UuidToStringA(&uuid, (RPC_CSTR*)&str);
cout<<str<<endl;
RpcStringFreeA((RPC_CSTR*)&str);
return 0;
}
回答by twocrush
This will do, if you're using modern C++.
如果您使用的是现代 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();
}
}
回答by CaptainCodeman
If you just want something random, I wrote this small function:
如果你只是想要一些随机的东西,我写了这个小函数:
string get_uuid() {
static random_device dev;
static mt19937 rng(dev());
uniform_int_distribution<int> dist(0, 15);
const char *v = "0123456789abcdef";
const bool dash[] = { 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
string res;
for (int i = 0; i < 16; i++) {
if (dash[i]) res += "-";
res += v[dist(rng)];
res += v[dist(rng)];
}
return res;
}
回答by harmic
The ossp-uuidlibrary can generate UUIDs and has C++ bindings.
所述OSSP-UUID库可以生成UUID,并具有C ++绑定。
It seems extremely simple to use:
使用起来似乎非常简单:
#include <uuid++.hh>
#include <iostream>
using namespace std;
int main() {
uuid id;
id.make(UUID_MAKE_V1);
const char* myId = id.string();
cout << myId << endl;
delete myId;
}
Note that it allocates and returns a C-style string, which the calling code must deallocate to avoid a leak.
请注意,它分配并返回一个 C 风格的字符串,调用代码必须释放该字符串以避免泄漏。
Another possibility is libuuid, which is part of the util-linux package, available from ftp://ftp.kernel.org/pub/linux/utils/util-linux/. Any Linux machine will have it installed already. It does not have a C++ API but is still callable from C++ using the C API.
另一种可能性是 libuuid,它是 util-linux 包的一部分,可从ftp://ftp.kernel.org/pub/linux/utils/util-linux/ 获得。任何 Linux 机器都已经安装了它。它没有 C++ API,但仍然可以使用 C API 从 C++ 调用。
回答by Paul Evans
Traditionally UUID's are simply the machine's MAC address concatenated with with the number of 100-nanosecond intervals since the adoption of the Gregorian calendar in the West. So it's not too difficult to write a C++ class
that does this for you.
传统上,UUID 只是机器的 MAC 地址与自西方采用公历以来的 100 纳秒间隔数连接在一起。所以编写一个 C++class
来为你做这件事并不难。