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

Author Topic: How do you access an individual node within an STL list container?  (Read 2976 times)

0 Members and 1 Guest are viewing this topic.

BobbyT432

  • Newbie
  • *
  • Posts: 8
    • View Profile
I understand that lists don't offer random access [], and you have to use an iterator, but how do you access the properties of that iterator?

For example:
                for (std::list<sf::VertexArray>::iterator it = vertexManager.begin(); it != vertexManager.end(); it++)
                {
                        // DO STUFF HERE
                           
                }
In the //DO STUFF HERE,
  I've tried.... 
- (*it)[0].position
- it[0].position
- it[0]->position
- (*it)[0].position

but I can't seem to access any properties.. thanks!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How do you access an individual node within an STL list container?
« Reply #1 on: April 14, 2020, 08:39:14 am »
*it // the pointed sf::VertexArray&
(*it)[0] // its first sf::Vertex
(*it)[0].position // its position
If it doesn't work then post the compiler error please. "I can't seem to access any property" won't help us to know anything about what's going on.

And...
for (auto& vertexArray : vertexManager)
    ...
Laurent Gomila - SFML developer

BobbyT432

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: How do you access an individual node within an STL list container?
« Reply #2 on: April 15, 2020, 12:08:04 am »
Yeah woops forgot to include that! However I fixed the issue, you need to access it by grabbing the address of it dereferenced.

&*it;

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How do you access an individual node within an STL list container?
« Reply #3 on: April 15, 2020, 12:49:58 am »
The address of a deferenced iterator is just a pointer that is equivalent to the iterator, is it not?

What are you doing with "&*it" that doesn't work with just "it"?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

BobbyT432

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: How do you access an individual node within an STL list container?
« Reply #4 on: April 15, 2020, 01:34:13 am »
Thats what I thought. I was trying to save the individual node within an STL List. I wanted to be able to capture the nodes address and edit it, however just using "it" (even though its a pointer..) did not work. On stack overflow someone said you have to do &*it, which yeah makes no sense to me, but it worked.

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: How do you access an individual node within an STL list container?
« Reply #5 on: April 15, 2020, 06:44:31 am »
The address of a deferenced iterator is just a pointer that is equivalent to the iterator, is it not?

It can be a simple pointer but it doesn't need to be. Some implementations use a class so they add extra functionality in debug mode.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How do you access an individual node within an STL list container?
« Reply #6 on: April 15, 2020, 08:31:53 am »
it is an instance of an iterator class. &*it is the address of the pointed item. Definitely not the same thing, even though an iterator has pointer semantics and is (was?) allowed to be a simple alias to T*.

Whether you store a std::list<T>::iterator or a T* shouldn't matter (ignoring everything else your code might do, of course), because list iterators never get invalidated as long as the item exists in the list.

However it seems like the code you posted doesn't reflect what you're actually doing, and you still didn't post the compiler error. There's no chance we can give you relevant answers in this context.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How do you access an individual node within an STL list container?
« Reply #7 on: April 15, 2020, 08:18:29 pm »
I should clarify that I wasn't saying that an iterator and pointer were the same thing, merely that they are used similarly (as Laurent put it: "an iterator has pointer semantics")

The point was that because they can be used similarly, what are you doing with one that doesn't work with the other?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*