Map Associative Container in C++
Associative Container in C++ Simplified Part 1
Forward: In this part of the series, I introduce what is called the map associative container.
By: Chrysanthus Date Published: 24 Aug 2012
There are two types of container classes, called Sequences and Associative Containers. This series deals with Associative Containers. I have written a different series for Sequence Containers, which I suggest you read before reading this one. This series is broken down into divisions. A division has at least one part. The first division concerns the map associative container.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at email@example.com. That is, contact me for the slightest problem you have about what you are reading.
C++ Standard Library
C++ installation comes with a library that has important and common features (ready-made code segments) that you can use, without writing code for the features. This library is called the standard library. The library is divided into categories. These categories are still called libraries. The categories are divided into components. Components are divided into entities. Entities are basic things like data types and functions.
There are many header files for the standard library. You access a feature (entity) through a header file. With a typical C++ installation, you do not need to know where (directory) the standard library or standard libraries are kept; you do not need to even know where the header files for the standard library are kept. With my installation (mingw), all you need to know is the name of the header files. To use the header files, you just need to include it at the top of your code (without indicating the path). You do that using the #include preprocessing directive with the name of the header file alone in angle brackets.
How do you know which feature is in a header file. One way to know that is by reading articles, which are of reference nature like this one (series).
What I give you in this series is a simplified version of what is in the ISO/IEC 14882:2003 C++ specification.
There are other articles (tutorials) I have written in this blog on C++. You need to have read them or articles similar to them before reading this series. The titles of the articles in this blog, which are prerequisite to reading this series are:
- Getting Started with C++
- OOP Basics in C++
- C++ Function Templates
- Dynamic Objects in C++
- Integer and Float Object Types in C++
- Reference in C++
- Assignment Operators in C++
- Basics of Exceptions in C++
- User Types from Fundamental Types in C++
- Pointer to Function in C++
- C++ String Class Basics
- An in-depth Look into C++ Core String
- Exception Classes in C++
- Introduction to C++ Standard Language Support Library
- The Reserved Word const in C++
- Introduction to a Simple C++ Sequence Container
A computer language builds up. There are certain things you have to learn first and then use them to learn higher things. Each of the above titles is either a tutorial or the first tutorial in a series. If it is the first part of a series, then you should have read the whole series. If it is a tutorial standing alone, then you should have read the tutorial. To reach any of the articles, just type the title of the article and my name Chrys in the Search Box of this page and click Search.
Associative Container and Sequence
The main deference between an associative container and a sequence is that an associative container uses what is known as a key instead of an index (number) to access an element. A key is a word of your (user) choice.
Instantiating a map
In simple terms one of the syntax to instantiate a map is,
map<key, T> mapName;
You begin with the reserved word, map. This is followed by angle brackets. In the brackets you have two template arguments. Template arguments are object types and not data identifiers. The first object type is for the key of the map. The second object type is for the values of the elements of the map. The information in the angle brackets form the template arguments. After the angle brackets, you have the name for the instantiated map. The following program illustrates this:
using namespace std;
map<const char*, int> myMap;
Note that the header file, map has been included. You need to include this file in order to instantiate and use map features.
The destructor method for the map is,
There is more to the map. Well, let us end here for this part of the series and continue in the next part.
Related CoursesC++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer A Jack of all Trade Poem