constructors

#include <iostream> #include <map> using namespace std; int main () { typedef multimap<int, char, less<int> > M; M m1; m1.insert(M::value_type(2,'B')); m1.insert(M::value_type(3,'C')); m1.insert(M::value_type(1,'A')); m1.insert(M::value_type(1,'a')); M::iterator It = m1.begin(); cout << endl << "m1:" << endl; while ( It != m1.end() ) { cout << (*It).first << " - " << (*It).second << endl; It++; } // copy constructor M m2(m1); It = m2.begin(); cout << endl << "m2:" << endl; while ( It != m2.end() ) { cout << (*It).first << " - " << (*It).second << endl; It++; } M m3(m2.begin(),m2.end()); It = m3.begin(); cout << endl << "m3:" << endl; while ( It != m3.end() ) { cout << (*It).first << " - " << (*It).second << endl; It++; } return 0; } OUTPUT: // m1: // 1 - A // 1 - a // 2 - B // 3 - C // // m2: // 1 - A // 1 - a // 2 - B // 3 - C // // m3: // 1 - A // 1 - a // 2 - B // 3 - C begin
returns an iterator to the first element #include <iostream> #include <map> #include <string> using namespace std; int main () { typedef multimap<string,int> M; typedef M::value_type v_t; M m; m.insert(v_t("first",100)); m.insert(v_t("second",200)); m.insert(v_t("third",300)); m.insert(v_t("second",400)); m.insert(v_t("third",500)); M::iterator It = m.begin(); cout << "m:" << endl; while ( It != m.end() ) { cout << (*It).first << " - " << (*It).second << endl; It++; } return 0; } OUTPUT: // m: // first - 100 // second - 200 // second - 400 // third - 300 // third - 500 clear
removes all elements #include <iostream> #include <map> OUTPUT: count
returns the number of elements #include <iostream> #include <map> #include <string> #include <fstream> using namespace std; int main () { typedef multimap<char,string> M1; typedef M1::value_type v_t1; M1 m1; typedef multimap<string,char,less<string> > M2; typedef M2::value_type v_t2; M2 m2; string word; int counter = 0; ifstream In("/usr/share/dict/words"); if ( In.good() ) { while(1) { getline(In,word); char ch = word.at(0); // file is sorted if ( ch != 'A' && ch != 'a' ) break; else { // for conting of words m1.insert(v_t1(ch,word)); // for upper-lower bound m2.insert(v_t2(word,ch)); } counter++; } In.close(); } cout << "System Dictionary consists " << counter << " words,\nwith first letter 'a' or 'A'" << endl; cout << m1.count('A') << " words start with 'A'" << endl; cout << m1.count('a') << " words start with 'a'" << endl; M2::iterator low = m2.lower_bound("Aba"); M2::iterator upp = m2.upper_bound("Abe"); cout << "Range of the words from 'Aba' to 'Abe':" << endl; while ( low != upp ) { cout << (*low).first << endl; low++; } return 0; } OUTPUT: // System Dictionary consists 3577 words, // with first letter 'a' or 'A' // 491 words start with 'A' // 3086 words start with 'a' // Range of the words from 'Aba' to 'Abe': // Ababa // Abba // Abbott // Abby // Abe empty
true if the maltimap is empty #include <iostream> #include <map> using namespace std; int main () { typedef multimap<int,int> M; typedef M::value_type v_t; M m; m.insert(v_t(1,100)); m.insert(v_t(1,200)); m.insert(v_t(2,300)); m.insert(v_t(3,400)); cout << "values of multimap 'm': "; M::iterator It = m.begin(); while ( It != m.end() ) { cout << (*It).second << " "; It++; } cout << endl; cout << "size of multimap = " << m.size() << endl; cout << "multimap 'm' is " << (m.empty() ? "" : "not ") << "empty" << endl << endl; m.erase(m.begin(),m.end()); cout << "After m.erase(m.begin(),m.end())" << endl; cout << "size of multimap = " << m.size() << endl; cout << "multimap 'm' is " << (m.empty() ? "" : "not ") << "empty" << endl; return 0; } OUTPUT: // values of multimap 'm': 100 200 300 400 // size of multimap = 4 // multimap 'm' is not empty // // After m.erase(m.begin(),m.end()) // size of multimap = 0 // multimap 'm' is empty end
returns an iterator to the last element #include <iostream> #include <map> #include <string> using namespace std; int main () { typedef multimap<string,int> M; typedef M::value_type v_t; M m; m.insert(v_t("first",100)); m.insert(v_t("second",200)); m.insert(v_t("third",300)); m.insert(v_t("second",400)); m.insert(v_t("third",500)); M::iterator It = m.begin(); cout << "m:" << endl; while ( It != m.end() ) { cout << (*It).first << " - " << (*It).second << endl; It++; } return 0; } OUTPUT: // m: // first - 100 // second - 200 // second - 400 // third - 300 // third - 500 equal_ranges
returns iterators to the first and last elements that match a certain key #include <iostream> #include <map> OUTPUT: erase
removes elements #include <iostream> #include <map> #include <string> using namespace std; typedef multimap<string, int, less<string> > M; void print (M& m) { M::iterator It = m.begin(); cout << "multimap :" << endl; while ( It != m.end() ) { cout << (*It).first << " - "; cout << (*It).second << endl; It++; } } //----------------------------------- int main () { typedef M::value_type v_t; M m; m.insert(v_t("AAA",1)); m.insert(v_t("BBB",2)); m.insert(v_t("CCC",3)); m.insert(v_t("EEE",4)); m.insert(v_t("CCC",5)); m.insert(v_t("DDD",6)); print(m); // remove element with key 'BBB' m.erase("BBB"); print(m); M::iterator It; It = m.find("DDD"); // remove element pointed by It m.erase(It); print(m); It = m.find("CCC"); // remove the range of elements m.erase(m.begin(),It); print(m); return 0; } OUTPUT: // multimap : // AAA - 1 // BBB - 2 // CCC - 3 // CCC - 5 // DDD - 6 // EEE - 4 // multimap : // AAA - 1 // CCC - 3 // CCC - 5 // DDD - 6 // EEE - 4 // multimap : // AAA - 1 // CCC - 3 // CCC - 5 // EEE - 4 // multimap : // CCC - 3 // CCC - 5 // EEE - 4 find
finds a given element #include <iostream> #include <map> using namespace std; int main () { typedef multimap<int,char> M; typedef M::value_type v_t; char ch = 'A'; M m; for ( int i=0; i<5; i++ ) m.insert(v_t(i,ch++)); m.insert(v_t(4,'F')); M::iterator It = m.begin(); cout << "multimap m:" << endl; while ( It != m.end() ) { cout << (*It).first << " - " << (*It).second << endl; It++; } It = m.find(4); if ( It != m.end() ) cout << "element key '4' has value " << (*It).second << endl; else cout << "element key '4' not found" << endl; M::iterator upp = m.upper_bound(4); cout << "all elements with key '4'" << endl; while ( It != upp ) { cout << (*It).first << " - " << (*It).second << endl; It++; } return 0; } OUTPUT: // multimap m: // 0 - A // 1 - B // 2 - C // 3 - D // 4 - E // 4 - F // element key '4' has value E // all elements with key '4' // 4 - E // 4 - F insert
inserts elements into the maltimap #include <iostream> #include <map> #include <string> using namespace std; int main () { typedef multimap<int, char, less<char> > M; typedef M::value_type v_t; M m1, m2; char ch = 'A'; for ( int i=0; i<3; i++ ) { m1.insert(v_t(i+1,ch+i)); m2.insert(v_t(i+4,ch+i+3)); } cout << "m1 :" << endl; M::iterator It = m1.begin(); while ( It != m1.end() ) { cout << (*It).first << " - " << (*It).second << endl; It++; } cout << "m2 :" << endl; It = m2.begin(); while ( It != m2.end() ) { cout << (*It).first << " - " << (*It).second << endl; It++; } // insert new element m1.insert(v_t(5,'E')); It = m2.find(6); // insert element pointed by It m1.insert(*It); cout << "m1 :" << endl; It = m1.begin(); while ( It != m1.end() ) { cout << (*It).first << " - " << (*It).second << endl; It++; } // insert the range of elements m1.insert(m2.begin(),m2.end()); cout << "m1 :" << endl; It = m1.begin(); while ( It != m1.end() ) { cout << (*It).first << " - " << (*It).second << endl; It++; } return 0; } OUTPUT: // m1 : // 1 - A // 2 - B // 3 - C // m2 : // 4 - D // 5 - E // 6 - F // m1 : // 1 - A // 2 - B // 3 - C // 5 - E // 6 - F // m1 : // 1 - A // 2 - B // 3 - C // 4 - D // 5 - E // 5 - E // 6 - F // 6 - F lower_bound
returns an iterator to the first element greater than a certain value #include <iostream> #include <map> #include <string> #include <fstream> using namespace std; int main () { typedef multimap<char,string> M1; typedef M1::value_type v_t1; M1 m1; typedef multimap<string,char,less<string> > M2; typedef M2::value_type v_t2; M2 m2; string word; int counter = 0; ifstream In("/usr/share/dict/words"); if ( In.good() ) { while(1) { getline(In,word); char ch = word.at(0); // file is sorted if ( ch != 'A' && ch != 'a' ) break; else { // for counting of words m1.insert(v_t1(ch,word)); // for upper-lower bound m2.insert(v_t2(word,ch)); } counter++; } In.close(); } cout << "System Dictionary consists " << counter << " words,\nwith first letter 'a' or 'A'" << endl; cout << m1.count('A') << " words start with 'A'" << endl; cout << m1.count('a') << " words start with 'a'" << endl; M2::iterator low = m2.lower_bound("Aba"); M2::iterator upp = m2.upper_bound("Abe"); cout << "Range of the words from 'Aba' to 'Abe':" << endl; while ( low != upp ) { cout << (*low).first << endl; low++; } return 0; } OUTPUT: // System Dictionary consists 3577 words, // with first letter 'a' or 'A' // 491 words start with 'A' // 3086 words start with 'a' // Range of the words from 'Aba' to 'Abe': // Ababa // Abba // Abbott // Abby // Abe key_comp
returns the function that compares keys #include <iostream> #include <map> OUTPUT: max_size
the maximum number of elements that the maltimap can hold #include <iostream> #include <map> using namespace std; int main () { typedef multimap<int, char, greater<int> > M; typedef M::value_type v_t; M m; m.insert(v_t(2,'B')); m.insert(v_t(3,'C')); m.insert(v_t(1,'A')); M::iterator It = m.begin(); cout << "m:" << endl; while ( It != m.end() ) { cout << (*It).first << " - " << (*It).second << endl; It++; } cout << "size of multimap 'm' " << m.size() << endl; cout << "max_size of 'm' " << m.max_size() << endl; return 0; } OUTPUT: // m: // 3 - C // 2 - B // 1 - A // size of multimap 'm' 3 // max_size of 'm' 4294967295 rbegin
returns a reverse iterator to the end of the maltimap #include <iostream> #include <map> #include <iomanip> #include <string> using namespace std; template<class T> class ID { public: ID(T t, T n) : id(t), name(n) {} void print () { cout.setf(ios::left); cout << setw(15) << name.c_str() << id << endl; cout.unsetf(ios::left); } private: T id, name; }; //====================================== int main () { typedef ID<string> Id; typedef multimap<int, Id> M; typedef M::value_type v_t; M m; m.insert(v_t(1,Id("000123","Shevchenko"))); m.insert(v_t(2,Id("000124","Pushkin"))); m.insert(v_t(3,Id("000125","Shakespeare"))); // same key m.insert(v_t(3,Id("000126","Smith"))); M::reverse_iterator It = m.rbegin(); while ( It != m.rend() ) { cout.setf(ios::left); cout << setw(3) << (*It).first; It->second.print(); It++; } return 0; } OUTPUT: // 3 Smith 000126 // 3 Shakespeare 000125 // 2 Pushkin 000124 // 1 Shevchenko 000123 rend
returns a reverse iterator to the beginning of the maltimap #include <iostream> #include <map> #include <iomanip> #include <string> using namespace std; template<class T> class ID { public: ID(T t, T n) : id(t), name(n) {} void print () { cout.setf(ios::left); cout << setw(15) << name.c_str() << id << endl; cout.unsetf(ios::left); } private: T id, name; }; //====================================== int main () { typedef ID<string> Id; typedef multimap<int, Id> M; typedef M::value_type v_t; M m; m.insert(v_t(1,Id("000123","Shevchenko"))); m.insert(v_t(2,Id("000124","Pushkin"))); m.insert(v_t(3,Id("000125","Shakespeare"))); // same key m.insert(v_t(3,Id("000126","Smith"))); M::reverse_iterator It = m.rbegin(); while ( It != m.rend() ) { cout.setf(ios::left); cout << setw(3) << (*It).first; It->second.print(); It++; } return 0; } OUTPUT: // 3 Smith 000126 // 3 Shakespeare 000125 // 2 Pushkin 000124 // 1 Shevchenko 000123 size
the number of elements in the maltimap #include <iostream> #include <map> #include <iomanip> #include <string> using namespace std; template<class T> class ID { public: ID(T t, T n) : id(t), name(n) {} void print () { cout.setf(ios::left); cout << setw(15) << name.c_str() << id << endl; cout.unsetf(ios::left); } private: T id, name; }; //====================================== int main () { typedef ID<string> Id; typedef multimap<int, Id> M; typedef M::value_type v_t; M m; m.insert(v_t(1,Id("000123","Shevchenko"))); m.insert(v_t(2,Id("000124","Pushkin"))); m.insert(v_t(3,Id("000125","Shakespeare"))); // same key m.insert(v_t(3,Id("000126","Smith"))); cout << "size of multimap 'm' = " << m.size() << endl; M::iterator It = m.begin(); while ( It != m.end() ) { cout.setf(ios::left); cout << setw(3) << (*It).first; It->second.print(); It++; } return 0; } OUTPUT: // size of multimap 'm' = 4 // 1 Shevchenko 000123 // 2 Pushkin 000124 // 3 Shakespeare 000125 // 3 Smith 000126 swap
exchanges two maltimaps #include <iostream> #include <map> #include <list> #include <numeric> #include <algorithm> using namespace std; typedef multimap<int, list<int> > M; void print (M m) { M::iterator It = m.begin(); list<int>::iterator Li; while ( It != m.end() ) { cout << "key : " << (*It).first << "; value : "; for ( Li = It->second.begin(); Li != It->second.end(); Li++ ) cout << *Li << " "; It++; } cout << endl; } //------------------------------------------- int main () { list<int> L1, L2; L1.push_back(1); L1.push_back(2); L1.push_back(3); copy(L1.begin(),L1.end(), back_inserter(L2)); M m1, m2; m1.insert(M::value_type(1,L1)); m2.insert(M::value_type(2,L2)); cout << "multimap m1:" << endl; print(m1); cout << "multimap m2:" << endl; print(m2); if ( m1 == m2 ) cout << "multimaps m1 and m2 are equal" << endl; else { cout << endl << "After m1.swap(m2)" << endl; m1.swap(m2); cout << "multimap m1:" << endl; print(m1); cout << "multimap m2:" << endl; print(m2); } return 0; } OUTPUT: // multimap m1: // key : 1; value : 1 2 3 // multimap m2: // key : 2; value : 1 2 3 // // After m1.swap(m2) // multimap m1: // key : 2; value : 1 2 3 // multimap m2: // key : 1; value : 1 2 3 upper_bound
returns an iterator to the first element greater than a certain value #include <iostream> #include <map> #include <string> #include <fstream> using namespace std; int main () { typedef multimap<char,string> M1; typedef M1::value_type v_t1; M1 m1; typedef multimap<string,char,less<string> > M2; typedef M2::value_type v_t2; M2 m2; string word; int counter = 0; ifstream In("/usr/share/dict/words"); if ( In.good() ) { while(1) { getline(In,word); char ch = word.at(0); // file is sorted if ( ch != 'A' && ch != 'a' ) break; else { // for conting of words m1.insert(v_t1(ch,word)); // for upper-lower bound m2.insert(v_t2(word,ch)); } counter++; } In.close(); } cout << "System Dictionary consists " << counter << " words,\nwith first letter 'a' or 'A'" << endl; cout << m1.count('A') << " words start with 'A'" << endl; cout << m1.count('a') << " words start with 'a'" << endl; M2::iterator low = m2.lower_bound("Aba"); M2::iterator upp = m2.upper_bound("Abe"); cout << "Range of the words from 'Aba' to 'Abe':" << endl; while ( low != upp ) { cout << (*low).first << endl; low++; } return 0; } OUTPUT: // System Dictionary consists 3577 words, // with first letter 'a' or 'A' // 491 words start with 'A' // 3086 words start with 'a' // Range of the words from 'Aba' to 'Abe': // Ababa // Abba // Abbott // Abby // Abe value_comp
returns the function that compares values #include <iostream> #include <map> OUTPUT: