gpt4 book ai didi

C++98 诉 C++11 std::set::insert 规范

转载 作者:IT老高 更新时间:2023-10-28 22:59:26 25 4
gpt4 key购买 nike

迭代器的含义作为位置提示传递给 std::set::insert(iterator position, const value_type& val)std::multiset::insert(iterator position , const value_type& val) 在 C++98 和 C++ 11 之间发生变化。在编译时是否有一种简单的方法来检测正在使用的代码并使用不同的代码?

对 C++11 的一般检查似乎不是一个好主意( 12 ),我没有看到合适的 Boost.Config宏。

具体来说,documentation对于 C++98 说:

The function optimizes its insertion time if position points to theelement that will precede the inserted element.

对于 C++11,它说:

The function optimizes its insertion time if position points to theelement that will follow the inserted element (or to the end, if itwould be the last).

这很重要,因为提示会影响插入调用的复杂性。如果提示正确,则复杂度只是一个摊销常数。但如果不是,它的大小是对数的。

更新

正如下面 JerryCoffin 很好地描述的那样,C++98 规范本质上是一个错字。

最佳答案

这是 defect report 的主题, LWG issue #233 , 和 n1780 .

如上所述,这显然是编辑 C++98 标准时的一个简单错误。

Stepanov 的原始实现根据当前规范工作,据我所知,大多数(所有?)实现都做了同样的事情(尽管有一个在提示之后看起来既在 之前又在位置,因此如果您指定其中一个,您将获得最佳行为)。

这也可以暗示插入将在集合的开头,这在以前的规范下是不可能的。

关于C++98 诉 C++11 std::set::insert 规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25250940/

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