-
help with STL lists
Have decided to implement the linked lists that come as a part of the standard template library in a program that I am writing, though am a bit confused as to the functions that they use and such.
Anyway, I am using the Dietel and Dietel C++ How to Program book as a starting point, though it only covers them in verry limited detail.
What I need to do is once I have information stored in the list ( this part is working properly due to the stl implementing lists through templates ) is to then be able to access the information both sequentialy ( through use of an iterator ) and non-sequentialy ( buy retrieving the stored data by its element number ).
This I have no idea how to do.
Any help with this would be greately apreciated.
Later,
-
list's don't support random access, ie "by element number" you have to do something like this
Code:
template<class T>
std::list<T>::iterator get_n(std::list<T> &l, int n) {
std::list<T>::iterator it = l.begin();
while(n>0 && it != l.end()) {it++;n--;}
return it;
}
This is painfully slow, in general the main reason you want a list is for "destructive splice" If you want to remove a sequence of elemetns from one container and insert it into the middle of another then list is the way to go. std::vector is probably what you really want.
-
Thanks for your help.
Turns out vectors are what I am after.
Later,