Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of UUID generation using Boost in C++

I want to generate just random UUID's, as it is just important for instances in my program to have unique identifiers. I looked into Boost UUID, but I can't manage to generate the UUID because I don't understand which class and method to use.

I would appreciate if someone could give me any example of how to achieve this.

like image 208
Nikola Avatar asked Jul 14 '10 15:07

Nikola


People also ask

Is boost UUID unique?

"The UUID is extremely likely to be unique." because the requirement of a random number generator is not to generate unique numbers (but with a good random number generator your extremely likely will be EXTREMELY likely).

What is UUID in c++?

A UUID, or Universally unique identifier, is intended to uniquely identify information in a distributed environment without significant central coordination. It can be used to tag objects with very short lifetimes, or to reliably identify very persistent objects across a network. UUIDs have many applications.

What does UUID look like?

Format. In its canonical textual representation, the 16 octets of a UUID are represented as 32 hexadecimal (base-16) digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 hexadecimal characters and 4 hyphens). For example: 123e4567-e89b-12d3-a456-426614174000.


1 Answers

A basic example:

#include <boost/uuid/uuid.hpp>            // uuid class #include <boost/uuid/uuid_generators.hpp> // generators #include <boost/uuid/uuid_io.hpp>         // streaming operators etc.  int main() {     boost::uuids::uuid uuid = boost::uuids::random_generator()();     std::cout << uuid << std::endl; } 

Example output:

7feb24af-fc38-44de-bc38-04defc3804de

like image 181
Georg Fritzsche Avatar answered Sep 18 '22 16:09

Georg Fritzsche