Std list help

I am working with lists to store some data (waht an idea :) ), and have a few things I'm noticing that make me feel like I'm doing it wrong.

first:
1
2
3
4
5
6
7
8
void Effect::printSection13(FileH& out){
	for (std::list<Section13*>::iterator iter = sec13.begin(); iter != sec13.end(); iter++){
		Section13* entry = *iter;  // Why do I need this?
		out.writeData(entry->strLength);
		out.writeData(entry->str, entry->strLength);
		out.writeData(entry->reference);
	}
}


I have to make entry? Shouldn't I be able to just write iter->strLength? (Now that I look at it, maybe *iter-> is needed.

Also, there is no list.at() (or something like it). If my list is a private member variable, then it seems a little stupid to do:
1
2
3
4
Section13* getPointer(unsigned int position){
  unsigned int i = 0;
  // For loop with iterator
  // increiment i and if i == position, return *iter 


Am I on track here? Thanks.
Last edited on
LowestOne You seem to be a person that want its to be right first time I was like that in coding but having error output makes you a better programmer.
Go if your gut instinct if the program seem to be outputting errors try first than seek advice and I am willing to help. Right now brain sleep mode so sorry.
You said the list is to store some data, but it's storing pointers instead. Why not std::list<Section13>?

Yes, if you hold pointers, you'll have to do (*i)->strLength and such.

And yes, there is no list.at(). If you need indexed access, use a container that provides such: vector or deque.
Thanks for the help Cubbi.

Believe me, I have many errors :)

Why pointers? Section13 might be relatively small, but there are about 1,000 of them. Section01 also has about 1000 entries, but this one has about 100 variables. The file is about 1mb of binary, and I don't want this information to be passed around or contiguous.
The file is about 1mb of binary, and I don't want this information to be passed around or contiguous.

Each node of the list is already allocated on heap, only pointers to it are manipulated. That's why inserting/erasing items from the list is a O(1) operation. Nothing is copied. Also, each node is allocated individually, storage of a list is not contiguous (only vector and string use contiguous storage)
Hmm, I guess since I'm storing pointers I don't really have to worry about 1000 of them being contiguous. Does make life a little easier, especially that all I need is a "replace all "list" with "vector"".
If you're worried about stressing the memory allocator by asking for large contiguous storage, but need indexed access, use std::deque<Section13>.
Topic archived. No new replies allowed.