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

前端 未结 8 1050
南旧
南旧 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:28

    My personal choice would be the range-based for loop, as in Oktalist's answer.

    Boost also offers a nice solution:

    #include 
    #include 
    #include 
    
    int main() {
    
        std::vector v{"first", "second"};
    
        std::string joined = boost::algorithm::join(v, ", ");
    
        std::cout << joined << std::endl;
    }
    

    This prints:

    first, second

提交回复
热议问题