이 기사에서는 C++ STL에서 map::key_comp() 함수의 작동, 구문 및 예제에 대해 논의할 것입니다.
C++ STL의 맵이란 무엇입니까?
맵은 키 값과 매핑된 값의 조합으로 형성된 요소를 특정 순서로 쉽게 저장할 수 있는 연관 컨테이너입니다. 지도 컨테이너에서 데이터는 항상 관련 키를 사용하여 내부적으로 정렬됩니다. 맵 컨테이너의 값은 고유 키로 액세스됩니다.
map::key_comp()란 무엇입니까?
map::key_comp( )는
구문
Key_compare.key_comp();
매개변수
이 함수는 매개변수를 허용하지 않습니다.
반환 값
비교 대상을 반환합니다.
예
입력
map<char, int> newmap; map<char, int> :: key_compare cmp = newmap.key_comp(); newmap[‘a’] = 1; newmap[‘b’] = 2; newmap[‘c’] = 3;
출력
a = 1 b = 2 c = 3
예
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, char> TP;
map<int, char>::key_compare cmp = TP.key_comp();
// Inserting elements
TP[0] = 'a';
TP[1] = 'b';
TP[2] = 'c';
TP[3] = 'd';
cout<<"Elements in the map are : \n";
int val = TP.rbegin()->first;
map<int, char>::iterator i = TP.begin();
do {
cout << i->first << " : " << i->second<<'\n';
} while (cmp((*i++).first, val));
return 0;
} 출력
Elements in the map are: 0 : a 1 : b 2 : c 3 : d
예
#include <bits/stdc++.h>
using namespace std;
int main() {
map<char, int> TP;
map<char, int>::key_compare cmp = TP.key_comp();
// Inserting elements
TP['a'] = 0;
TP['b'] = 1;
TP['c'] = 3;
TP['d'] = 2;
cout<<"Elements in the map are : \n";
char val = TP.rbegin()->first;
map<char, int>::iterator i = TP.begin();
do {
cout << i->first << " : " << i->second<<'\n';
} while (cmp((*i++).first, val));
return 0;
} 출력
Elements in the map are: a : 0 b : 1 c : 3 d : 2