C ++ STL learning notes 10 multimap multi- ing container

Source: Internet
Author: User

/*
*
**************************************** ****
* Basic descriptions of multimap multi- ing containers:
**************************************** ****
*
* Multimap multi- ing container: the data structure of the container is managed using a red/black tree.
* All multimap elements are pair. The first element is a key and cannot be modified. The second element is a real value and can be modified.
*
* The features and usage of multimap are exactly the same as those of map. The only difference is:
* Allow elements with duplicate key values to be inserted into the container (using the insert_equal function of Rb-tree)
* Therefore:
* The ing relationship between the key value and the element value is many-to-many.
* No [] operation defined
*
* Sorted associative container pair associative container unique associative container
*
* To use multimap, you must use a macro statement # include <map>
*
**************************************** **************************************** ******
*
* Create a multimap object:
* 1. multimap <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. multimap (const key_compare & Comp) // specify a comparison function object comp to create a map object
* 3. multimap (const multisetr &); // multimap <int, char *> B (a); // use the default key value to compare functions less <int>
* 4. multimap (first, last );
* 5. multimap (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 };
* Multimap <const int, char> M4 (pairarray, pairarray + 5 );
* Multimap <const int, char> m3 (M4 );
* Multimap <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;
* Size_type count (const key_type & K) const; // number of elements whose return key value is K
* 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>

// Basic operation and set type, 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;
};

Int main (){
Using namespace STD;

Student s [] = {
{"Tong Jin", 23, "Wuhan", "XXX "},
{"Boss", 23, "Wuhan", "XXX "},
{"Dumplings", 23, "Wuhan", "XXX "},
{"Wang tiao", 23, "Wuhan", "XXX "},
{"Zhou runfa", 23, "Wuhan", "XXX "},
{"Zhou Xing", 23, "Wuhan", "XXX "}
};
Pair <int, student> P1 (4, s [0]);
Pair <int, student> P2 (2, s [1]);
Pair <int, student> P3 (3, S [2]);
Pair <int, student> P4 (4, s [3]); // the key value is the same as that of P1
Pair <int, student> P5 (5, s [4]);
Pair <int, student> P6 (6, s [5]);
Multimap <int, student>;
A. insert (P1 );
A. insert (P2 );
A. insert (P3 );
A. insert (P4 );
A. insert (P5 );
A. insert (P6 );
Typedef multimap <int, student>: iterator int_multimap;
Pair <int_multimap, int_multimap> P = A. pai_range (4 );
Int_multimap I = A. Find (4 );
Cout <"the key value in the class is" <I-> first <":" <. count (4) <"name," <"they are:" <Endl;
For (int_multimap K = P. First; k! = P. Second; k ++ ){
Cout <k-> second. Name <Endl;
}
Cout <"classmates who delete duplicate key values" <Endl;
A. Erase (I );
Cout <"Total number of students in the current class:" <A. Size () <". Personnel:" <Endl;
For (multimap <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;
}

Return 0;
}

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.