Map container
The data processed by map is similar to a database table with key values, and establishes a mathematical mapping between the key value and the mapped data. The data structure of the map container is still managed by the red and black tree, the inserted element key value is not allowed to be duplicated, the comparison function of the node element used only compares the key value of the element, and the data of the element can be retrieved by the key value. For the key value and the mapping data, can be encapsulated into a structure object by the pair ,map to do is to insert this pair object into the red black tree, but also need to provide a only use the key value to compare the function object, Pass it to the red-black tree. At this point, the operation of the red and black tree can be used to insert the map element into the correct position of the two-fork tree and to delete and retrieve the elements. The main difference between map and set is thatmap deals with the fast insertion, deletion, and retrieval of data for record-type elements with key values, while set is the processing of a single data. Both are a generalization of a generic library to a two-fork tree.
Create a Map object
There are several main ways to do this.
(1) map ()
Creates an empty map object with a key value type of char, an element with a mapping data type of int, and a comparison function object for the key value greater<char>
Map<char,int,grater<char>> m;
(2) map (const KEY_COMPARE&CMP)
struct strless{
BOOL Operator () (const char* s1,const char*s2) const
{
Return strcmp (S1,S2) <0;
}
};
Map<const char*,int> m (strless ());
(3) map (const map&)
Copy constructor.
Map<int,char*> M1;
map<int,char*> m2 (M1);
(4) map (inputiteratorfirst,inputiterator last)
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> ARRAY[]={P1,P2,P3,P4};
Map<const int,char> m (array,array+4);
(5) map (inputiteratorfirst,inputiterator last, const KEY_COMPARE&CMP)
Map<const int,char,greater<const int>>m (Array,array+4,greater<const int> ());
Insertion of elements
The insertion of an element mainly takes advantage of the Insert function, which can also be explicitly assigned to a map using an array operation, but cannot detect if the insert succeeds.
#include <iostream> #include <map>using namespace Std;int main () {map<const char*,float> m;m["Apple"] =1.5f;m["Orange"]=2.0f;m["banana"]=1.1f;cout<< "Apple Price:" <<m["apple"]<< "Yuan/catty" <<endl;cout << "Orange Price:" <<m["orange"]<< "Yuan/Jin" <<endl;cout<< "Banana Price:" <<m["banana"]<< "yuan/catty "<<endl;return 0;}
Deletion of elements
As with the set collection container, themap container can delete an element on an iterator position, an element equal to a key value, an element on an iterator interval, and all elements in the container, theerase function and the clear function.
Traversal of elements
The traversal of the map element accesses the element in addition to the array of key values, and can be accessed using an iterator.
#include <iostream> #include <map>using namespace std;struct Stuinfo{char *name;int age;}; struct Sturecord{int id;stuinfo SF;}; int main () {Sturecord sarray[]={{1, "Li", 20},{10, "Shi", 18},{3, "Wang",21}};map<int,stuinfo> m;for (int i=0;i< 3;i++) {m[sarray[i].id]=sarray[i].sf;} Map<int,stuinfo>::iterator begin,end;end=m.end ();cout<< "School Number" << "name" << "Age" <<endl;for (Begin=m.begin (); begin!=end;begin++) {cout<< (*begin) .first<< "" << (*begin) .second.name<< "" << (*begin) .second.age<< "" <<ENDL; return 0;}
It can be seen from the results that, although it is an unordered insertion, the result of the traversal is orderly.
Searching for elements
Use the find function to search for an element with a certain key value.
#include <iostream> #include <map>using namespace std;struct sturecord{struct Stuinfo{char *name; int age;}; Sturecord (int id_,char *name_,int age_) {Id=id_;sf.name=name_;sf.age=age_;} int id;stuinfo SF;}; int main () {typedef map<int,sturecord::stuinfo> STUMAP;STUMAP m;pair<stumap::iterator,bool> p;// Insert the first Student record Sturecord Stu1=sturecord (5, "Li");p air<int,sturecord::stuinfo> pairStu1 (stu1.id,stu1.sf);p = M.insert (PAIRSTU1); if (!p.second) cout<< "Insert student record failed \ n";//Insert second student record Sturecord Stu2=sturecord (1, "Shi",;p Air) <int,stuRecord::stuInfo> pairStu2 (STU2.ID,STU2.SF);p =m.insert (PAIRSTU2); if (!p.second) cout<< " Insert student record failed \ n ";//Insert Third student record Sturecord Stu3=sturecord (" Zhang ");p air<int,sturecord::stuinfo> PairStu3 ( STU3.ID,STU3.SF);p =m.insert (PAIRSTU3); if (!p.second) cout<< "Insert student record failed \ n"; Search Stumap::iterator I=m.find (5);cout<< "Search for 5 records: \ n" << (*i) .first<< "<< (*i). Second.name << ' << (*i) .second.age<< ' <<endl; Return 0;}
Map also provides other functions, empty, size, swap, Lower_bound, Upper_bound, Equal_range, and so on.
Multimap Multi-mapping container
Multimap containers are also used in red and black trees to record-type element data in accordance with the key value of the comparison between the fast insertion, deletion, retrieval, element retrieval is the time complexity of the number of levels, and map is different,Multimap Allows elements with duplicate key values to be inserted into the container, and the key value of the element is many-to-many mappings to the element's mapping data.
Create a Multimap object
There are several ways to do this.
(1) multimap ()
multimap<char,int,greater<char>>mm;
(2) Multimap (constkey_compare&cmp)
struct strless{
BOOL Operator () (const char *s1,const char*s2) const
{
Return strcmp (S1,S2) <0;
}
};
multimap<constchar*,int> mm (strless ());
(3) multimap (const map&)
Multimap<int,char*> mm1;
Multimap<int,char*> mm2 (MM1);
(4) Multimap (inputiteratorfirst,inputiterator last)
pair<constint,char> P1 (1, ' a ');
pair<constint,char> P2 (1, ' e ');
Pair<constint,char> P3 (2, ' B ');
Pair<constint,char> P4 (3, ' C ');
Pair<constint,char> PAIRARRAY[]={P1,P2,P3,P4};
multimap<constint,char> mm (pairarray,pairarray+4);
(5) Multimap (inputiteratorfirst,inputiterator last, const KEY_COMPARE&CMP)
Multimap<constint,char,greater<const int>> mm (pairarray,pairarray+4,greater<constint> ());
Insertion of elements
The Multimap container can only insert elements into the container's red-black tree using the insert function.
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.1f, "orange"));
Mm.insert (pair<float,char*> (1.5f, "banana"));
Deletion of elements
Like the map collection container, themap container can delete an element on an iterator position, an element equal to a key value, an element on an iterator interval, and all elements in the container, theerase function and the clear function.
Traversal of elements
The traversal of the Multimap container can be implemented with iterators.
#include <iostream> #include <map>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.1f, "orange")); Mm.insert (Pair<float,char *> (1.5f, "banana")); Multimap<float,char *>::iterator begin,end; End=mm.end (); For (Begin=mm.begin (); begin!=end;begin++) { cout<< (*begin) .second<< "" << (*begin). << "Yuan/Jin" <<endl; } Cout<<endl;return 0;}
Reverse traversal
Reverse iterators can be used to implement reverse traversal.
#include <iostream> #include <map>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.1f, "orange")); Mm.insert (Pair<float,char *> (1.5f, "banana")); Multimap<float,char *>::reverse_iterator rbegin,rend; Rend=mm.rend (); For (Rbegin=mm.rbegin (); rbegin!=rend;rbegin++) { cout<< (*rbegin) .second<< "" << (*rbegin ) .first<< "Yuan/Jin" <<endl; } return 0;}
Searching for elements
The Multimap container's find function returns the position of the first searched element, and returns the end element position if the element does not exist.
Other common functions
other commonly used functions are Empty , size , Count , Lower_bound , Upper_bound and so on.
#include <iostream> #include <map>using namespace Std;int main () {multimap<int,char> mm;cout<< Mm.size () <<endl;mm.insert (pair<int,char> (3, ' a ')), Mm.insert (pair<int,char> (3, ' C ')); Mm.insert ( Pair<int,char> (1, ' B ')); Mm.insert (pair<int,char> (2, ' d ')); Mm.insert (pair<int,char> (3, ' e ')); Mm.insert (pair<int,char> (4, ' G ')); Cout<<mm.count (3) <<endl;cout<<mm.size () <<endl; return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Map and Multimap map containers