News:

11 March 2016 - Forum Rules

Main Menu

C++ List Container Question

Started by RedComet, August 05, 2011, 10:25:43 PM

Previous topic - Next topic

RedComet

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.


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


Nightcrawler

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

#3
Quote from: RedComet on August 05, 2011, 10:25:43 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++;

RedComet

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