C++ STL 中的 map::value_comp() 函式
在本文中,我們將討論 C++ STL 中 map::value_comp() 函式的工作原理、語法和示例。
C++ STL 中什麼是 Map?
Map 是關聯容器,便於儲存由鍵值和對映值在特定順序下組合而成的元素。在 map 容器中,資料始終在內部使用關聯鍵進行排序。Map 容器中的值由其唯一鍵訪問。
什麼是 map::value_comp()?
map::value_comp() 是 C++ STL 中的內建函式,其在
它是一種函式指標或函式物件,可比較特定集合中兩個同類型值的比較,如果第一個元素小於容器中的第二個元素,則返回 true,否則返回 false。
語法
Map_name.value_comp(key& k);
引數
此函式不接受任何引數。
返回值
此函式返回關聯集合容器的比較物件。
示例
輸入
map<char, int> newmap; newmap[‘a’] = 1; newmap[‘b’] = 2; newmap[‘c’] = 3; set<int>::value_compare cmp = myset.value_comp()
輸出
1 2 3
示例
#include <iostream> #include <map> using namespace std; int main() { map<char, int> TP = { { 'a', 10 }, { 'b', 20 }, { 'c', 30 }, { 'd', 40 }, { 'e', 50 }, }; auto temp = *TP.rbegin(); auto i = TP.begin(); cout <<"Elements in map are : \n"; do { cout<< i->first << " = " << i->second<< endl; } while (TP.value_comp()(*i++, temp)); return 0; }
輸出
Elements in map are : a = 10 b = 20 c = 30 d = 40 e = 50
廣告