std::list iterator: get next element

前端 未结 6 1757
情书的邮戳
情书的邮戳 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:42

    Yet another possibility:

    #include "infix_iterator.h"
    #include 
    
    typedef std::list > DataItemList;
    
    std::string Compose(DataItemList const &diList) {
        std::ostringstream ret;
        infix_ostream_iterator out(ret, ",");
    
        for (item = diList.begin(); item != diList.end(); ++item)
            *out++ = (*item)->ToString();
        return ret.str();
    }
    

    You can get infix_iterator.h from Google's Usenet archive (or various web sites).

提交回复
热议问题