std::list iterator: get next element

前端 未结 6 1769
情书的邮戳
情书的邮戳 2021-02-08 21:59

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 stri

6条回答
  •  自闭症患者
    2021-02-08 22:35

    Another solution is to have the first entry be the special case, instead of the last entry:

    std::string Compose(DataItemList& dilList)
    {
        std::stringstream ssDataSegment;
        for(iterItems = dilList.begin();
            iterItems != dilList.end(); 
            ++iterItems)
        {
            // See if current element is the first
            if(iterItems == dilList.begin())  
            {
                ssDataSegment << (*iterItems)->ToString();
            }
            else
            {
                ssDataSegment << "," << (*iterItems)->ToString();
            }
        }
        return ssDataSegment.str();
    }
    

提交回复
热议问题