Trong C++, mảng kết hợp (associative array) cho phép chỉ mục (key) là bất kỳ kiểu dữ liệu nào (string, float, char...). Còn gọi là map hoặc dictionary - cấu trúc cặp key-value.
Ví dụ minh họa
#include <bits/stdc++.h>
using namespace std;
int main() {
map<string, int> speed = {
{"ninja", 290},
{"s1000rr", 310},
{"bullet", 127},
{"Duke", 135},
{"R1", 286}
};
cout << "Toc do cao cua cac xe:" << endl;
for (auto i = speed.begin(); i != speed.end(); i++)
cout << i->first << " " << i->second << endl;
cout << endl << "Toc do Bullet: " << speed["bullet"] << endl;
}
Kết quả
Toc do cao cua cac xe: Duke 135 R1 286 Bullet 127 ninja 290 s1000rr 310 Toc do Bullet: 127