# C++ Effective STL study notes Item21

**WhatsUp Gold**.

**Not end users.**

Well, to put some English, really don't want translated, but also feel not so in Translation:

The STL is awash in comparisons of objects to see if they have the same value. For example, when you ask find to locate the first object in a range with a particular value, find has to be able to compare two objects to see if the value of one is the same as the value of the other. Similarly, when you attempt to insert a new element into a set, set::insert has to be able to determine whether that element’s value is already in the set.

The find algorithm and set’s insert member function are representative of many functions that must determine whether two values are the same. Yet they do it in different ways. find’s definition of “the same” is **equality**, which is based on operator==. set::insert’s definition of “the same” is **equivalence**, which is usually based on operator<.

Operationally, the notion of equality is based on operator==. If the expression “x == y” returns true, x and y have equal values, otherwise they don’t.

x and y have equivalent values with respect to operator<if the following expression is true:

!(x <y) && !(y <x)

Well, on the equivalence and equivalence of discussion, we first so far. We introduce a topic, suppose we now create a set:

set<int, less_equal<int> > s; // s is sorted by “<=”

To insert one of 10:

`s.insert(10);`

And then to insert 10, as we know, the elements in the set is not repeated, so the program will judge whether 10 has been in the current set, in order to facilitate the description, we put the first into the 10 called 10A, the second is called 10B. The judging process set is whether 10A and 10B are the same, the definition of set is the same as we mentioned before**equivalence**.

Then the program of less_equal is how to judge? According to the above derivation, a reasonable deduction, set might be judged:

!(10A <= 10B) && !(10B <= 10A) // test 10A and 10B for equivalence

Of course it's judged result is: &! (true); &! (true), equivalent to the false & & false, so the final results will be the false, that is to say, decision results tell us, 10A and 10B is not equivalent! Then 10B, namely 10 will be once again into set, oh, no！

Well, this is just a joke, set can't weak to the point, is not the existence of repetitive elements in set. Friends must feel confused, this long hairy ah, set does not use less_equal in this order, because it does not allow the equal, this is not you find difficult.！

Don't worry, set has not repeat the characteristics, so that if a multiset, according to this idea, this operation will be successful, that is inserted into the two different elements into multiset, we need to make assumptions related to the operation of equal_range in the back of the (it identifies a range of **equivalent** values), the problem is big the！

Ha ha, and you open a big joke, STL is after thoroughly tempered, how could there be such silly questions! About the problem of SGI is the most authoritative, we can go to SGI check, to see what is going on! Keywords: really, Strict Weak Ordering, good let us have a look how you solved the problem:

See, the somebody else is to avoid <= comparison, so how to avoid? I think that means that at present cannot avoid, not this way, look at the code.:

multiset<int, less_equal<int> > s; s.insert(10); s.insert(10);

This code through the compiler, but implementation will report to assert error! I don't know the principle, continue to study it.！

Well, around such a circle, I just want to tell you, in time to achieve a similar less_equal functors themselves, must be careful, do not have such bug！

Thank you for reading, I hope to help you！

Published by Windows Live Writer.

**Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**.

**Free Download**

Posted by Carey at November 13, 2013 - 9:02 AM