Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Iterating through a list of pointers  (Read 6647 times)

0 Members and 1 Guest are viewing this topic.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Iterating through a list of pointers
« on: December 23, 2012, 05:20:28 pm »
I have this list:

std::list<ButtonBase*> buttons;

I want to iterate through it.

I have:

std::list<ButtonBase*>::iterator currentButton = buttons.begin();

It however tells me that there is no suitable conversion function for this. This is pretty straight forward, what am I missing?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Iterating through a list of pointers
« Reply #1 on: December 23, 2012, 05:24:31 pm »
What is the exact error message? I'm guessing that you're calling begin() in const method so it can't assign const_iterator to non const one.
Back to C++ gamedev with SFML in May 2023

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Iterating through a list of pointers
« Reply #2 on: December 23, 2012, 05:31:32 pm »
Nevermind, I fixed it. I had some stuff out of order.

pwnstar23

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: Iterating through a list of pointers
« Reply #3 on: December 23, 2012, 11:54:50 pm »
Why a list?  vector would be much better in this situation.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Iterating through a list of pointers
« Reply #4 on: December 23, 2012, 11:59:45 pm »
Why a list?  vector would be much better in this situation.
Well you don't know what all kind of mad things he's doing with his list... ;D

But yeah a std::vector is mostly preferable. So is the use of smart pointers over raw pointers.
std::vector<std::unique_ptr<ButtonBase>>
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Iterating through a list of pointers
« Reply #5 on: December 24, 2012, 04:51:56 am »
Why do you say a vector would be better? List is faster for iterating through than a vector.

Also, I've seen people talk about these stl pointer classes, what are they exactly?

I'm not doing a whole lot with the pointer, its just so i can have a list of all my buttons and check them for being clicked or not, but not all buttons do the same things. Some are on off buttons, some are multistate buttons like on, off, and half, and some are click to do this thing. I made a base virtual class to inherit in the other buttons so I can just have a singular list, which is then encapsulated inside of a handler class so that in the main class of the builder i just have to deal with the handler class which passes info between the main builder class and the buttons. Only way i can think of to update the buttons is the iterate through them all.

The mouse coordinates and state of the mouse buttons get passed to the handler, which then checks it against each button and sets the state of the button accordingly. If the button is used, then the handler will get an enum value from the button which gets passed into an event handler which the main builder class uses to determine what it should do from the button presses. Probably not the easiest way to go about it, but it prevents circular dependency and keeps them pretty loosely coupled I think. Each button, based on its state, figures out what it's sprite should be and the handler generates a list of sprites from each button and then the main builder class gets this list and draws them.
« Last Edit: December 24, 2012, 05:00:04 am by Azaral »

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Iterating through a list of pointers
« Reply #6 on: December 24, 2012, 06:45:54 am »
Quote
Why do you say a vector would be better? List is faster for iterating through than a vector.

I can't imagine a less true statement.  vectors are far faster for iteration than lists.  The only thing that really recommends a list is a large volume of insertions and removals at arbitrary points in the list, with relatively few full-list iterations.   It doesn't seem likely that is the case for you.




eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Iterating through a list of pointers
« Reply #7 on: December 24, 2012, 09:45:10 am »
Why do you say a vector would be better? List is faster for iterating through than a vector.
I don't know from where you got that impression, but as cire said, std::vector are faster for iteration, because they reduce cache misses, since the memory is allocated contiguous. Although for vector of pointers it doesn't matter too much, since only the memory of the pointers are allocated contiguous, but the actual data (i.e. buttons) are 'randomly' distributed in memory.

Also, I've seen people talk about these stl pointer classes, what are they exactly?
They are called smart pointers and are one of the bigger parts of C++11. But some where already introduced with the TR1 and thus many not uptodate compiler, also "support" them already (e.g. VS2010). They should basically replace manual memory management (calling new and delete by hand) and thus drastically reduce the complexity of such, by using RAII (see this thread with example).
Specially std::unique_ptr works kind of like a raw pointer, e.g. you can use
std::unique_ptr<BaseClass> ptr = std::unique_ptr<BaseClass>(new DerivedClass);
The biggest difference being that there can always be just one unique_ptr that holds the class instance and if you want to 'copy' the pointer you'll have to use std::move.
At best you use Google and find some more information, it's quite important stuff, unfortunately there aren't many books yet.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Iterating through a list of pointers
« Reply #8 on: December 24, 2012, 01:10:44 pm »
I've always read people saying lists are faster for going through and vectors are better if you need a specific thing. Or maybe that was just adding and removing. Or maybe I'm just dumb.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Iterating through a list of pointers
« Reply #9 on: December 24, 2012, 01:26:02 pm »
Quote
I've always read people saying lists are faster for going through and vectors are better if you need a specific thing. Or maybe that was just adding and removing. Or maybe I'm just dumb.
Well, in this case stop reading, and make your own opinion ;)

- vector iteration
current++;

// operations involved:
- one addition
- one assignment
-> probably optimized to a single machine instruction

// other things to consider:
- the next element is contiguous to the current one, so it is
  most likely already loaded in the processor's cache

- list iteration
current = current->next;

// operations involved:
- one indirection (resolving 'current->')
- one read ('current->next')
- one assignment

// other things to consider:
- the next element can be anywhere in memory, so it is
  most likely *not* in the processor's cache

Note that this is very approximative, I'm not an expert in low-level machine instructions.
Laurent Gomila - SFML developer

pwnstar23

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: Iterating through a list of pointers
« Reply #10 on: December 24, 2012, 06:27:58 pm »
How about some good hard facts to set the record straight.  This article was featured on isocpp.org

http://www.baptiste-wicht.com/2012/11/cpp-benchmark-vector-vs-list/

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Iterating through a list of pointers
« Reply #11 on: December 24, 2012, 06:47:48 pm »
Interesting article there. I'm going to use vectors from now on. I like vectors better anyways. Seems unless the data being stored is big, vector is the winner.