Printing lists with commas C++

后端 未结 28 1779
时光取名叫无心
时光取名叫无心 2020-11-22 03:33

I know how to do this in other languages, but not C++, which I am forced to use here.

I have a Set of Strings that I\'m printing to out in a list, and they need a co

28条回答
  •  长发绾君心
    2020-11-22 03:51

    Using boost:

    std::string add_str("");
    const std::string sep(",");
    
    for_each(v.begin(), v.end(), add_str += boost::lambda::ret(boost::lambda::_1 + sep));
    

    and you obtain a string containing the vector, comma delimited.

    EDIT: to remove the last comma, just issue:

    add_str = add_str.substr(0, add_str.size()-1);
    

提交回复
热议问题