Skip to main content
gamedev.net
🔒 Locked

std::list<> & iterator

Started by V3rt3x 12 replies 650+ views
Original Post
V3rt3x
V3rt3x
Hi all! I have a list of std::string(s) which I want to display only the X last of them. But I have''nt found any better solution than :
  
itor = m_szBuffer.end();
for( int i = 0; i < szCount; i++, itor-- )
   ;
  
to set it at the good position in the linked list (I hope you understand me :/) Furthermore I find this code very poor that the next piece of code is :
  
std::string text = "";
for( i = 0; i < szCount; i++, itor++ )
   text += (*itor);
  
So is there a better solution to adjust my iterator on the wanted string on my list ? I don''t know much about iterators, I hope there is a better way to do that...
Ironside
Ironside
just dereference your iterator...

itor = m_szBuffer.back();
text += (*itor);


I think that's what you want...

front( ) and back( ) return an iterator at the first and last elemetns in the list

begin() and end() return an itiorator before the first and after the last elements respectively.


oh. wait i see what your doing.. if your displaying the last X strings in your list why don't you do something like thiis


    
itor = m_szBuffer.end();
for( int i = 0; i < szCount; i++, itor--)
text = (*itor) + text;






[edited by - Ironside on February 26, 2003 6:02:40 PM]

[edited by - Ironside on February 26, 2003 6:03:22 PM]
civguy
civguy
Ironside, that's inefficient. A lot of temporary strings need to be created.

Standard algorithms can be used, but they aren't any faster. They're a nice way to confuse people:
  std::list<std::string>::reverse_iterator ritor = m_szBuffer.rbegin();
std::advance(ritor, szCount);
std::string text = std::accumulate(ritor.base(), m_szBuffer.end(), std::string());


(damn forums, needed to press 'post' five times to get through..)

[edited by - civguy on February 26, 2003 6:44:26 PM]
V3rt3x
V3rt3x
Oh thanx civguy and anonymous poster! it works perfectly!!!

Ironside
Ironside
Ooh.. advance, that outta help my code all over the place.
Ironside
Ironside
Ooh.. advance, that outta help my code all over the place.
Lorenzo
Lorenzo
quote:
Original post by Ironside

front( ) and back( ) return an iterator at the first and last elemetns in the list

begin() and end() return an itiorator before the first and after the last elements respectively.



That''s wrong! front() and begin() both return an iterator to the first element!


civguy
civguy
quote:
Original post by Lorenzo
That''s wrong! front() and begin() both return an iterator to the first element!
Since when? Why would there be two functions to do the same?

http://www.sgi.com/tech/stl/Vector.html

See what''s said about front()
fizban75
fizban75
front() and back() return *references* to the first and last elements in the list.

begin() returns an *iterator* to the first element in the list. end() returns an *iterator* to just beyond the end of the list.

Ironside
Ironside
The pointer, or first element in the list doesn''t contain a value though, it''s also not counted as part of the lists size.
So you you use begin() you are getting an iterator at the first element in your list. however the content you instert into the list doesn''t start until the following element.

that''s why i said begin() returns an iterator before the first element in the list. Symantics

And yes fizban75 is correct, front and back return *references* my bad.

Doc
Doc
quote:
Original post by Ironside
The pointer, or first element in the list doesn''t contain a value though, it''s also not counted as part of the lists size.
So you you use begin() you are getting an iterator at the first element in your list. however the content you instert into the list doesn''t start until the following element.

that''s why i said begin() returns an iterator before the first element in the list. Symantics



What the hell are you talking about? Of course the first element contains a value (unless the list is empty). begin() returns an iterator to the first element in the list. Where a new element will be inserted into the list given an iterator is a quirk of the insert method, not of what begin gives you.

Peace,
Doc
Ironside
Ironside
fire it up in the debugger and take a look...

list points;

int size = points.size();

set a breakpoint on the above line and look at the points variable in the watch window. There''s a node structure called Head, with members _Next, _Prev, and _Value.


points.push_back(2);

look at points in the watch window again...
the Head element''s _Value member is still garbage..
however if you expand Heads''s _Next member and look at that _Value member you''ll see it''s been initialized to 2.



Shannon Barber
Shannon Barber
You may want to use .rbegin(), .rend(), & reverse iterators if X is small compared to the length of the list.

.rend() returns an iterator prior to the first element - as .end() returns an iterator after the last.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.