當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ map value_comp()用法及代碼示例

std::map::value_comp()是C++ STL中的函數。它返回一個函數對象,該對象比較std::map::value類型的對象。

用法:

value_compare value_comp() const

參數:它不接受任何參數。


返回值:此方法返回一個函數對象,該對象比較std::map::value類型的對象。

時間複雜度:O(1)

以下示例說明了map::value_comp()方法:

示例1:

// C++ program to illustrate 
// map::value_comp() method 
  
#include <iostream> 
#include <map> 
using namespace std; 
  
int main() 
{ 
    map<char, int> m = { 
        { 'a', 1 }, 
        { 'b', 2 }, 
        { 'c', 3 }, 
        { 'd', 4 }, 
        { 'e', 5 }, 
    }; 
  
    auto last = *m.rbegin(); 
    auto i = m.begin(); 
  
    cout << "Map contains "
         << "following elements"
         << endl; 
  
    do { 
  
        cout << i->first 
             << " = "
             << i->second 
             << endl; 
    } while (m.value_comp()(*i++, last)); 
  
    return 0; 
}
輸出:
Map contains following elements
a = 1
b = 2
c = 3
d = 4
e = 5

示例2:

// C++ Program to illustrate 
// map::rbegin() method 
  
#include <iostream> 
#include <map> 
using namespace std; 
  
int main() 
{ 
  
    map<char, char> m = { 
        { 'a', 'A' }, 
        { 'b', 'B' }, 
        { 'c', 'C' }, 
        { 'd', 'D' }, 
        { 'e', 'E' }, 
    }; 
  
    auto last = *m.rbegin(); 
    auto i = m.begin(); 
  
    cout << "Map contains "
         << "following elements"
         << endl; 
  
    do { 
  
        cout << i->first 
             << " = "
             << i->second 
             << endl; 
    } while (m.value_comp()(*i++, last)); 
  
    return 0; 
}
輸出:
Map contains following elements
a = A
b = B
c = C
d = D
e = E


相關用法


注:本文由純淨天空篩選整理自lakshita大神的英文原創作品 map value_comp() in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。