Set of STL applications

Source: Internet
Author: User

PreviouslyAlgorithmWhen applying the question to the set, I made some knowledge about the STL container class. In my impression, set is a set with no repeated elements, and in fact it is like this. No matter from msdn or anywhere else, we will be told that the Set elements cannot be repeated. On the contrary, as long as the elements are not repeated, they can be smoothly put into the set. It seems completely clear, but if you think about it carefully, you will find that the elements that do not repeat can be put into a set, but what elements are not repeated? Or, what elements are different from each other? For built-in data types, that is, the legendary primary data type, such as int, double, unsigned, or even string, the "same" of these elements is very easy to understand. What about custom types? What data is the same? What kind of data is different?

I used to learn STL before. If you want to add a custom type to the set, you need to reload the "<" symbol, the reason is that set is an ordered set. The set is sorted in the ascending order by default according to the size of the <"comparison. Assume that the following types are designed:

 
ClassMytype
 
{
Public:
 
IntA, B, C;
 
}

This is, in order to allow the mytype to be smoothly put into the set, I must reload "<". Now the question is, how to reload it? This type has three data members. Can I arrange the data by the size of a? If the phase is equal, it will be arranged by the size of B or C? If the implementation is arranged according to the size of a, the overload function is as follows:

Bool Operator<(ConstMytype & mytype)Const
 
{
 
ReturnA <mytype.;
 
}

It looks simple and clear, but is it true? If I declare a set and put mytype (, 3), mytype (, 4) in it, can I succeed? After an experiment, the results are as follows:

Used for testingCodeYes:

 
# Include <iostream>
 
# Include <set>
 
Using NamespaceSTD;
 
 
ClassMytype
 
{
 
Public:
 
IntA, B, C;
Mytype (IntA,IntB,IntC): A (a), B (B), C (c ){}
 
Bool Operator<(ConstMytype & mytype)Const
 
{
ReturnA <mytype.;
 
}
 
};
 
IntMain ()
 
{
Set <mytype> Se;
 
Mytype type1 (1, 2, 3 );
 
Mytype type2 (1, 2, 4 );
 
Se. insert (type1 );
 
Se. insert (type2 );
 
 
Cout <"The set size:"<Se. Size () <Endl;
 
Cout <"Elements in the set as follows:"<Endl;
 
For(Set <mytype >:: iterator it = Se. Begin (); it! = Se. End (); It ++)
{
 
Cout <"("<It-> A <","<It-> B <","<It-> C <")";
 
}
 
Cout <Endl;
Return0;
 
}

Obviously, after I put mytype (, 3) in the set, I cannot put mytype (, 4) in it. But why? These two objects look really different! When the STL comparison is the same, isn't it necessary to compare every data member? From the examples above, we can see that this is indeed not the case. STL will not perform any comparison automatically. It only works for the actions you have described. When "<" is reloaded, If you specify only a part of a large number of data members, if the parts are the same, set is considered to be the same element. As shown above, only a is compared during the reload, but it does not indicate whether to compare the remaining elements if AIS the same. In this way, set considers that mytype (, 3) and mytype (, 4) are the same. To make the set correctly compare the size, the comparison of all data members must be described for the custom type. For example, the "<" overload in the above example should be written as follows:

Bool Operator<(ConstMytype & mytype)Const
 
{
 
ReturnA <mytype.?True:( B <mytype. B?True: C <mytype. C );
 
}

In this way, the comparative relationship between each data member is fully illustrated, and set can work normally. Or mytype (, 3) and mytype (, 4). The running result is as follows:

Run the following code:

 
# Include <iostream>
 
# Include <set>
 
Using NamespaceSTD;
 
 
ClassMytype
 
{
 
Public:
 
IntA, B, C;
Mytype (IntA,IntB,IntC): A (a), B (B), C (c ){}
 
Bool Operator<(ConstMytype & mytype)Const
 
{
ReturnA <mytype.?True:( B <mytype. B?True: C <mytype. C );
 
}
 
};
 
IntMain ()
 
{
Set <mytype> Se;
 
Mytype type1 (1, 2, 3 );
 
Mytype type2 (1, 2, 4 );
 
Se. insert (type1 );
 
Se. insert (type2 );
 
 
Cout <"The set size:"<Se. Size () <Endl;
 
Cout <"Elements in the set as follows:"<Endl;
 
For(Set <mytype >:: iterator it = Se. Begin (); it! = Se. End (); It ++)
{
 
Cout <"("<It-> A <","<It-> B <","<It-> C <")";
 
}
 
Cout <Endl;
Return0;
 
}

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.