gpt4 book ai didi

c++ - uint64_t 中的唯一值在 int64_t 中是否也是唯一的

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:01:07 25 4
gpt4 key购买 nike

给定一个唯一的(std::uint64_t 类型)整数 vector ,如果我将(std::uint64_t 类型)的 vector 转换为( std::int64_t type) integers,能保证唯一吗?

像这样类型转换

std::vector<std::uint64_t> unsignedVec;
std::vector<std::int64_t> signedVec( unsignedVec.begin(), unsignedVec.end() );

最佳答案

will it be guaranteed to be unique?

形式上它是实现定义的,但在任何合理的平台上它应该是唯一的。

(特别是考虑到 int64_t has to be 2 的补码,如果提供的话。)

从 C++20 开始,它必须是唯一的。


https://en.cppreference.com/w/cpp/language/implicit_conversion#Numeric_conversions

If the destination type is signed, the value does not change if the source integer can be represented in the destination type. [Otherwise the result is implementation-defined (until C++20)] [the unique value of the destination type equal to the source value modulo 2n where n is the number of bits used to represent the destination type. (since C++20)].

关于c++ - uint64_t 中的唯一值在 int64_t 中是否也是唯一的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56157240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com