Bonjour, je me pose une question sur binary_search, en effet elle ne liste pas tous les elements de mon vector afin de les comparer, jugez par ce code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <algorithm>
#include <vector>
 
using namespace std;
 
 
class CFriend
{
  public:
 
  int _x;
 
  bool operator<(const CFriend& c) const
  {
	  cout << "Value :  " << _x << " | " << c._x << endl;
	return false;
  }
};
 
bool compare(const CFriend &ref1, const CFriend &ref2)
{
	cout << "Value : " << ref1._x << " | " << ref2._x << endl;
	return false;
}
 
int main()
{
        CFriend *c1 = new CFriend();
	CFriend *c2 = new CFriend();
	CFriend *c3 = new CFriend();
 
        c1->_x = 69;
	c2->_x = 13;
	c3->_x = 45;
 
	CFriend *search = new CFriend();
	search->_x = 69;
 
       vector<CFriend> vc;
	vc.push_back(*c1);
	vc.push_back(*c2);
	vc.push_back(*c3);
 
	if(binary_search(vc.begin(), vc.end(), *search)) cout << "Found !\n";
	// 	if(binary_search(vc.begin(), vc.end(), *search, compare)) cout << "Found !\n";
	else cout << "Not found !\n";
 
	return 0;
}
Qui me retourne :
Value : 13 | 69
Value : 69 | 69
Value : 69 | 69
Donc il manque un objet dans la liste, celui avec _x == 45...
Pourquoi ?

Une autre question : pourquoi devoir redéfinir operator< et pas operator== ?

Merci pour vos réponses.