Set of STL applications

Source: Internet
Author: User

Previously, when solving an algorithm question, we applied it to set and made some knowledge of 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:
Class MyType
{
Public:
Int a, 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 <(const MyType & myType) const
{
Return a <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:



The code used for testing is as follows:
# Include <iostream>
# Include <set>
Using namespace std;
Class MyType
{
Public:
Int a, B, c;
MyType (int a, int B, int c): a (a), B (B), c (c ){}
Bool operator <(const MyType & myType) const
{
Return a <myType.;
}
};
Int main ()
{
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;
Return 0;
}
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 <(const MyType & myType) const
{
Return a <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 namespace std;
Class MyType
{
Public:
Int a, B, c;
MyType (int a, int B, int c): a (a), B (B), c (c ){}
Bool operator <(const MyType & myType) const
{
Return a <myType.? True: (B <myType. B? True: c <myType. c );
}
};
Int main ()
{
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;
Return 0;
}

 

From Martin's Tips
 

Related Article

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.