std :: lower_bound() ve std :: upper_bound() sözdiziminde (iyi, tür dönüştürme) gerçekten bir tutarsızlık gibi görünen şeyleri gördünüz ve herhangi birinin açıklamasını isteyip istemediğini merak ettiniz mi? Yorumlara göre, satır 2, satır 1'e olan belirgin benzerliğine rağmen derlenmeyecektir; Eğer 3. hatta gösterilen formu kullanmaya gerek (en azından gcc üzerinde 4.7.3/ubuntu 64 bit - ben oynamak için bir tek bu var)upper_bound ve lower_bound tutarsız değer gereksinimleri
#include <set>
#include <algorithm>
using namespace std;
class MyInt {
private:
int val;
public:
MyInt(int _val): val(_val) {}
bool operator<(const MyInt& other) const {return val < other.val;}
};
int main() {
set<MyInt> s;
s.insert(1); // demonstrate implicit conversion works
s.insert(MyInt(2));
s.insert(3); // one last one for the road
set<MyInt>::iterator itL = lower_bound(s.begin(), s.end(), 2); //LINE 1
// the line below will NOT compile
set<MyInt>::iterator itU = upper_bound(s.begin(), s.end(), 2); //LINE 2
// the line below WILL compile
set<MyInt>::iterator itU2 = upper_bound(s.begin(), s.end(), MyInt(2)); // LINE 3
return 0;
}
G ++ 4.8.4 ile aynı davranış burada. Bu kesinlikle bir g ++ hatası. –