STL Map - C & C++

less than 1 minute read

STL Maps – Associative Arrays

This is very useful to study associative array in C++. If you have used variety of programming languages, sometimes it could be confusing which to use. Associative Array is different concept compare to C language.

map class is part of the std namespace and to use it:

std::map <key_type, data_type, [comparison_function]>

Unlike C, C++ uses associate strings. For example, char type could be retrieved by strings:

std::map<string, char> gradeList;
gradeList["Sean"] = 'A';

gradeList[“Sean”] = ‘A’; gradeList.erase(“Sean”);


Similar to normal search method: std::map <string, char> gradeList; gradeList[“Sean”] = ‘A’; if(gradeList.find(“Tom”) == gradeList.end()) { std::cout«“Tom is not in the list”«endl; }


Leave a comment