gpt4 book ai didi

c++ - 这个 C++ 示例可移植吗?

转载 作者:可可西里 更新时间:2023-11-01 17:25:56 27 4
gpt4 key购买 nike

正在关注 this问题,我试着复制粘贴找到的例子here在 VS2010 中:

#include <algorithm>
#include <vector>
#include <iostream>

struct S
{
int number;
char name;

S ( int number, char name )
: number ( number ), name ( name )
{}

// only the number is relevant with this comparison
bool operator< ( const S& s ) const
{
return number < s.number;
}
};

struct Comp
{
bool operator() ( const S& s, int i )
{
return s.number < i;
}

bool operator() ( int i, const S& s )
{
return i < s.number;
}
};

int main()
{
std::vector<S> vec = { {1,'A'}, {2,'B'}, {2,'C'}, {2,'D'}, {3,'F'}, {4,'G'} }; //this syntax won't compile in VS2010, so you can leave an empty vector here

auto p = std::equal_range(vec.begin(),vec.end(),2,Comp());

for ( auto i = p.first; i != p.second; ++i )
std::cout << i->name << ' ';
}

这将在 Release模式下编译正常,但在 Debug模式下,它将无法编译。原因是在 Debug模式下,实现将检查迭代器范围是否已经排序,使用给定的谓词:

template<class _FwdIt,
class _Pr> inline
void _Debug_order2(_FwdIt _First, _FwdIt _Last, _Pr _Pred,
_Dbfile_t _File, _Dbline_t _Line, forward_iterator_tag)
{ // test if range is ordered by predicate, forward iterators
for (_FwdIt _Next = _First; _First != _Last && ++_Next != _Last; ++_First)
if (_DEBUG_LT_PRED(_Pred, *_Next, *_First))
_DEBUG_ERROR2("sequence not ordered", _File, _Line);
}

这最终会调用:

template<class _Pr, class _Ty1, class _Ty2> inline
bool _Debug_lt_pred(_Pr _Pred,
const _Ty1& _Left, const _Ty2& _Right,
_Dbfile_t _File, _Dbline_t _Line)
{ // test if _Pred(_Left, _Right) and _Pred is strict weak ordering
if (!_Pred(_Left, _Right))
return (false);
else if (_Pred(_Right, _Left))
_DEBUG_ERROR2("invalid operator<", _File, _Line);
return (true);
}

除了在我的例子中,没有operator() 可以接受左右“S”参数。那么,Visual 实现中是否存在错误?或者原始示例不应该是可移植的?我想我可以通过提供第三个 operator() 重载来让它工作,但它似乎应该在没有

的情况下工作

谢谢

最佳答案

标准中没有任何内容要求比较器可以用范围中的两个对象调用。所以这是 VS 2010 使用的标准库中的错误。

以下是所有相关要求(引用 C++11):

[下限]§1+2:

1 Requires: The elements e of [first,last) shall be partitioned with respect to the expression ... comp(e, value).

2 Returns: The furthermost iterator i in the range [first,last] such that for any iterator j in the range [first,i) the following corresponding conditions hold: ... comp(*j, value) != false.

[上界]§1+2:

1 Requires: The elements e of [first,last) shall be partitioned with respect to the expression ... !comp(value, e).

2 Returns: The furthermost iterator i in the range [first,last] such that for any iterator j in the range [first,i) the following corresponding conditions hold: ... comp(value, *j) == false.

[等于.range]§1+2:

1 Requires: The elements e of [first,last) shall be partitioned with respect to the expressions ... comp(e, value) and !comp(value, e). Also, for all elements e of [first, last), ... comp(e, value) shall imply !comp(value, e).

2 Returns:

...

make_pair(lower_bound(first, last, value, comp),
upper_bound(first, last, value, comp))

(省略号表示非比较器版本)。

关于c++ - 这个 C++ 示例可移植吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18767829/

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