Problems with c++? or template list ? or ???

Started by
14 comments, last by Fruny 19 years, 4 months ago
Quote:Original post by joanusdmentia
If he just wanted more features, then sure.


Well i never suggested it because of features, it isn't to do with that.
Advertisement
Quote:Original post by snk_kid
Quote:Original post by joanusdmentia
If he just wanted more features, then sure.


Well i never suggested it because of features, it isn't to do with that.

Sorry, wasn't implying that you did. I was just meaning that deferring to the STL for a linked-list because he can't get a basic implementation working himself isn't a good idea.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Lot of helpful info, and yes I have decided to convert it to the STL. I always wanted to learn this, but never got around to it, so now I go through the painful process :).

I see in the STL list you can retrieve a node based on the contents of the node. Is it possible to retrieve a node based on the position within the list ?

for eg :
let's say i want the 10th element, can I retrieve it by doing something like this :
(*loc_iter + 10) ?

tia
You can't just add ten to the iterator since list does not support random access iterators, but you could do:
std::list<Type>::iterator iterator = myList.begin();std::advance(iterator, 10);doSomething(*iterator);

However, saying that an item will be in the tenth position in a list implies that you know that the structure is fairly static. Wanting to retrieve that item means you want some kind of random access. In these cases a deque or vector may be a better choice.

Enigma
For linked lists, I think you can only move an iterator by one element at a time. If you want a list with random access, you should probably try an STL vector. Vectors are implemented with a resizable array and its members can be accessed through an iterator or by its index. Otherwise, they work very similarly to a linked list.

For example:

void MyFunc() {
vector<int> myList;

myList.push_back(0);
myList.push_back(1);
myList.push_back(100);
myList.push_back(20);

int i = myList[2]; //i == 100
}
Quote:Original post by Toonkides
I see in the STL list you can retrieve a node based on the contents of the node. Is it possible to retrieve a node based on the position within the list ?


Part of the process of learning how to use the STL is to learn in which situation each container class is suitable. If you want random access (get any item you want), you'll want a vector or a deque. A list is strictly bidirectional access (move to the previous or next element).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement