Analysis of STL source code [container] (17) [Stl_multimap.h]

Source: Internet
Author: User

Multimap, like map, uses the red-black tree to record the element data, by the element key value comparison relation, carries on the fast inserts, deletes and retrieves the operation, the difference is Multimap allows the element that has duplicate key value to insert the container. In the Multimap container, the relationship between the key value of the element and the mapping data of the element is Many-to-many, so the Multimap is called a multiple mapping container. The multiplicity of characteristics between Multimap and map is similar to the multiple characteristics difference between multiset and set.
Multimap multiple mapping container implements interface specification of Sorted associative Container, Pair associative Container and multimap associative Container concept.

Multimap Source:

Filename:stl_multimap.h//Comment by: Frost//e-mail:mdl2009@vip.qq.com//Blog:http://blog.csdn.net /mdl13412 * * * Copyright (c) 1994 * Hewlett-Packard Company * * Permission to use, copy, modify, distribute and SE ll this software * and its documentation to any purpose are hereby granted without fee, * provided that above Copyri Ght notice appear in all copies and * so both that the copyright notice and this permission notice appear * in supporting  Documentation.  Hewlett-Packard Company makes no * representations about the suitability of this software to any * purpose.
 It is provided ' as is ' without express or implied warranty.
 * * Copyright (c) 1996,1997 * Silicon Graphics Computer Systems, Inc.  * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby Granted without fee, * provided that above copyright notice appear in all copies and * that both Ice And this permission notice appear * in supporting documentation.  Silicon Graphics makes no * representations about the suitability of this software to any * purpose.
 It is provided ' as is ' without express or implied warranty.
 */* Note:this is a internal header file, included by the other STL headers.
 * You should not attempt to use it directly. * * #ifndef __sgi_stl_internal_multimap_h #define __SGI_STL_INTERNAL_MULTIMAP_H __stl_begin_namespace #if defined (__ SGI) &&!defined (__gnuc__) && (_mips_sim!= _mips_sim_abi32) #pragma set Woff 1174 #endif//If the compiler is not able to follow the preceding model The board parameter derives the default parameter type that is used later,//Then it needs to be manually specified, this implementation multimap internal element default use less for comparison//internal maintenance of the data structure is red-black tree, with very good worst-case time complexity//NOTE: Unlike map, Multimap allows duplicate elements #ifndef __stl_limited_default_templates template <class Key, class T, class Compare = less<key> , class Alloc = alloc> #else template <class Key, class T, Class Compare, class Alloc = Alloc> #endif class Multi Map {public://Here is the same as the map definition, see <setl_map.h> typedef Key Key_type;
  typedef T DATA_TYPE;
  typedef T MAPPED_TYPE;
  typedef pair<const Key, t> value_type;

  typedef Compare Key_compare; About why inherit from Binary_function see <stl_function.h> in the Tutorial//nested class to provide key comparison operations class Value_compare:public BINARY_FUNCTION&L
  T;value_type, Value_type, bool> {friend class Multimap<key, T, Compare, alloc>;
    Protected:compare comp; Value_compare (compare C): Comp (c) {} Public:bool operator () (const value_type& x, const value_type& y) cons
    T {return comp (X.first, Y.first);

}
  }; Private://internal use of red-black tree for data structure, in fact now <stl_tree.h> typedef rb_tree<key_type, Value_type, select1st&
  Lt;value_type&gt, Key_compare, alloc> Rep_type;  Rep_type T; Red-black tree representing Multimap public://marked as ' STL standard mandatory ' typedefs used to provide iterator_traits<i> support//NOTE: iterators, Reference types are designed as const, which is determined by the nature of the multimap,//If the user changes their values themselves, may cause problems with the internal red-black tree typedef typename REP_TYPE::p oInter pointer;
  The STL standard obligates the TypeDef typename Rep_type::const_pointer Const_pointer;              typedef typename Rep_type::reference Reference;
  The STL standard obligates the TypeDef typename Rep_type::const_reference const_reference;                typedef typename Rep_type::iterator iterator;
  The STL standard obligates the TypeDef typename Rep_type::const_iterator Const_iterator;
  typedef typename Rep_type::reverse_iterator Reverse_iterator;
  typedef typename Rep_type::const_reverse_iterator Const_reverse_iterator;
  typedef typename REP_TYPE::SIZE_TYPE Size_type;  typedef typename REP_TYPE::d ifference_type difference_type; STL Standard enforces Multimap (): t (Compare ()) {} explicit Multimap (const compare& comp): T (comp) {} #ifdef __STL_MEM Ber_templates template <class inputiterator> multimap (Inputiterator-I, Inputiterator last): T (Compare ()

  ) {t.insert_equal (I, last);} Template <class inputiterator> multimap (Inputiterator, Inputiterator, Last, Const compare& Comp): T (comp) {t.insert_equal (I, last);} #else Multimap (const value_type*-I, const V
  Alue_type* last): T (Compare ()) {t.insert_equal (I, last);} Multimap (const value_type*, const value_type* last, const compare& comp): T (comp) {t.insert_equ Al (A, last);
  } multimap (Const_iterator, Const_iterator last): T (Compare ()) {t.insert_equal (A, last);} Multimap (Const_iterator, const_iterator, const compare& comp): T (comp) {t.insert_equal (I, last); #endif/* __stl_member_templates/* MULTIMAP (const Multimap<key, T, Compare, alloc>& x): T (x.t) {} mul 
    Timap<key, T, Compare, alloc>& operator= (const Multimap<key, T, Compare, alloc>& x) {t = x.t;
  return *this;

  //Returns the function for key comparison Key_compare Key_comp () const {return t.key_comp ();} Because of the nature of the Multimap, value comparison and key use the same comparison function Value_compare Value_comp () const {return Value_cOmpare (T.key_comp ());
  Iterator begin () {return t.begin ();}
  Const_iterator begin () const {return t.begin ();}
  Iterator End () {return t.end ();}
  Const_iterator End () const {return t.end ();}
  Reverse_iterator Rbegin () {return t.rbegin ();}
  Const_reverse_iterator Rbegin () const {return t.rbegin ();}
  Reverse_iterator rend () {return t.rend ();}
  Const_reverse_iterator rend () const {return t.rend ();}
  BOOL Empty () const {return t.empty ();}
  Size_type size () const {return t.size ();}

  Size_type max_size () const {return t.max_size ();} This calls for a dedicated swap, not a global swap, which is scheduled for <stl_tree.h> void Swap (Multimap<key, T, Compare, alloc>& x) {T.swap (x.t );

  }//Insert element, note that the inserted element key allows repeated iterator inserts (const value_type& x) {return t.insert_equal (x);} Inserts an element at position, but position is only a hint, and if the given position cannot be inserted, the//STL will search, which can result in poor efficiency iterator insert (iterator position, const VA
  lue_type& x) {return t.insert_equal (position, x); } #ifdef __Stl_member_templates template <class inputiterator> void Insert (Inputiterator-I, Inputiterator last) {
  T.insert_equal (the last);
  #else void Insert (const value_type*, value_type* last) {t.insert_equal (a);
  } void Insert (Const_iterator, Const_iterator last) {t.insert_equal (a); #endif/* __stl_member_templates//Erase element at the specified location causes the inner red and black tree to rearrange the void erase (iterator position) {t.erase (position);

  //Returns the number of erase elements Size_type Erase (const key_type& x) {return t.erase (x);}

  Erasing the elements of a specified interval causes a large change in the red and Black tree Void erase (iterator, iterator last) {t.erase (I, last);}

  All right, clear all, bye. Red black tree Void Clear () {t.clear ();}
  Finds the specified element iterator find (const key_type& x) {return t.find (x);}

  Const_iterator Find (const key_type& x) const {return t.find (x);}

  Returns the number of specified elements size_type count (const key_type& x) const {return t.count (x);} Returns the first pluggable position less than the current element iterator Lower_bounD (const key_type& x) {return t.lower_bound (x);}
  Const_iterator lower_bound (const key_type& x) const {return t.lower_bound (x);
  //Returns the first pluggable position greater than the current element iterator Upper_bound (const key_type& x) {return t.upper_bound (x);}
  Const_iterator upper_bound (const key_type& x) const {return t.upper_bound (x);
  } pair<iterator,iterator> Equal_range (const key_type& x) {return t.equal_range (x);
  } pair<const_iterator,const_iterator> Equal_range (const key_type& x) const {return t.equal_range (x); friend BOOL operator== __stl_null_tmpl_args (const multimap&, con
  St multimap&); friend BOOL operator< __stl_null_tmpl_args (const multimap&, const MU
ltimap&);

}; Comparison of two multimap is its internal red-black tree, will trigger the red-black tree operator template <class Key, class T, Class Compare, class alloc> inline bool O perator== (const multimap<Key, T, Compare, alloc>& x, const Multimap<key, T, Compare, alloc>& y) {return
x.t = = y.t; Template <class Key, class T, Class Compare, class alloc> inline bool operator< (const Multimap<key, T, Comp 
are, alloc>& x, const Multimap<key, T, Compare, alloc>& y) {return x.t < y.t; //If the compiler supports template function-specific precedence//Then the global swap is implemented to use Multimap private swap to improve efficiency #ifdef __stl_function_tmpl_partial_order template <cla
                 SS Key, Class T, Class Compare, class alloc> inline void Swap (Multimap<key, T, Compare, alloc>& x,

Multimap<key, T, Compare, alloc>& y) {x.swap (y);}  #endif/* __stl_function_tmpl_partial_order * * #if defined (__SGI) &&!defined (__gnuc__) && _mips_sim!= _MIPS_SIM_ABI32) #pragma reset woff 1174 #endif __stl_end_namespace #endif/* __sgi_stl_internal_multimap_h *//Loca L Variables://mode:c++//end:

Example:


 

Reverse Traversal/* Use the MULTIMAP container-defined reverse iterator reverse_iterator and const_reverse_iterator, and obtain the rbegin and rend functions of the reverse-end-to-end element, which can be traversed in reverse Multimap
    The element of the container.
    For example, the following use of reverse traversal, the fruit price list records by price size, from large to small print out. Note: Pear with the same key value will be printed before Apple.
It can be seen that, in the case of the reverse traversal of the key values, the inserted elements are printed first. * *--------------------------------------------------------Reverse traverse the elements of the Multimap container #pragma warning (disable:4786) #
Include <map> #include <iostream> using namespace std;
    int main () {multimap<float, char*> mm;
    Mm.insert (Pair<float, char*> (3.0f, "apple"));
    Mm.insert (Pair<float, char*> (3.0f, "pear"));
    Mm.insert (Pair<float, char*> (2.6f, "orange"));
    Mm.insert (Pair<float, char*> (1.8f, "banana"));
    Mm.insert (Pair<float, char*> (6.3f, "Lichee"));
    Reverse traverse print multimap<float, Char*>::reverse_iterator R_i, r_iend;
    R_iend = Mm.rend (); For (R_i=mm.rbegin (); R_i!=r_iend ++r_i) cout << (*r_i). Second << ' << (*r_i). I &LT;&L T

    "Yuan/jin \";
return 0; } 


 

Search for elements of the Multimap container 1/* The element with the same key value in the Multimap container may be more than one, because the key value allows repeat insertion. Therefore, the find function of the Multimap container returns the position of the first element to be searched, and returns the end element position if the element does not exist.
The Equal_range function returns a pair object that can indicate an equal range of elements. */--------------------------------------------------------search for elements of Multimap container 1 #pragma warning (disable:4786) #

Include <map> #include <iostream> using namespace std;  Curriculum records Structure body struct Courserecord {//Course information structure struct Courseinfo {char* course;   Course name int period; Hours char* required;

    compulsory or elective};  char* teacher;
    Instructor Courseinfo CF;
        Courserecord (char* teacher_, char* course_, int period_, char* required_) {teacher = Teacher_;
        Cf.course = Course_;
        Cf.period = Period_;
    cf.required = Required_;

}
};
    int main () {///Create Multimap container object mm typedef multimap<char*, courserecord::courseinfo> Coursemmap;
    Coursemmap mm;
 Insert 1th record Courserecord course1 = Courserecord ("John", "Operating system development", 60, "compulsory");   pair<char*, courserecord::courseinfo> pairCourse1 (Course1.teacher, COURSE1.CF);

    Mm.insert (PAIRCOURSE1);
    Insert 2nd record Courserecord Course2 = Courserecord ("Dick", "Compiler development", 30, "compulsory");
    pair<char*, courserecord::courseinfo> pairCourse2 (Course2.teacher, COURSE2.CF);


    Mm.insert (PAIRCOURSE2);
    Insert 3rd record Courserecord course3 = Courserecord ("Dick", "Data Structure", 20, "compulsory");
    pair<char*, courserecord::courseinfo> pairCourse3 (Course3.teacher, COURSE3.CF);


    Mm.insert (PAIRCOURSE3);
    Insert 4th record Courserecord Course4 = Courserecord ("Dick", "Java development Application", 38, "compulsory");
    pair<char*, courserecord::courseinfo> pairCourse4 (Course4.teacher, COURSE4.CF);

    Mm.insert (PAIRCOURSE4);
    Insert 5th record Courserecord course5 = Courserecord ("Harry", "C + + programming", 58, "compulsory");
    pair<char*, courserecord::courseinfo> pairCourse5 (Course5.teacher, COURSE5.CF);

    Mm.insert (PAIRCOURSE5);
    Record search cout << "Search < dick teacher > classroom record: \ n"; Pair<coursemmap:: iterator, coursemmap::iterator> p = mm.equal_range ("Dick");
    Print results Coursemmap::iterator i; For (I=p.first i!=p.second ++i) cout << (*i). A << ' << (*i). second.cours E << ' << (*i) second.period << "Hours" << (*i). second.required &L
    t;< ' << Endl;

    cout << Endl << Endl;
return 0; }


 

Multimap container Element Search 2--------------------------------------------------------multimap container element Search 2 #pragma warning ( disable:4786) #include <iostream> #include <string> #include <UTILITY> #include <map> using

namespace Std;
    struct Userdevice {string m_devicename;
    Long m_deviced;
int m_devicepopedom;

};        
typedef multimap<string, userdevice> usertable;
typedef usertable::const_iterator CIT;

typedef pair<cit, cit> Range;
    int main () {//define an iterator CIT it;
    Define 4 devices Userdevice D1, D2, D3, D4;
    d1.m_deviced = 12341234;
    D1.m_devicename = "D1";
    
    D1.m_devicepopedom = 123;
    d2.m_deviced = 23622344;
    D2.m_devicename = "D2";
    
    D2.m_devicepopedom = 234;
    d3.m_deviced = 3451234;
    D3.m_devicename = "D3";
    
    D3.m_devicepopedom = 345;
    d4.m_deviced = 43622344;
    D4.m_devicename = "D4";
    
    D4.m_devicepopedom = 456;
    Usertable M_user; M_user.insert (pair<string, Userdevice>("Zhangsanfeng", D1));

    M_user.insert (pair<string, userdevice> ("Zhangsanfeng", D2));
    M_user.insert (pair<string, userdevice> ("Zhangsanfeng2", D3));
    
    M_user.insert (pair<string, userdevice> ("Zhangsanfeng2", D4));
    Find method One (lookup key value is "Zhangsanfeng") Range range = M_user.equal_range ("Zhangsanfeng");
            for (CIT i = Range.first i!=range.second; ++i) {cout<< i->second.m_deviced << ', ' << i->second.m_devicename.c_str () << ', ' << i->second.m_devicepopedom &LT;&L T
    Endl
    


    } cout<< Endl;
    Find method Two (find "Zhangsanfeng2") CIT it2 = M_user.find ("Zhangsanfeng2"); while (It2!=m_user.end ()) {cout<< it2->second.m_deviced << ', ' << it2-
        >second.m_devicename.c_str () << ', ' << it2->second.m_devicepopedom << Endl;
    ++it2;
    

  } cout<< Endl;  Traverse CIT it3 = M_user.begin (); while (It3!=m_user.end ()) {cout<< i->second.m_deviced << ', ' << It3->secon
        
        D.m_devicename.c_str () << ', ' << it3->second.m_devicepopedom << Endl;
    ++IT3;

    } cout<< Endl;
return 0; }


Design of Multimap container elements/
* The    following example program calls the size and count functions of the Multimap container and counts the number of elements and the number of elements that go to a key value.
*

------------------- Design of-------------------------------------multimap container elements
#pragma warning (disable:4786)
#include <map>
#include <iostream>
using namespace std;


int main ()
{
    multimap<int, char> mm;
    Number of printed elements: 0
    cout << mm.size () << Endl;  
    Mm.insert (Pair<int, char> (3, ' a '));
    Mm.insert (Pair<int, char> (3, ' C '));
    Mm.insert (Pair<int, char> (4, ' d '));
    Mm.insert (Pair<int, char> (5, ' e '));
    The number of elements that print a key value of 3
    cout << mm.count (3) << Endl;
    Number of printed elements
    cout << mm.size () << 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.