c++ 通过一对长长的在套里找到安全吗?

ahy6op9u  于 2023-05-30  发布在  其他
关注(0)|答案(2)|浏览(91)

我有以下代码:

std::set<std::pair<int, int>> my_set;

long long x = (some value);
long long y = (some value);

// should be true if x or y exceed the limits of int
bool z = my_set.find({x, y}) == my_set.end();

它的工作没有任何错误,似乎工作正确。但是,我想知道它是否“安全”,因为我将一对long long传递给find函数。
谢谢!

pcww981p

pcww981p1#

这是不安全的,因为实际比较的值可能不是您所期望的。
两个long long值将被转换为int,然后find将查找包含这两个转换后的int值的pair。由于C++20,因此可以保证此转换产生由int表示的唯一值,该值等于原始值模2的int宽度的幂。
如果xy的值落在int的范围之外,这意味着z可能会变成false,即使在集合中可能没有一对xy的值(数学上)等于集合中的对的值。
但是,除此之外,没有什么错。从long long转换为int后的搜索将完全按预期工作。

edqdpe6u

edqdpe6u2#

您没有向函数传递一对long long。由于没有指定传递给std::set::find的键的类型,编译器使用构造函数,该构造函数接受对const Key对象的左值引用,即在这种情况下为std::pair<int, int>
这伴随着您从可能超过int范围的值创建std::pair<int, int>的所有缺点。

std::set<std::pair<int, int>> my_set;

my_set.emplace(0, 0);

long long x = 0x100000000;
long long y = 0;

// should be false if x or y exceed the limits of int
bool z = my_set.find({ x, y }) == my_set.end();
std::cout << std::boolalpha << z << '\n'; // prints false

相关问题