How to construct a std::string from a std::vector?

前端 未结 8 1073
南旧
南旧 2020-12-23 02:20

I\'d like to build a std::string from a std::vector.

I could use std::stringsteam, but imagine there is a s

8条回答
  •  清歌不尽
    2020-12-23 02:34

    Why not just use operator + to add them together?

    std::string string_from_vector(const std::vector &pieces) {
       return std::accumulate(pieces.begin(), pieces.end(), std::string(""));
    }
    

    std::accumulate uses std::plus under the hood by default, and adding two strings is concatenation in C++, as the operator + is overloaded for std::string.

提交回复
热议问题