std::list iterator: get next element

Posted by sheepsimulator on Stack Overflow See other posts from Stack Overflow or by sheepsimulator
Published on 2010-04-23T15:12:55Z Indexed on 2010/04/23 16:13 UTC
Read the original article Hit count: 321

Filed under:
|
|
|

I'm trying to build a string using data elements stored in a std::list, where I want commas placed only between the elements (ie, if elements are {A,B,C,D} in list, result string should be "A,B,C,D".

This code does not work:

typedef std::list< shared_ptr<EventDataItem> > DataItemList;
// ...
std::string Compose(DataItemList& dilList)
{
    std::stringstream ssDataSegment;
    for(iterItems = dilList.begin();
        iterItems != dilList.end(); 
        iterItems++)
    {
        // Lookahead in list to see if next element is end
        if((iterItems + 1) == dilList.end())  
        {
            ssDataSegment << (*iterItems)->ToString();
        }
        else
        {
            ssDataSegment << (*iterItems)->ToString() << ",";
        }
    }
    return ssDataSegment.str();
}

How do I get at "the-next-item" in a std::list using an iterator? I would expect that it's a linked-list, why can't I get at the next item?

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl