Re: std::set<> and predicates
On 5 Ott, 12:36, Rune Allnor <all...@tele.ntnu.no> wrote:
On 5 Okt, 06:29, "Alf P. Steinbach" <al...@start.no> wrote:
* Rune Allnor:
Are there any obvious pitfalls I have missed here?
Make sure that for any values A, B and C your predicate ensures that A<B && B<C
implies A<C, and that A<C implies !(C<A).
As I said, the predicate works as expected when tested
in isolation outside the std::set<>.
How do you tell that it is not working when used by set?
The following code, adapted from your OP, proves that it works:
-------
#include <iostream>
#include <set>
class predicate : public std::less<size_t> {};
int main()
{
predicate p;
std::set<size_t> s(p);
s.insert(20);
s.insert(10);
std::set<size_t>::iterator i = s.begin();
std::set<size_t>::iterator j = i;
j++;
std::cout << "*i == " << *i << std::endl;
std::cout << "*j == " << *j << std::endl;
if(p(*i,*j)) {
std::cout << "true" << std::endl;
} else {
std::cout << "false" << std::endl;
}
}
-------
Output:
-------
*i == 10
*j == 20
true
-------
Where exactly are you stuck in?
--
Francesco S. Carta, http://fscode.altervista.org
First time here? Read the 'Welcome' and the 'FAQ'
Welcome: http://www.slack.net/~shiva/welcome.txt
C++ FAQ: http://www.parashift.com/c++-faq-lite