Pointer to the penultimate element in a list

A

Adam Hartshorne

Hi All,

I want to set a pointer to the penultimate element in a std::list, what
is the best way of doing this? Also would setting a pointer to the end
of the list, and then adding another element, also result in a valid
pointer to the then penultimate element of the the list? (i.e. achieving
basically the same thing)

I ask this because I know there is problems with setting pointers with
std::vector then adding elements to the vector. From my experience this
scenario results in an invalid pointer, but I have in my mind that lists
do not suffer the same problem.

Adam
 
V

Victor Bazarov

Adam said:
I want to set a pointer to the penultimate element in a std::list, what
is the best way of doing this?

Also would setting a pointer to the end
of the list

I presume you mean "to the last element of the list"...
, and then adding another element, also result in a valid
pointer to the then penultimate element of the the list? (i.e. achieving
basically the same thing)
Yes.

I ask this because I know there is problems with setting pointers with
std::vector then adding elements to the vector. From my experience this
scenario results in an invalid pointer, but I have in my mind that lists
do not suffer the same problem.

For a list the pointer remains valid. For a vector the pointer is
invalidated only if reallocation occurs, you cannot predict it, but you
can try to avoid it.

V
 
C

Chris \( Val \)

| Hi All,
|
| I want to set a pointer to the penultimate element in a std::list, what
| is the best way of doing this? Also would setting a pointer to the end
| of the list, and then adding another element, also result in a valid
| pointer to the then penultimate element of the the list? (i.e. achieving
| basically the same thing)
|
| I ask this because I know there is problems with setting pointers with
| std::vector then adding elements to the vector. From my experience this
| scenario results in an invalid pointer, but I have in my mind that lists
| do not suffer the same problem.

How about something like this ?:

typedef std::list<int> IntList;
IntList::reverse_iterator GetPenultimate( IntList& L )
{
return (L.rbegin() != L.rend()) ? ++L.rbegin() : L.rend();
}

Also look up 'std::advance()'

Cheers,
Chris Val
 
S

Stephen Howe

I want to set a pointer to the penultimate element in a std::list, what is
the best way of doing this?

If you actually want the last real element in a list (ie the one before
end()), then
back() returns a reference to it (vector and deque also have back() as a
member).
So with

list <int > l;

int * pi = &l.back();

assuming that l does have some elements in it.
Also would setting a pointer to the end of the list, and then adding
another element, also result in a valid pointer to the then penultimate
element of the the list? (i.e. achieving basically the same thing)
No.

I ask this because I know there is problems with setting pointers with
std::vector then adding elements to the vector. From my experience this
scenario results in an invalid pointer, but I have in my mind that lists
do not suffer the same problem.

Store indexes rather than pointers. At any time you can convert an index to
a pointer.
&v[index] is your pointer.
This way, even if vector reallocates internally, the computed pointer is
still valid (providing index is valid).

Stephen Howe
 
O

Old Wolf

Victor said:
For a list the pointer remains valid. For a vector the pointer is
invalidated only if reallocation occurs, you cannot predict it, but
you can try to avoid it.

Vector reallocation won't occur unless you add elements beyond
the capacity(), right? This seems predictable.
 
V

Victor Bazarov

Old said:
Vector reallocation won't occur unless you add elements beyond
the capacity(), right? This seems predictable.

True. So, if there were a mechanism allowing you to be warned when
allocation is about to happen, you'd know about it. Alternatively,
you can poll the vector's capacity every time you (or somebody else
in their code where you passed your vector) is about to insert a new
element...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top