Angelscript addon Template Containers  2.31.0
STL container powerr for Angelscript
Iterators

Iterators are named "CONTAINERNAME_iterator". Iteration is done with only one function for better runtime performance. The iterators' constructor takes a handle to the host container. Iterators can also be constructed with "container::begin()" and "container::end()".

Iterator script interface:

template<typename T> class vector_iterator{
//constructor
vector_iterator(vector<T>@ host);
//copy constructor
vector_iterator(const vector_iterator &in other);
T& current();
//property accessor for current value
T get_value() const;
void set_value(T value);
bool next();
bool opPreInc();//++operator, same thing as next, but with 71% less keypresses and 100% more operator overloading swag
bool opPostInc();//operator++, same thing as next, but with 71% less keypresses and 100% more operator overloading swag
bool opEquals(const vector_iterator &in other);
//check if this iterator equals it's container's end() without building any AS objects
bool IsEnd();
/*
Ensure that this iterator's host container has not been modified during iteration.
Using an invalid iterator will cause an Angelscript exception.
if aatc_CONFIG_ENABLE_ERRORCHECK_ITERATOR_SAFETY_VERSION_NUMBERS is not set to 1, this will always return true
*/
bool IsValid();
};

The "next" method evaluates iteration ending and advances the iterator.

The "map" and "unordered_map" containers' iterators can access both the key and value with the same iterator. Their iterators dont have a "current" method, instead they have "current_key" and "current_value". "current_key" returns a const reference to preserve the container's ordering.


Examples

Example of iterating through a vector, neatest syntax:

vector<int> cont;
cont.push_back(1);
cont.push_back(2);
cont.push_back(3);
for(auto it = cont.begin(); it++;){
Print("content = " + it.value);
}

Example of iterating through a vector, clunkiest syntax:

vector<int> cont;
cont.push_back(1);
cont.push_back(2);
cont.push_back(3);
for(vector_iterator<int> it(@cont); it++;){
Print("content = "+it.current());
}

Both print:

content = 1
content = 2
content = 3

Note how the third parameter of the for loop is empty.


Example of iterating through a map:

map<int,string> cont;
cont.insert(2,"bb");
cont.insert(1,"aa");
cont.insert(3,"cc");
for(auto it = cont.begin(); it++;){
Print("content = "+it.key+" , "+it.value);
}

Prints:

content = 1 , aa
content = 2 , bb
content = 3 , cc