Is there a way to implement analog of Python's 'separator'.join() in C++?

后端 未结 6 623
傲寒
傲寒 2021-02-07 03:44

All I\'ve found is boost::algorithm::string::join. However, it seems like overkill to use Boost only for join. So maybe there are some time-tested recipes?

<
6条回答
  •  天涯浪人
    2021-02-07 04:11

    simply, where the type in the container is an int:

    std::string s = std::accumulate(++v.begin(), v.end(), std::to_string(v[0]),
                         [](const std::string& a, int b){
                               return a + ", " + std::to_string(b);
                         });
    

提交回复
热议问题