C ++ STL study notes seven set containers

Source: Internet
Author: User

/*
*
**************************************** ****
* Basic description of the Set set container:
**************************************** ****
*
* The set collection container uses the balanced binary tree of the RB-tree to retrieve the data structure of the tree. duplicate key values cannot be inserted.
* The key value of each subtree node is greater than the key value of all the nodes in the left subtree, and less than the key value of all the nodes in the right subtree.
* Balanced processing is required during the insertion process, but the retrieval process is highly efficient.
*
* Supports element insertion, deletion, and retrieval.
* Unique sorted associative container simple associative container
*
* To use set, you must use a macro statement # include <set>
*
**************************************** **************************************** ******
*
* Create a set object:
* 1.set< int>;
* 2.set( const key_compare & Comp) // specify a comparison function object comp to create a set object
*
* Srtuct strless {
* Bool operator () (const char * S1, const char * S2) const {
* Return strcmp (S1, S2) <0;
*}
*};
* Set <const char *, strless> S (strless ());
*
* 3.set( const set &); // set <int> B ();
* 4.set( first, last); // set <char> C (A. Begin (), A. End ())
* 5.set( first, last, const key_compare & Comp );
**************************************** **************************************** ******
*
* Element insertion // repeated key values cannot be inserted
* Pair <iterator, bool> insert (const value_type & V); // It can be used to determine whether to insert elements repeatedly. This can be used for special information.
* Iterator insert (iterator POs, const value_type & V );
* Void insert (first, last );
*
**************************************** **************************************** ******
*
* Deletion of Elements
* Void erase (iterator POS );
* Size_type erase (const key_type & K); // delete an element equal to key value K
* Void erase (first, last); // deletes the elements in the [first, last) interval.
* Void clear ();
*
**************************************** **************************************** ******
*
* Access and search
*
* Iterator begin (); iterator end ();
* Reverse_iterator rbegin (); reverse_iterator rend ();
*
* Iterator find (const key_type & K) const;
*
* Other common functions
* Bool empty () const;
* Size_type size () const;
* Void swap ();
*
* // The following three functions have not been found as an example, so we will not describe them.
* Iterator lower_bound (); iterator upper_bound (); pair <iterator, iterator> interval _range (); // upper bound, next interval, and definite interval
*
*
*
**************************************** ****
** Cumirror ** tongjinooo@163.com ****
**************************************** ****
*
*/

# Include <set>
# Include <iostream>

// Insert Custom Data
Struct student {
Char name [20];
Int age;
Char City [20];
Char phone [20];
Bool operator () (const student & A, const student & B) const {
Return strcmp (A. Name, B. Name) <0;
}
};

Int main (){
Using namespace STD;
Set <int>;
A. insert (10 );
A. insert (19 );
A. insert (8 );
A. insert (1, 102 );
A. insert (1 );
Pair <set <int >:: iterator, bool> P = A. insert (18 );
If (P. Second ){
Cout <"the new element inserted is:" <* (p. First) <Endl;
}
Else {
Cout <"This element already exists and is inserted repeatedly" <Endl;
}

Set <int>: iterator I = A. Find (8 );
// * I = 250; // There are discrepancies with the 237 page of Hou Jie STL source code analysis
Cout <* I <Endl; // the original article is: it is not allowed to change elements through the iterator.
// A. insert (251); // but there is no problem in compiling and running vc6.0, but the sorting in the set is incorrect.
// Even if you re-insert 251, the error is not corrected because it does not violate the red/black tree standard.
// Is it because of STL version problems: an error is returned after stlport is changed.
// The STL inventory in vc6.0 is faulty.
Cout <"element access:" <Endl;
For (set <int>: iterator J = A. Begin (); J! = A. End (); j ++ ){
Cout <* j <Endl;
}

// What is container? I think it is about tolerance for user-defined data. Therefore, write the following example for test and verification.
// You can also try to use age as the judgment condition.
Student stu1 = {"Tong Jin", 23, "Wuhan", "XXX "};
Student stu2 = {"boss", 28, "Wuhan", "XXX"}; // boss, you are 5 years old, haha
Student stu3 = {"dumplings", 23, "Wuhan", "XXX "};
Set <student, student> B (student ());
B. insert (stu1 );
B. insert (stu2 );
B. insert (stu3 );

Student stu4 = {"dumplings 123", 88, "Fuzhou", "XXX "};
Pair <set <student, student>: iterator, bool> query = B. insert (stu4 );
If (query. Second = false) // query. First returns the iterator for inserting elements. query. Second indicates whether the insertion is successful. True indicates that the insertion fails.
Cout <"Duplicate element insertion will fail" <Endl;
Cout <query. First-> name <Endl;
For (set <student, student >:: iterator K = B. Begin (); k! = B. End (); k ++ ){
Cout <k-> name <Endl;
}

// Student test1 = {"boss", 23, "Wuhan", "XXX"}; // This element can be found
// Set <student, student>: iterator v = B. Find (test1 );
// Student Test2 = {"", 23, "Wuhan", "XXX"}; // cannot be found
// Set <student, student>: iterator v = B. Find (Test2 );
Student test3 = {"boss", 99, "", ""}; // you can find it, presumably:
Set <student, student >:: iterator v = B. Find (test3); // 1. The key value is set according to the settings in the key_compare () function.
Cout <v-> age <Endl; // 2. The key value is the first element in the defined data type.
// Conclusion: speculative 1 is correct.
// You can modify the operator () function for verification, or you can refer to the examples in the subsequent Multiset.
Return 0;
}

 

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.