News: 11 March 2016 - Forum Rules
Current Moderators - DarkSol, KingMike, MathOnNapkins, Azkadellia, Danke

Author Topic: C++ List Container Question  (Read 5350 times)

RedComet

  • Hero Member
  • *****
  • Posts: 3168
    • View Profile
    • Twilight Translations
C++ List Container Question
« on: August 05, 2011, 10:25:43 pm »
I'm not exactly sure how to explain this, but I'll try. So I'll throw some code up first and then try to explain.

Code: [Select]
list<int> int_list;

for(int i = 0; i < 5; i++)
{
int_list.push_back(i + 100);
}

Is there a way for me to return a pointer to say the third element in the list and then traverse the list either forward or backward? Simply using a list of int pointers won't accomplish what I want, because that will only give me access to the specified element in the list. What I want is to be able to keep track of a list of special values, then if certain conditions are met change those values AND alter the values immediately after them.

So, say I start at the third element in the above. I want to be able to add 20 or something to the third element, the fourth and the fifth if a certain condition is met. I understand I can use an iterator to traverse the *entire* list, but that's not what I want. There's only certain elements I need to access and alter while the rest I want to leave alone.

tl;dr: is there a way to get a list element to return and iterator to the *next* element in the list?
Twilight Translations - More than just Dragonball Z. :P

Kiyoshi Aman

  • RHDN Patreon Supporter!
  • Hero Member
  • *****
  • Posts: 2262
  • Browncoat Captain
    • View Profile
    • Aerdan's Blog
Re: C++ List Container Question
« Reply #1 on: August 06, 2011, 10:59:34 am »
Here, have a doubly-linked list.

It does what you want, and can do it even in reverse.

Nightcrawler

  • Hero Member
  • *****
  • Posts: 5787
    • View Profile
    • Nightcrawler's Translation Corporation
Re: C++ List Container Question
« Reply #2 on: August 06, 2011, 06:03:09 pm »
Can't you just use a vector instead? Then you can have random access to all elements.
TransCorp - Over 20 years of community dedication.
Dual Orb 2, Wozz, Emerald Dragon, Tenshi No Uta, Glory of Heracles IV SFC/SNES Translations

Klarth

  • Sr. Member
  • ****
  • Posts: 495
    • View Profile
Re: C++ List Container Question
« Reply #3 on: August 06, 2011, 06:46:08 pm »
There's only certain elements I need to access and alter while the rest I want to leave alone.

tl;dr: is there a way to get a list element to return and iterator to the *next* element in the list?
Lists by definition have only sequential access.  std::list is doubly linked.  If there's a real reason that you can't use a vector...then use iterators.  This is just some random stuff you can do.

typedef std::list<int>::iterator IntListIt;

for(IntListIt it = yourlist.start(); it != yourlist.end(); it++)
{
    if(NeedsPrevChanged)
    { // Test if prev exists
        IntListIt prev = it--;
        *prev = *prev - 15;
    }
    if(NeedsNextChanged)
    { // Test if next exists...
        IntListIt next = it++;
        *next = *next + 15;
    }
    if(NeedsNewInsertedBeforeThisElem)
    {
       yourlist.insert(it, 25);
    }
}

If you need the third elem of the list, you'd do the following...which is painfully slow once you get into a list of thousands of elements.

IntListIt ThirdElem = yourlist.start();
for(int i = 1; i < 3; i++)
  ThirdElem++;
« Last Edit: August 06, 2011, 10:01:42 pm by Klarth »

RedComet

  • Hero Member
  • *****
  • Posts: 3168
    • View Profile
    • Twilight Translations
Re: C++ List Container Question
« Reply #4 on: August 08, 2011, 10:44:03 am »
I really wanted to be able to keep track of "special" elements in the list as they're added and then process them later after the entire list has been populated. I was considering using either a vector or implementing my own list class, but I wanted to make sure the standard list container couldn't do what I want. I'm lazy and wanted to get more familiar with the standard libraries rather than build everything from scratch like I usually do. :P

Thanks guys. :)
Twilight Translations - More than just Dragonball Z. :P