6.1. Creating a list
Using the default constructor
You can create an empty list with the default constructor. The C version returns a handler on the list.
template <class T> TCODList::TCODList()
TCOD_list_t TCOD_list_new()
Example:
TCODList<int> intList;
TCODList<float> *floatList = new TCODList<float>();
TCOD_list_t intList = TCOD_list_new();
TCOD_list_t floatList = TCOD_list_new();
Duplicating an existing list
You can create a list by duplicating an existing list.
template <class T> TCODList::TCODList(const TCODList &l)
TCOD_list_t TCOD_list_duplicate(TCOD_list_t l)
Parameter | Description |
---|---|
l | Existing list to duplicate. |
Example:
TCODList<int> intList;
intList.push(3);
intList.push(5);
TCODList<int> intList2(intList);
TCOD_list_t intList = TCOD_list_new();
TCOD_list_push(intList,(const void *)3);
TCOD_list_push(intList,(const void *)5);
TCOD_list_t intList2 = TCOD_list_duplicate(intList);
Preallocating memory
You can also create an empty list and pre-allocate memory for elements. Use this if you know the list size and want the memory to fit it perfectly.
template <class T> TCODList::TCODList(int nbElements)
TCOD_list_t TCOD_list_allocate(int nbElements)
Parameter | Description |
---|---|
nbElements | Allocate memory for nbElements. |
Example:
TCODList<int> intList(5);
TCOD_list_t intList = TCOD_list_allocate(5);
Deleting a list
You can delete a list, freeing any allocated resources. Note that deleting the list does not delete it's elements. You have to use clearAndDelete before deleting the list if you want to destroy the elements too.
virtual template <class T> TCODList::~TCODList()
void TCOD_list_delete(TCOD_list_t l)
Parameter | Description |
---|---|
l | In the C version, the list handler, returned by a constructor. |
Example:
TCODList<int> *intList = new TCODList<int>(); // allocate a new empty list
intList->push(5); // the list contains 1 element at position 0, value = 5
delete intList;
TCOD_list_t intList = TCOD_list_new();
TCOD_list_push(intList,(const void *)5);
TCOD_list_delete(intList);