ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน map::get_allocator() ใน C++ STL
แผนที่ใน C++ STL คืออะไร
แผนที่เป็นคอนเทนเนอร์ที่เชื่อมโยงกัน ซึ่งอำนวยความสะดวกในการจัดเก็บองค์ประกอบที่เกิดขึ้นจากการรวมกันของค่าคีย์และค่าที่แมปในลำดับเฉพาะ ในคอนเทนเนอร์แผนที่ ข้อมูลจะถูกจัดเรียงภายในเสมอด้วยความช่วยเหลือของคีย์ที่เกี่ยวข้อง ค่าในคอนเทนเนอร์แผนที่สามารถเข้าถึงได้โดยคีย์เฉพาะของมัน
map::get_allocator() คืออะไร
map::get_allocator( ) เป็นฟังก์ชันที่อยู่ภายใต้ไฟล์ส่วนหัว
ไวยากรณ์
map_name.get_allocator(key_value k);
พารามิเตอร์
ฟังก์ชันนี้ไม่รับพารามิเตอร์
คืนค่า
ส่งคืนวัตถุจัดสรรของแผนที่
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, int> TP;
map<int, int>::allocator_type tp = TP.get_allocator();
cout << "checking Is allocator Pair<int, int> : "<<
boolalpha << (tp == allocator<pair<int, int> >());
return 0;
} ผลลัพธ์
checking Is allocator Pair<int, int> : true
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
int main(void) {
map<char, int> TP;
pair<const char, int>* TP_pair;
TP_pair = TP.get_allocator().allocate(5);
cout<<"Size after allocating is: " << sizeof(*TP_pair) * 5 << endl;
return 0;
} ผลลัพธ์
Size after allocating is: 40