C ++ STL learning notes 9 Map ing container

Source: Internet
Author: User

/*
*
**************************************** ****
* Basic descriptions of map ing containers:
**************************************** ****
*
* Map ing container: the data structure of the container is managed by the red/black tree, And the inserted element key values cannot be duplicated.
* All elements of map are pair. The first element is a key and cannot be modified. The second element is a real value and can be modified.
*
* Similar to map and list, after insert or erase is used, all previous iterators remain valid after the operation is complete.
* []: Not only can the element ing data be accessed using an array of key values, but also can be used to add elements of the map container // examples in main
*
* Sorted associative container pair associative container unique associative container
*
* To use map, you must use a macro statement # include <map>
*
**************************************** **************************************** ******
*
* Create a map object:
* 1.map< char, Int, greater <char> A; // The element key value type is Char, The ing data type is int, and the key value comparison function object is greater <char>
* 2.map( const key_compare & Comp) // specify a comparison function object comp to create a map object
* 3.map( const map &); // Map <int, char *> B (a); // use the default Key Value Comparison function less <int>
* 4.map( first, last );
* 5.map( first, last, const key_compare & Comp );
*
* // Example:
* Pair <const int, char> P1 (1, 'A ');
* Pair <const int, char> P2 (2, 'B ');
* Pair <const int, char> P3 (3, 'C ');
* Pair <const int, char> P4 (4, 'D ');
* Pair <const int, char> pairarray [] = {P1, P2, P3, P4 };
* Map <const int, char> M4 (pairarray, pairarray + 5 );
* Map <const int, char> m3 (M4 );
* Map <const int, Char, greater <const int> M5 (pairarray, pairarray + 5, greater <const int> ());
*
**************************************** **************************************** ******
*
* Element insertion
* // Typedef pair <const key, T> value_type;
* Pair <iterator, bool> insert (const value_type & V );
* 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 (); // It is not allowed to change elements through the iterator.
* Reverse_iterator rbegin (); reverse_iterator rend ();
*
* Iterator find (const key_type & K) const;
* Pair <iterator, iterator> pai_range (const key_type & K) const; // The returned pair object,
* // First is lower_bound (k); the first element location greater than or equal to K
* // Second is upper_bound (); the first element position greater than K
*
* Other common functions
* Bool empty () const;
* Size_type size () const;
* Void swap ();
*
* Iterator lower_bound (); iterator upper_bound (); pair <iterator, iterator> interval _range (); // upper bound, next interval, and definite interval
*
*
*
**************************************** ****
** Cumirror ** tongjinooo@163.com ****
**************************************** ****
*
*/

# Include <map>
# Include <string>
# Include <iostream>

// The basic operation is similar to set. Remember that all elements in map are pair.
// For user-defined classes, beginners may find it difficult to construct comparative functions. refer to the previous writing example.
// If the key value is set to int or char type, you do not need to construct a comparison function.

Struct student {
Char * Name;
Int age;
Char * city;
Char * phone;
};

Struct strcmp {
Bool operator () (const char * a, const char * B) const {
Return strcmp (a, B) <0;
}
};

Struct strcmpbig {
Bool operator () (const char * a, const char * B) const {
Return strcmp (a, B)> 0;
}
};

Int main (){
Using namespace STD;
// Use the [] Operator
Map <string, int> animal;
Animal [String ("fish")] = 12;
Animal [String ("dog")] = 10;
Animal [String ("cat")] = 5;
Cout <animal ["cat"] <Endl;

// The string class has a default comparison function, so the following statement can be executed. If it is converted to the char * type, the execution will fail.
// Iterator I points to the pair type
For (Map <string, int >:: iterator I = animal. Begin (); I! = Animal. End (); I ++ ){
Cout <"the number of" <I-> first <":" <I-> second <Endl;
}

Student s [] = {
{"Tong Jin", 23, "Wuhan", "XXX "},
{"Boss", 23, "Wuhan", "XXX "},
{"Dumplings", 23, "Wuhan", "XXX "}
};
Pair <int, student> P1 (4, s [0]);
Pair <int, student> P2 (2, s [1]);
Pair <int, student> P3 (3, S [2]);
Map <int, student>;
A. insert (P1 );
A. insert (P2 );
A. insert (P3 );
// Sort the output by key values 2, 3, and 4
For (Map <int, student >:: iterator J = A. Begin (); J! = A. End (); j ++ ){
Cout <"the name:" <j-> second. Name <"<" Age: "<j-> second. age <""
<"City:" <j-> second. City <"" <"Phone:" <j-> second. Phone <Endl;
}

// Think, this is arranged by key value. If I want to change and rearrange by name or age, what should I do?
 
Cout <"New Arrangement" <Endl;
Pair <const char *, student> q1 (s [0]. Name, s [0]);
Pair <const char *, student> Q2 (s [1]. Name, s [1]);
Pair <const char *, student> Q3 (s [2]. Name, s [2]);

Student teststu = {"AB", 23, "Wuhan", "XXX "};

// Why should we use the form of function objects instead of functions? This is related to the internal implementation mechanism of C ++.

Map <const char *, student, strcmp> B;
B. insert (Q1 );
B. insert (Q2 );
B. insert (Q3 );

// Test the insert function and observe the value of putting it back into the iterator. You can change the name to see if the insert position depends on the actual situation.
// Returns the iterator for inserting elements.
Pair <const char *, student> Q4 (teststu. Name, teststu );
Map <const char *, student, strcmp>: iterator test = B. insert (B. Begin () ++, Q4 );
Cout <Test-> second. Name <Endl;

For (Map <const char *, student, strcmp>: iterator K = B. Begin (); k! = B. End (); k ++ ){
Cout <"the name:" <k-> second. Name <"<" Age: "<k-> second. age <""
<"City:" <k-> second. City <"" <"Phone:" <k-> second. Phone <Endl;
}

// You can set the function object during the copy operation. Can the order change be implemented if the function object is changed?
// It is not implemented yet. Do you know it is feasible? Try again later

// Personal opinion: not feasible. Function objects compare keys. Keys cannot be changed if they are re-arranged,
// So the so-called re-arrangement is not just reverse order, but the reverse order output map already has such a function.

Cout <"re-arrange during copy" <Endl; // not implemented
Map <const char *, student, strcmp> C (B );
For (Map <const char *, student, strcmp/* strcmpbig */>: iterator M = C. Begin (); m! = C. End (); m ++ ){
Cout <"the name:" <m-> second. Name <"<" Age: "<m-> second. age <""
<"City:" <m-> second. City <"" <"Phone:" <m-> second. Phone <Endl;
}

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.