2024年4月3日发(作者:)
16 std::stringstream hexstream;
17 hexstream << std::hex << rc;
18 auto hex = ();
19 ss << (() < 2 ? '0' + hex : hex);
20 }
21 return ();
22 }
The C++ standard libary has come a long way in the last ten years. It contains a lot of useful functions that make stuff easy that used to be a
chore in C++ compared to other languages. However, there is still a gap between what the STL provides compared to other modern
languages.
One such case is UUID/GUID creation. In languages like Python and Java, to make a GUID, all you need to do is import a module and you
are good to go. In C++, this will still be relegated to an external framework like the Windows API or QT. There are even some
smaller libraries specifically targeting this functionality. In most cases, this is still the best place to go. These methods are battle-tested:
they'll be fast and robust.
If you want something more portable that doesn't rely on any external linking, you'll have to write it yourself. Luckily, the isn't particularly
complex. If you can generate random hex characters, you can make a GUID easily.
The STL in C++11 now includes
std::random
which makes this a lot easier. Before it, you would still probably need to go to an external library
to get random values. In combination with std::hex, almost all of the hard parts of this problem can be solved with stuff in the standard
library.


发布评论