multimap key_comp() 在 C++ STL 中
本文將討論 C++ STL 中 multimap::key_comp() 函式的工作原理、語法和示例。
什麼是 C++ STL 中的多重對映?
多重對映是關聯容器,類似於對映容器。它還能方便地儲存由鍵值和對映值組合形成的元素,並按特定順序存放。在多重對映容器中,可以將多個元素與同一個鍵關聯。這些資料透過其關聯的鍵始終在內部進行排序。
什麼是 multimap::key_comp()?
multimap::key_comp() 是 `
語法
Key_compare.key_comp();
引數
此函式不接受任何引數。
返回值
它返回一個比較物件。
輸入
multimap<char, int> newmap; multimap<char, int> :: key_compare cmp = newmap.key_comp(); newmap.insert(make_pair(‘A’, 1)); newmap.insert(make_pair(‘B’, 2)); newmap.insert(make_pair(‘C’, 3));
輸出
A= 1 B= 2 C= 3
示例
#include <iostream> #include <map< using namespace stgd; int main(){ multimap<int, char> mul; multimap<int, char>::key_compare cmp = mul.key_comp(); //inserting elements at given key mul.insert(make_pair(0, 'A')); mul.insert(make_pair(1, 'B')); mul.insert(make_pair(2, 'C')); mul.insert(make_pair(3, 'D')); int a = mul.rbegin()->first; multimap<int, char>::iterator it = mul.begin(); cout<<"Elements at given key is : "<<'\n'; do { cout << it->first << " = " << it->second << '\n'; } while (cmp((*it++).first, a)); return 0; }
輸出
如果執行上述程式碼,它將生成以下輸出 −
Elements at given key is : 0 = A 1 = B 2 = C 3 = D
廣告