map/multimap都是以key/value對的方式存儲數據的,經過key便可查找到對應的value,兩者區別,前者的key不能夠重複,後者能夠重複。
set/multiset每一個元素只是以一個實值存在,前者不容許重複,後者容許重複。函數
void MapDefine() { // 定義空對象 typedef pair<int, char> int_pair; pair<map<int,char>::iterator, char> m_iter; map<int, char> mp; mp.insert(int_pair(1, 'a')); mp.insert(int_pair(2, 'b'));// insert返回值是一個pair<iterator,bool>類型,iterator是對應map的迭代器,bool爲true則插入成功,反之插入失敗 m_iter = mp.insert(int_pair(2, 'c'));//返回false,下面輸出還是1 a, 2 b for_each(mp.begin(), mp.end(), fun);//輸出結果會默認排序的 cout << endl; // 用另外一個對象定義 map<int, char> mp1(mp); for_each(mp1.begin(), mp1.end(), fun); cout << endl; // 經過另外一個對象的一段數據定義 map<int, char> mp2(mp1.begin(), mp1.end()); for_each(mp2.begin(), mp2.end(),fun); cout << endl; }
void MapProperty() { typedef pair<int, char> i_pair; pair<map<int, char>::iterator, bool> m_iter; map<int, char> mp1; mp1.insert(i_pair(111, 'a')); mp1.insert(i_pair(222, 'b')); mp1.insert(i_pair(333, 'c')); mp1.insert(i_pair(111, 'w'));// 沒插入進去,返回的pair<iterator,bool> 中的bool爲0 for_each(mp1.begin(), mp1.end(), fun); // size()/max_size() cout << mp1.size() << endl;// 輸出3,元素個數 cout << mp1.max_size() << endl; // 根據內存清空輸出,可以存儲的最大元素個數 // empty() cout << mp1.empty() << endl;//輸出0, 不爲空 // rbegin()/rend() cout << "-------------" << endl; map<int, char>::reverse_iterator riter; riter = mp1.rbegin(); for (riter; riter != mp1.rend(); ++riter) { cout << riter->second << endl;// 輸出c b a } // count()/find() cout << mp1.count(111) << endl;//輸出1,返回map中指定key的個數,map和set返回不是1就是0,由於不能重複元素,mutimap和mutiset就不一樣了 cout << (mp1.find(222))->second << endl;//輸出b, find返回的是iterator,沒找到則返回.end() // key_comp()/value_comp() cout << "-------------" << endl; map<int, char>::key_compare kc = mp1.key_comp();//返回一個比較key的函數,判斷左操做數是否小於右操做數 map<int, char>::value_compare vc = mp1.value_comp();// 這裏需注意,vc必須賦值,不然報錯,但上面的kc不會 map<int, char>::iterator f_iter = mp1.begin();//f_iter指向mp1首元素 map<int, char>::iterator s_iter = --(--(mp1.end()));//s_iter指向mp1第二個元素 cout << kc(1, 3) << endl;// 輸出1 cout << f_iter->second << " " << s_iter->second << endl;// 輸出a b cout << vc(*f_iter,*s_iter) << endl;//輸出1,若是f_iter對應的key在s_iter對應的key前面,返回true // upper_bound()/lower_bound() cout << "-------------" << endl; map<int, char>::iterator test_iter1; map<int, char>::iterator test_iter2; test_iter1 = mp1.upper_bound(222); test_iter2 = mp1.lower_bound(222); cout << test_iter1->second << endl;// 輸出c,upper_bound返回一個迭代器,指向map中鍵值>key的第一個元素 cout << test_iter2->second << endl;// 輸出b,lower_bound返回一個迭代器,指向map中鍵值>=key的第一個元素。 }
void MapMethods() { typedef pair<int, char> i_pair; map<int, char> mp1,mp2; mp1.insert(i_pair(11, 'a')); mp1.insert(i_pair(22, 'b')); mp1.insert(i_pair(33, 'c')); mp1.insert(i_pair(44, 'd')); for_each(mp1.begin(), mp1.end(), fun);// a b c d /* void erase( iterator pos ); void erase( iterator start, iterator end ); size_type erase( const KEY_TYPE &key ); */ cout << "--------------" << endl; // erase() mp1.erase(mp1.begin()++); for_each(mp1.begin(), mp1.end(), fun);// 輸出 b c d // swap() cout << "--------------" << endl; mp2.insert(i_pair(11, 'w')); mp2.insert(i_pair(22, 'x')); mp2.insert(i_pair(33, 'y')); mp2.insert(i_pair(44, 'z')); mp1.swap(mp2); for_each(mp1.begin(), mp1.end(), fun); // clear() cout << "--------------" << endl; mp1.clear(); cout << mp1.size() << endl; }
void MapEqualRange() { typedef pair<int, char> i_pair; multimap<int, char> mp1; mp1.insert(i_pair(11, 'a')); mp1.insert(i_pair(11, 'b')); mp1.insert(i_pair(22, 'c')); mp1.insert(i_pair(11, 'o')); mp1.insert(i_pair(11, 'p')); mp1.insert(i_pair(11, 'q')); mp1.insert(i_pair(33, 'w')); mp1.insert(i_pair(44, 'y')); for_each(mp1.begin(), mp1.end(), fun);// 會按照key自動排序輸出 cout << "------------" << endl; // equal_range() /* 理解equal_range:一個multimap中(存在重複key),全部元素的區間爲[first,last],因全部元素會自動按照key排序,因此存在一個區間[i,j], 區間內的全部key必然相同(對應的value可能不一樣),lower_bound()返回的iterator指向所查找key的第一個元素(i的位置),upper_bound()返回 的是所查找key最後一個元素的下一個位置(j+1),所以以下會輸出a c。而equal_range()方法則以pair的方式把兩個iterator返回。 */ pair<map<int, char>::iterator, map<int, char>::iterator> p; p = mp1.equal_range(11); cout << (p.first)->second << " " << (p.second)->second << endl;// 輸出a c cout << (mp1.lower_bound(11))->second <<" "<< (mp1.upper_bound(11))->second << endl;// 輸出a c }