Are pointers allowed as keys in ordered STL containers?(是否允许指针作为有序 STL 容器中的键?)
问题描述
this other question询问如何比较指针应该如何解释 wrt C++ Std.
There's this other question asking about how comparing pointers is supposed to be interpreted wrt the C++ Std.
所以我想知道 C++ 标准对在有序标准库 (STL) 容器中使用指针作为键有什么看法——即是否允许拥有
So I was wondering what the C++ Std has to say about using pointers as keys in ordered standard library (STL) containers -- i.e. is one allowed to have
std::map
这是由于 std::less
或 内置运算符 <
的规范造成的吗?
and is this due to the specification of std::less
or builtin operator <
?
推荐答案
是的,因为它使用了 std::less
,即使 也需要产生全序.
没有.(<
将被允许将来自不同序列的不同指针视为相同,如果您插入来自不同序列的指针,这将导致 map
等的奇怪行为.
Yes, because it uses std::less
, which is required to result in a total order even if <
doesn't. (<
would be allowed to treat different pointers from distinct sequences as equal, which would result in an odd behaviour of map
etc if you insert pointers from different sequences).
这篇关于是否允许指针作为有序 STL 容器中的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否允许指针作为有序 STL 容器中的键?
基础教程推荐
- end() 能否成为 stl 容器的昂贵操作 2022-10-23
- C++按值调用 1970-01-01
- 明确指定任何或所有枚举数的整数值 1970-01-01
- C++定义类对象 1970-01-01
- C语言访问数组元素 1970-01-01
- 使用scanf()读取字符串 1970-01-01
- 分别使用%o和%x以八进制或十六进制格式显示整 1970-01-01
- C++ #define 1970-01-01
- C++输入/输出运算符重载 1970-01-01
- 初始化变量和赋值运算符 1970-01-01