C4 Engine
C4 Engine API Documentation

class Map

Defined in:  TSMap.h
The Map class encapsulates an associative key-value map.
Definition

template <class type> class Map : public MapBase

Member Functions
Map::GetFirstMapElement Returns the first element in a map.
Map::GetLastMapElement Returns the last element in a map.
Map::Member Returns a boolean value indicating whether a particular object is a member of a map.
Map::Empty Returns a boolean value indicating whether a map is empty.
Map::GetMapElementCount Returns the number of elements in a map.
Map::InsertMapElement Adds an object to a map.
Map::InsertReplaceMapElement Adds an object to a map and replaces an existing object having the same key.
Map::RemoveMapElement Removes a particular element from a map.
Map::RemoveAllMapElements Removes all elements from a map.
Map::PurgeMap Deletes all elements in a map.
Map::FindMapElement Finds an object in a map.
Template Parameters
type The type of the class that can be stored in the map. The class specified by this parameter should inherit directly from the MapElement class using the same template parameter.
Constructor

Map();

Description
The Map class template is a container used to store an associative key-value map of objects. The class type of objects that are to be stored in the map must be a subclass of the MapElement class template using the same template parameters as the Map container.

Upon construction, a Map object is empty. When a Map object is destroyed, all of the members of the map are also destroyed. To avoid deleting the members of a map when a Map object is destroyed, first call the Map::RemoveAllMapElements function to remove all of the map's members.

The class specified by the type template parameter must define a type named KeyType and a function named GetKey that has one of the following two prototypes.
KeyType GetKey(voidconstconst KeyType& GetKey(voidconst;
This function should return the key associated with the object for which it is called. The KeyType type must be capable of being compared to other key values using the < and > operators.

It is possible to iterate over the elements of a map using a range-based for loop. This is illustrated by the following code, where map is a variable of type Map<type>.
for (type *element : map)
{
    ...
}
Overloaded Operators
type *operator [](machine index) const; Returns the element of a map whose zero-based index is index. If index is greater than or equal to the number of elements in the map, then the return value is nullptr.
Base Classes
MapBase Used internally to encapsulate common functionality that is independent of the template parameters.
See Also

MapElement