constructors

#include <iostream> #include <map> using namespace std; int main () { typedef map<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')); 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 // 2 - B // 3 - C // // m2: // 1 - A // 2 - B // 3 - C // // m3: // 1 - A // 2 - B // 3 - C begin
returns an iterator to the first element #include <iostream> #include <map> using namespace std; int main () { typedef map<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++; } return 0; } OUTPUT: // m: // 3 - C // 2 - B // 1 - A clear
removes all elements #include <iostream> #include <map> OUTPUT: count
returns the number of elements #include <iostream> #include <map> #include <list> #include <numeric> using namespace std; int main () { list<int> L1(3), L2(3); iota(L1.begin(),L1.end(),1); iota(L2.begin(),L2.end(),4); typedef map<int, list<int> > M; M m; m.insert(M::value_type(1,L1)); m.insert(M::value_type(2,L2)); M::iterator It; list<int>::iterator Li; for ( It = m.begin(); It != m.end(); It++ ) { cout << "map " << (*It).first << ": "; for ( Li = It->second.begin(); Li != It->second.end(); Li++ ) cout << *Li << " "; cout << endl; } int n = m.count(2); cout << "count of element with key '2' (0 or 1) is " << n << endl; return 0; } OUTPUT: // map 1: 1 2 3 // map 2: 4 5 6 // count of element with key '2' (0 or 1) is 1 empty
true if the map is empty #include <iostream> #include <map> using namespace std; int main () { typedef map<int,int> M; M m; m[1] = 100; m[3] = 200; m[5] = 300; cout << "values of map 'm': "; M::iterator It = m.begin(); while ( It != m.end() ) { cout << (*It).second << " "; It++; } cout << endl; cout << "size of map = " << m.size() << endl; cout << "map '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 map = " << m.size() << endl; cout << "map 'm' is " << (m.empty() ? "" : "not ") << "empty" << endl; return 0; } OUTPUT: // values of map 'm': 100 200 300 // size of map = 3 // map 'm' is not empty // // After m.erase(m.begin(),m.end()) // size of map = 0 // map 'm' is empty end
returns an iterator to the last element #include <iostream> #include <map> OUTPUT: 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 map<string, int, less<string> > M; void print (M& m) { M::iterator It = m.begin(); cout << "map :" << 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["DDD"] = 4; m["EEE"] = 5; 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: // map : // AAA - 1 // BBB - 2 // CCC - 3 // DDD - 4 // EEE - 5 // map : // AAA - 1 // CCC - 3 // DDD - 4 // EEE - 5 // map : // AAA - 1 // CCC - 3 // EEE - 5 // map : // EEE - 5 find
finds a given element #include <iostream> #include <map> using namespace std; int main () { typedef map<int,char> M; char ch = 'A'; M m; for ( int i=0; i<5; i++ ) m[i] = ch++; M::iterator It = m.begin(); cout << "map 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; return 0; } OUTPUT: // map m: // 0 - A // 1 - B // 2 - C // 3 - D // 4 - E // element key '4' has value E insert
inserts elements into the map #include <iostream> #include <map> #include <string> using namespace std; int main () { typedef map<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[i+1] = ch+i; m2[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 // 6 - F lower_bound
returns an iterator to the first element greater than a certain value #include <iostream> #include <map> #include <ctime> using namespace std; unsigned long int gener_rand() { unsigned long int random = (unsigned long int) (10000.0 * rand() / (RAND_MAX + 1.0 )) % 10; return random; } //----------------------------------- int main () { unsigned long int ary[100]; typedef map<int, unsigned long int> M; M m; // initialize all values to 0 for ( int i=0; i<10; i++ ) m[i] = 0; srand(time(0)); // initialize ary[] with random values for ( int i=0; i<100; i++ ) ary[i] = gener_rand(); for ( int i=0; i<100; i++ ) { if ( i % 10 == 0 && i != 0 ) cout << endl; cout << ary[i] << " "; // generate freaquances m[ary[i]] += 1; } cout << endl << endl; M::iterator It = m.begin(); while ( It != m.end() ) { cout << "number " << (*It).first << " occurred " << (*It).second << " time(s)" << endl; It++; } cout << endl; m[12] = 123; m[15] = 234; m[18] = 345; It = m.lower_bound(11); cout << "lower_bound(11) = " << (*It).first << endl; It = m.upper_bound(11); cout << "upper_bound(11) = " << (*It).first << endl; return 0; } OUTPUT: // 9 7 9 6 9 6 2 0 8 9 // 6 6 6 8 0 9 5 6 5 7 // 2 1 0 3 2 3 4 4 2 2 // 2 1 9 1 8 9 8 0 0 6 // 9 6 3 6 5 3 5 0 0 0 // 8 2 2 8 6 4 2 0 9 4 // 3 1 6 2 3 4 2 1 5 2 // 8 5 8 1 1 3 5 7 4 6 // 7 2 9 0 1 5 4 4 6 4 // 9 9 5 5 2 8 0 4 0 6 // // number 0 occurred 12 time(s) // number 1 occurred 8 time(s) // number 2 occurred 14 time(s) // number 3 occurred 7 time(s) // number 4 occurred 10 time(s) // number 5 occurred 10 time(s) // number 6 occurred 14 time(s) // number 7 occurred 4 time(s) // number 8 occurred 9 time(s) // number 9 occurred 12 time(s) // // lower_bound(11) = 12 // upper_bound(11) = 12 key_comp
returns the function that compares keys #include <iostream> #include <map> OUTPUT: max_size
the maximum number of elements that the map can hold #include <iostream> #include <map> OUTPUT: rbegin
returns a reverse iterator to the end of the map #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 map<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 Shakespeare 000125 // 2 Pushkin 000124 // 1 Shevchenko 000123 rend
returns a reverse iterator to the beginning of the map #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 map<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 Shakespeare 000125 // 2 Pushkin 000124 // 1 Shevchenko 000123 size
the number of elements in the map #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 map<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 << "saze of map '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: // saze of map 'm' = 3 // 1 Shevchenko 000123 // 2 Pushkin 000124 // 3 Shakespeare 000125 swap
exchanges two maps #include <iostream> #include <map> #include <list> #include <numeric> #include <algorithm> using namespace std; typedef map<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 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 << "map m1:" << endl; print(m1); cout << "map m2:" << endl; print(m2); if ( m1 == m2 ) cout << "maps m1 and m2 are equal" << endl; else { cout << endl << "After m1.swap(m2)" << endl; m1.swap(m2); cout << "map m1:" << endl; print(m1); cout << "map m2:" << endl; print(m2); } return 0; } OUTPUT: // map m1: // key : 1; value : 1 2 3 // map m2: // key : 2; value : 1 2 3 // // After m1.swap(m2) // map m1: // key : 2; value : 1 2 3 // map 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 <ctime> using namespace std; unsigned long int gener_rand() { unsigned long int random = (unsigned long int) (10000.0 * rand() / (RAND_MAX + 1.0 )) % 10; return random; } //----------------------------------- int main () { unsigned long int ary[100]; typedef map<int, unsigned long int> M; M m; // initialize all values to 0 for ( int i=0; i<10; i++ ) m[i] = 0; srand(time(0)); // initialize ary[] with random values for ( int i=0; i<100; i++ ) ary[i] = gener_rand(); for ( int i=0; i<100; i++ ) { if ( i % 10 == 0 && i != 0 ) cout << endl; cout << ary[i] << " "; // generate freaquances m[ary[i]] += 1; } cout << endl << endl; M::iterator It = m.begin(); while ( It != m.end() ) { cout << "number " << (*It).first << " occurred " << (*It).second << " time(s)" << endl; It++; } cout << endl; m[12] = 123; m[15] = 234; m[18] = 345; It = m.lower_bound(11); cout << "lower_bound(11) = " << (*It).first << endl; It = m.upper_bound(11); cout << "upper_bound(11) = " << (*It).first << endl; return 0; } OUTPUT: // 9 7 9 6 9 6 2 0 8 9 // 6 6 6 8 0 9 5 6 5 7 // 2 1 0 3 2 3 4 4 2 2 // 2 1 9 1 8 9 8 0 0 6 // 9 6 3 6 5 3 5 0 0 0 // 8 2 2 8 6 4 2 0 9 4 // 3 1 6 2 3 4 2 1 5 2 // 8 5 8 1 1 3 5 7 4 6 // 7 2 9 0 1 5 4 4 6 4 // 9 9 5 5 2 8 0 4 0 6 // // number 0 occurred 12 time(s) // number 1 occurred 8 time(s) // number 2 occurred 14 time(s) // number 3 occurred 7 time(s) // number 4 occurred 10 time(s) // number 5 occurred 10 time(s) // number 6 occurred 14 time(s) // number 7 occurred 4 time(s) // number 8 occurred 9 time(s) // number 9 occurred 12 time(s) // // lower_bound(11) = 12 // upper_bound(11) = 12 value_comp
returns the function that compares values #include <iostream> #include <map> OUTPUT: