Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

การจัดเรียงลำดับจากมากไปน้อยใน Map และ Multimap ของ C++ STL

โดยทั่วไป พฤติกรรมเริ่มต้นของ map และ multimap คือการจัดเก็บข้อมูลในลำดับจากน้อยไปมาก (ascending order) แต่เราสามารถจัดเก็บในลำดับจากมากไปน้อย (descending order) ได้โดยใช้ฟังก์ชัน greater

Map แบบเรียงลำดับจากมากไปน้อย

ฟังก์ชันที่ใช้ในตัวอย่าง

  • m::find() – คืนค่า iterator ไปยังองค์ประกอบที่มีคีย์ 'b' ใน map หากพบ มิฉะนั้นคืน iterator ไปยัง end
  • m::erase() – ลบคีย์ออกจาก map
  • m::equal_range() – คืนค่าคู่ของ iterator ที่อ้างถึงช่วงขององค์ประกอบทั้งหมดที่มีคีย์เทียบเท่ากับคีย์ที่กำหนด
  • m::insert() – ใช้สำหรับแทรกองค์ประกอบลงใน map container
  • m::size() – คืนจำนวนองค์ประกอบใน map container
  • m::count() – คืนจำนวนการจับคู่ขององค์ประกอบที่มีคีย์ 'a' หรือ 'f' ใน map

โค้ดตัวอย่าง

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    // ใช้ greater<char> เพื่อจัดเรียงคีย์แบบ descending
    map<char, int, greater<char>> 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 << ":";
        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

Multimap แบบเรียงลำดับจากมากไปน้อย

ฟังก์ชันที่ใช้

  • mm::find() – คืน iterator ไปยังองค์ประกอบที่มีคีย์ 'b' ใน multimap หากพบ มิฉะนั้นคืน iterator ไปยัง end
  • mm::erase() – ลบคีย์ออกจาก multimap
  • mm::equal_range() – คืนค่าคู่ของ iterator ที่อ้างถึงช่วงขององค์ประกอบที่มีคีย์เทียบเท่า
  • mm::insert() – ใช้แทรกองค์ประกอบลงใน multimap container
  • mm::size() – คืนจำนวนองค์ประกอบใน multimap container

โค้ดตัวอย่าง

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    // ใช้ greater<char> สำหรับ multimap
    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 << ":";
        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 << "multimap 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
multimap contains:
b => 40
a => 10
a => 30

สรุป: การใช้ greater<KeyType> เป็นพารามิเตอร์เทมเพลตที่สามของ map/multimap จะทำให้คอนเทนเนอร์จัดเรียงคีย์จากมากไปน้อยโดยอัตโนมัติ ซึ่งมีประโยชน์เมื่อต้องการเข้าถึงข้อมูลที่มีคีย์สูงสุดก่อน