Objective STL Study Notes for C ++ Item21

Source: Internet
Author: User

Now, I want to post an English post. I really don't want to translate it, and I don't feel like it is in place:

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 particle 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 functions that must determine whether two values are the same. yet they do it in different ways. find's definition of "the same" isEquality, Which is based on operator =. set: insert's definition of "the same" isEquivalence, 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, let's end with the discussion of equivalence and equivalence first. Let's introduce the next topic. Suppose we create a set as below:

<, less_equal<> > s; 

Insert 10 to it:

s.insert();

Then insert 10 to it. We know that the elements in the set are not repeated, so the program will determine whether 10 is already in the current set. For convenience, we call 10 of the First insert as 10A, and the second insert as 10B. The set judgment process is like 10A and 10B. The set definition is the same as what we mentioned earlier.Equivalence.

How does less_equal in the program determine it? According to the above derivation, a reasonable derivation, set may be determined as follows:

!(10A <= 10B) && !(10B <= 10A)      

Of course, the result of the above determination is :! (True )&&! (True), equivalent to false & false, so the final result will be false. That is to say, the judgment result tells us that 10A and 10B are not equivalent! Then 10B, that is, 10, will be inserted into the set again. oh, no!

Well, this is just a joke. set won't be so weak, and there won't be repeated elements in set. The friends are sure to be confused. This means that it is useless for a long time. set won't use the order like less_equal, because equal is not allowed at all. Isn't that awkward!

Don't worry, set does not repeat this feature, so if it is a multiset, according to this idea, this operation will be successful, it will be considered that two different elements are inserted into the multiset, let's assume that we need to perform the related operations (it identifies a rangeEquivalentValues), the problem is big!

Haha, I made a joke with you. STL has been refined. How can this silly problem exist! SGI is the most authoritative question. We can check it out at SGI to see what is going on! There is really a keyword: Strict Weak Ordering. Well, let's see how to solve this problem:

, Less_equal <> );

This code is compiled, but an assert error is reported during execution! I have not figured out the principles. Continue to study!

Well, I want to tell you that when implementing a less_equal-like function like myself, you must be careful and never have such a bug!

Thank you for reading this article. I hope it will help you!

Published by Windows Live Writer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.