472,779 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

question about <list> and pointer to its items

Hello

Given this:

list<mystruct_t> lst;

lst.push_back(item1);
lst.push_back(item2);
lst.push_back(item3);

Now say I have this:

mystruct_t *p1, *p2, *p3 pointing to item1->item3 memory in side the list.

If I delete "item2" (or second item) will the other pointers still be valid?

--
Elias
Jul 22 '05 #1
4 1255

"lallous" <la*****@lgwm.org> wrote in message
news:2t*************@uni-berlin.de...
Hello

Given this:

list<mystruct_t> lst;

lst.push_back(item1);
lst.push_back(item2);
lst.push_back(item3);

Now say I have this:

mystruct_t *p1, *p2, *p3 pointing to item1->item3 memory in side the list.

If I delete "item2" (or second item) will the other pointers still be valid?


If you use iterators instead of pointers (like you should) then the C++
standard guarantees that the iterators will remain valid. I don't know that
the standard guarantees that pointers will be OK, but its very likely to
work.

John
Jul 22 '05 #2
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2t*************@uni-berlin.de...

"lallous" <la*****@lgwm.org> wrote in message
news:2t*************@uni-berlin.de...
Hello

Given this:

list<mystruct_t> lst;

lst.push_back(item1);
lst.push_back(item2);
lst.push_back(item3);

Now say I have this:

mystruct_t *p1, *p2, *p3 pointing to item1->item3 memory in side the
list.

If I delete "item2" (or second item) will the other pointers still be

valid?


If you use iterators instead of pointers (like you should) then the C++
standard guarantees that the iterators will remain valid. I don't know
that
the standard guarantees that pointers will be OK, but its very likely to
work.


You mean I change p1, p2, p3 to:
list<mystruct_t>::iterator p1, p2, p3;

then have p1, p2, p3 point to each element, then when any element is deleted
the other iterators are still valid?

--
Elias
Jul 22 '05 #3

"John Harrison" <jo*************@hotmail.com> wrote in message
If you use iterators instead of pointers (like you should) then the C++
standard guarantees that the iterators will remain valid.


I think Standard only talks of iterators and references remaining valid, and
am too lazy to check the Standard :-)

Sharad
Jul 22 '05 #4

"lallous" <la*****@lgwm.org> wrote in message
news:2t*************@uni-berlin.de...
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2t*************@uni-berlin.de...

"lallous" <la*****@lgwm.org> wrote in message
news:2t*************@uni-berlin.de...
Hello

Given this:

list<mystruct_t> lst;

lst.push_back(item1);
lst.push_back(item2);
lst.push_back(item3);

Now say I have this:

mystruct_t *p1, *p2, *p3 pointing to item1->item3 memory in side the
list.

If I delete "item2" (or second item) will the other pointers still be valid?


If you use iterators instead of pointers (like you should) then the C++
standard guarantees that the iterators will remain valid. I don't know
that
the standard guarantees that pointers will be OK, but its very likely to
work.


You mean I change p1, p2, p3 to:
list<mystruct_t>::iterator p1, p2, p3;

then have p1, p2, p3 point to each element, then when any element is

deleted the other iterators are still valid?


Yes.

john
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: G?nter Omer | last post by:
Hi there! I'm just trying to compile a header file (unsing Borland C++ Builder 10)implementing a class, containing the declaration of the STL <list> but it refuses to work. The following...
5
by: Kenneth | last post by:
<list> seems to be a powerful structure to store the related nodes in memory for fast operations, but the examples I found are all related to primitive type storage. I'm doing a project on C++...
2
by: Tom Vogel | last post by:
I'd like to use the XML Documentation Tags to comment my C# code. But many of the tags do not have any effect when I execute the "Build Comment Web Pages" menu. For example, the <list> tag gets...
1
by: Steffo | last post by:
Why can't I use stl list in my dll. I'ts no problem with vector, but when trying list I get the followning error msg: error C2061: syntax error : identifier 'list' Hu!! S.
3
by: kuiyuli | last post by:
I'm using VC++ .Net to do a simlple program. I tried to use <vector> <list> in the program, and I simply put the folowing lines " #include <list> #include <vector> #include <string> using...
4
by: glumaka | last post by:
Hi there 1 This part has no problem with compilation, but further I don't know how to access "item". Pls help ! Thx ! { typedef double tab; std::list<tab> tbBigList; ...
17
by: Cralis | last post by:
I am trying to populate a ListView with a list of 'Models' of cars. I have a data object class for my models, which has a function, 'getListOfModels', which I want to retuyrn a <Listof models. ...
5
by: Deadlocker | last post by:
Hallo, Ive already found an answer : http://www.thescripts.com/forum/thread59807.html But it doesn't work.. here is the error message I get : C:\Dokumente und Einstellungen\piouf.cpp(121)...
12
by: arnuld | last post by:
It works fine. any advice on making it better or if I can improve my C++ coding skills: /* C++ Primer - 4/e * * Chapter 9 - Sequential Containers * exercise 9.18 - STATEMENT * ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.