在c ++中生成唯一ID [英] Generating a Unique ID in c++

查看:184
本文介绍了在c ++中生成唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,从两个(或更多个)短整型中生成唯一ID的最佳方法是什么?我试图在图中唯一标识顶点。顶点包含两到四个短整数作为数据,理想情况下,ID将是某种类型的散列。喜欢便携性和唯一性超过速度或轻松。

What is the best way to generate a Unique ID from two (or more) short ints in C++? I am trying to uniquely identify vertices in a graph. The vertices contain two to four short ints as data, and ideally the ID would be some kind of a hash of them. Prefer portability and uniqueness over speed or ease.

这里有很多很棒的答案,我会尝试他们今晚看看什么适合我的问题最好的。关于我在做什么更多的话。

There are a lot of great answers here, I will be trying them all tonight to see what fits my problem the best. A few more words on what I'm doing.

图表是音频文件中的示例集合。我使用图形作为马尔科夫链从旧文件生成一个新的音频文件。由于每个顶点存储几个样本并指向另一个样本,并且样本都是短整数,因此从数据生成ID似乎是很自然的。将它们组合成一个长长的声音听起来不错,但也许只是一个简单的0 1 2 3 generateID 是我需要的。不确定需要多少空间来保证唯一性,如果每个顶点存储2个16位样本,有2 ^ 32种可能的组合正确吗?所以如果每个顶点存储4个样本,有2 ^ 64种可能的组合?

The graph is a collection of samples from an audio file. I use the graph as a Markov Chain to generate a new audio file from the old file. Since each vertex stores a few samples and points to another sample, and the samples are all short ints, it seemed natural to generate an ID from the data. Combining them into a long long sounds good, but maybe something as simple as just a 0 1 2 3 generateID is all I need. not sure how much space is necessary to guarantee uniqueness, if each vertex stores 2 16 bit samples, there are 2^32 possible combinations correct? and so if each vertex stores 4 samples, there are 2^64 possible combinations?

图书馆和平台特定解决方案与此问题无关。我不希望任何人谁可能编译我的程序,必须下载额外的库或更改代码,以适应自己的操作系统。

Library and platform specific solutions not really relevant to this question. I don't want anyone else who might compile my program to have to download additional libraries or change the code to suit their OS.

推荐答案

一个简单的解决方案是使用64位整数,其中低16位是第一个顶点坐标,第二,等等。这将是所有的顶点是唯一的,虽然不是很紧凑。

A simple solution is to use a 64 bit integer where the lower 16 bits is the first vertex coordinate, next 16 bits is the second, and so on. This will be unique for all your vertices, though not very compact.

这里有一些half-assed代码来做到这一点。希望我得到的演员是正确的。

So here's some half-assed code to do this. Hopefully I got the casts right.

uint64_t generateId( uint16_t v1, uint16_t v2, uint16_t v3, uint16_t v4)
{ 
   uint64_t id;
   id = v1 | (((uint64_t)v2) << 16) | (((uint64_t)v3) << 32) | (((uint64_t)v4) << 48);
   return id;
}

可选地,这可以通过联合完成(Leon Timmermans的好主意,见评论)。很清楚这种方式:

Optionally this could be done with a union (great idea from Leon Timmermans, see comment). Very clean this way:

struct vertex
{
    uint16_t v1;
    uint16_t v2;
    uint16_t v3;
    uint16_t v4;
};

union vertexWithId
{
    vertex v;
    uint64_t id;
};

int main()
{
    vertexWithId vWithId;
    // Setup your vertices
    vWithId.v.v1 = 2;
    vWithId.v.v2 = 5;

    // Your id is automatically setup for you!
    std::cout << "Id is " << vWithId.id << std::endl;
    return 0;
}

这篇关于在c ++中生成唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆