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

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

    A little late to the party, but I liked the fact that we can use initializer lists:

    std::string join(std::initializer_list i)
    {
      std::vector v(i);
      std::string res;
      for (const auto &s: v) res += s;
      return res;   
    }
    

    Then you can simply invoke (Python style):

    join({"Hello", "World", "1"})
    

提交回复
热议问题