I tried to keep it mostly C-like, with some small uses of the STL.
Maps and Vectors are just too useful to pass over, even if you want to avoid complicated C++ object/template junk.
A few gotchas:
If you add elements to a vector, all pointers within the vector might be invalidated. Do not store pointers to objects stored in vectors.
maps are harder to use than vectors, especially to someone new to the STL.
You could just use a vector (which behaves nearly exactly like an array), and manage the mapping yourself.
Vector 101:
Basic use of vectors involves using them as resizeable arrays:
std::vector<int> my_vector; // creates a vector of "int"s.
std::vector<double> my_vector2; // creates a vector of "double"s.
int vec_size = my_vector.size(); // returns # of elements in the vector.
my_vector.push_back(int); // places "int " onto the back of the vector. Makes the vector 1 larger.
int value = my_vector[index] // accesses the index'd element of the vector. Do not pass in a negative index, or an index larger than .size().
my_vector[index] = 7; // you can use [] much like you do with an array or pointer.
my_vector.resize(int); // changes the size of the vector.
&my_vector.front(); // get a pointer to the vector data. This pionter can be invalidated by resize(), push_back(), or many other methods.
When a vector goes away, it deletes the memory it owns automatically. This makes leaks harder than managing a buffer using malloc() and realloc().
You cannot use {0} notation to initialize a struct containing a std::vector. =/
__________________
Last edited by JHVH : 10-29-4004 BC at 09:00 PM. Reason: Time for a rest.
|