[英]How can I generate UUID in c++, without using boost library?
我想為我的應用程序生成 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();
}
}
如評論中所述,您可以使用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;
}
如果你只是想要一些隨機的東西,我寫了這個小函數:
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;
}
ossp-uuid庫可以生成 UUID 並具有 C++ 綁定。
使用起來似乎非常簡單:
#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;
}
請注意,它分配並返回一個 C 風格的字符串,調用代碼必須解除分配以避免泄漏。
另一種可能性是 libuuid,它是 util-linux 包的一部分,可從ftp://ftp.kernel.org/pub/linux/utils/util-linux/獲得。 任何 Linux 機器都已經安裝了它。 它沒有 C++ API,但仍然可以使用 C API 從 C++ 調用。
2021 年,我建議使用單頭庫stduuid 。 它是跨平台的,不會引入任何不需要的依賴項。
從項目的 github 頁面下載uuid.h
,然后一個最小的工作示例是:
#include <iostream>
#include <string>
#define UUID_SYSTEM_GENERATOR
#include "uuid.h"
int main (void) {
std::string id = uuids::to_string (uuids::uuid_system_generator{}());
std::cout << id << std::endl;
}
有關各種選項和生成器的更多詳細信息和文檔,請參閱項目的 github 頁面。
傳統上,UUID 只是機器的 MAC 地址,加上自西方采用公歷以來的 100 納秒間隔數。 因此,編寫一個為您執行此操作的 C++ class
並不難。
您可以使用我開發的第一個C++ 庫來執行此操作:
#include "rst/guid/guid.h"
std::string guid = rst::Guid().AsString();
// Performs no allocations.
std::cout << rst::Guid().AsStringView().value() << std::endl;
這是一個使用隨機庫在c ++中基於字符串生成唯一uuid的函數,而不使用boost庫
std::string generate_uuid(std::string &seedstring)
{
auto sum = std::accumulate(seedstring.begin(), seedstring.end(), 0);
std::mt19937 gen(sum);
std::uniform_int_distribution<> dis(0, 15);
std::uniform_int_distribution<> dis2(8, 11);
std::stringstream ss;
ss.clear();
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);
};
cout << "uuid is" << ss.str() << endl;
return ss.str();
}
this function will generate same uuid until seedstring value is same.
這段代碼對我有用:)。
#pragma comment(lib, "rpcrt4.lib")
#include <windows.h>
#include <rpcdce.h>
/*
* Generate a Universal Unique Identifier using windows api.
* Note: throw exceptions if uuid wasn't created successfully.
*/
std::string generate_uuid()
{
UUID uuid;
RPC_CSTR uuid_str;
std::string uuid_out;
if (UuidCreate(&uuid) != RPC_S_OK)
std::cout << "couldn't create uuid\nError code" << GetLastError() << std::endl;
if (UuidToStringA(&uuid, &uuid_str) != RPC_S_OK)
std::cout << "couldn't convert uuid to string\nError code" << GetLastError() << std::endl;
uuid_out = (char*)uuid_str;
RpcStringFreeA(&uuid_str);
return uuid_out;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.