일반적으로 맵 및 다중 맵 맵의 기본 동작은 요소를 오름차순으로 저장하는 것입니다. 하지만 Greater 함수를 사용하여 요소를 내림차순으로 저장할 수 있습니다.
지도 내림차순:
함수는 여기에서 사용됩니다. -
-
m::find() – 맵에서 키 값 'b'가 있는 요소에 대한 반복자를 발견하면 반환하고, 그렇지 않으면 종료할 반복자를 반환합니다.
-
m::erase() – 맵에서 키 값을 제거합니다.
-
m::equal_range() – 쌍의 반복자를 반환합니다. 쌍은 key와 동일한 키를 가진 컨테이너의 모든 요소를 포함하는 범위의 경계를 나타냅니다.
-
삽입() – 맵 컨테이너에 요소를 삽입합니다.
-
m 크기() – 맵 컨테이너의 요소 수를 반환합니다.
-
수() – 맵에서 키 값이 'a' 또는 'f'인 요소와 일치하는 수를 반환합니다.
예시 코드
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main () {
map<char, int,greater <int>> m;
map<char, int>::iterator it;
m.insert (pair<char, int>('a', 10));
m.insert (pair<char, int>('b', 20));
m.insert (pair<char, int>('c', 30));
m.insert (pair<char, int>('d', 40));
cout<<"Size of the map: "<< m.size() <<endl;
cout << "map contains:\n";
for (it = m.begin(); it != m.end(); ++it)
cout << (*it).first << " => " << (*it).second << '\n';
for (char c = 'a'; c <= 'd'; c++) {
cout << "There are " << m.count(c) << " element(s) with key " << c << ":";
map<char, int>::iterator it;
for (it = m.equal_range(c).first; it != m.equal_range(c).second; ++it)
cout << ' ' << (*it).second;
cout << endl;
}
if (m.count('a'))
cout << "The key a is present\n";
else
cout << "The key a is not present\n";
if (m.count('f'))
cout << "The key f is present\n";
else
cout << "The key f is not present\n";
it = m.find('b');
m.erase (it);
cout<<"Size of the map: "<<m.size()<<endl;
cout << "map contains:\n";
for (it = m.begin(); it != m.end(); ++it)
cout << (*it).first << " => " << (*it).second << '\n';
return 0;
} 출력
Size of the map: 4 map contains: d => 40 c => 30 b => 20 a => 10 There are 1 element(s) with key a: 10 There are 1 element(s) with key b: 20 There are 1 element(s) with key c: 30 There are 1 element(s) with key d: 40 The key a is present The key f is not present Size of the map: 3 map contains: d => 40 c => 30 a => 10
내림차순 멀티맵:
여기서 사용되는 함수:
-
mm::찾기() – 멀티맵에서 키 값이 'b'인 요소에 대한 반복자를 찾으면 반환하고, 그렇지 않으면 종료할 반복자를 반환합니다.
-
mm::erase() – 멀티맵에서 키 값을 제거합니다.
-
mm::equal_range() – 쌍의 반복자를 반환합니다. 쌍은 key와 동일한 키를 가진 컨테이너의 모든 요소를 포함하는 범위의 경계를 나타냅니다.
-
mm 삽입() – 멀티맵 컨테이너에 요소를 삽입합니다.
-
mm 크기() – 멀티맵 컨테이너의 요소 수를 반환합니다.
예시 코드
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main () {
multimap<char, int,greater <char>> mm;
multimap<char, int>::iterator it;
mm.insert (pair<char, int>('a', 10));
mm.insert (pair<char, int>('b', 20));
mm.insert (pair<char, int>('a', 30));
mm.insert (pair<char, int>('b', 40));
cout<<"Size of the multimap: "<< mm.size() <<endl;
cout << "multimap contains:\n";
for (it = mm.begin(); it != mm.end(); ++it)
cout << (*it).first << " => " << (*it).second << '\n';
for (char c = 'a'; c <= 'd'; c++) {
cout << "There are " << mm.count(c) << " elements with key " << c << ":";
map<char, int>::iterator it;
for (it = mm.equal_range(c).first; it != mm.equal_range(c).second; ++it)
cout << ' ' << (*it).second;
cout << endl;
}
if (mm.count('a'))
cout << "The key a is present\n";
else
cout << "The key a is not present\n";
if (mm.count('f'))
cout << "The key f is present\n";
else
cout << "The key f is not present\n";
it = mm.find('b');
mm.erase (it);
cout<<"Size of the multimap: "<<mm.size()<<endl;
cout << "multiap contains:\n";
for (it = mm.begin(); it != mm.end(); ++it)
cout << (*it).first << " => " << (*it).second << '\n';
return 0;
} 출력
Size of the multimap: 4 multimap contains: b => 20 b => 40 a => 10 a => 30 There are 2 elements with key a: 10 30 There are 2 elements with key b: 20 40 There are 0 elements with key c: There are 0 elements with key d: The key a is present The key f is not present Size of the multimap: 3 multiap contains: b => 40 a => 10 a => 30