ในภาษาโปรแกรม c++ แอสโซซิเอทีฟอาเรย์เป็นอาร์เรย์ชนิดพิเศษที่ค่าดัชนีสามารถเป็นข้อมูลประเภทใดก็ได้ เช่น ถ่าน ทุ่น สตริง เป็นต้น อาร์เรย์ที่เชื่อมโยงเหล่านี้เรียกอีกอย่างว่าแผนที่หรือพจนานุกรม นอกจากนี้ ดัชนียังได้รับชื่ออื่นซึ่งเป็นคีย์และข้อมูลที่เก็บอยู่ที่ตำแหน่งของคีย์คือค่า
ดังนั้น เราสามารถกำหนด associative array เป็นคู่คีย์-ค่าได้
มากำหนดชุดเชื่อมโยงของจักรยานยนต์และความเร็วสูงสุดกัน
Bike top speed Ninja 290 S1000rr 310 Bullet 127 Duke 135 R1 286
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
int main(){
map<string, int> speed{ { "ninja", 290 },
{ "s1000rr", 310 }, { "bullet", 127 },
{ "Duke", 135 }, { "R1", 286 } };
map<string, int>::iterator i;
cout << "The topspeed of bikes are" << endl;
for (i = speed.begin(); i != speed.end(); i++)
cout<<i->first<<" "<<i->second <<endl;
cout << endl;
cout << "The top speed of bullet is "<< speed["bullet"] << endl;
} ผลลัพธ์
The topspeed of bikes are Duke 135 R1 286 Bullet 127 ninja 290 s1000rr 310 The top speed of bullet is 127