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

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

    With c++11 the stringstream way is not too scary:

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main()
    {
        std::vector v{"Hello, ", " Cruel ", "World!"};
       std::stringstream s;
       std::for_each(begin(v), end(v), [&s](const std::string &elem) { s << elem; } );
       std::cout << s.str();
    }
    

提交回复
热议问题