std::vector to string with custom delimiter

前端 未结 9 1281
独厮守ぢ
独厮守ぢ 2020-12-03 16:42

I would like to copy the contents of a vector to one long string with a custom delimiter. So far, I\'ve tried:

// .h
string getLabe         


        
9条回答
  •  独厮守ぢ
    2020-12-03 17:13

    C++11:

    vector x = {"1", "2", "3"};
    string s = std::accumulate(std::begin(x), std::end(x), string(),
                                    [](string &ss, string &s)
                                    {
                                        return ss.empty() ? s : ss + "," + s;
                                    });
    

提交回复
热议问题