Concatenating strings

前端 未结 2 1543
时光取名叫无心
时光取名叫无心 2021-01-06 03:38

I want to join a vector into a single string, separated by spaces. For example,

sample
string
for
this
example


        
相关标签:
2条回答
  • 2021-01-06 03:51

    boost::join

    0 讨论(0)
  • 2021-01-06 03:53
    #include <iterator>
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <algorithm>
    
    std::vector<std::string> v;
    ...
    
    std::stringstream ss;
    std::copy(v.begin(), v.end(), std::ostream_iterator<std::string>(ss, " "));
    std::string result = ss.str();
    if (!result.empty()) {
        result.resize(result.length() - 1); // trim trailing space
    }
    std::cout << result << std::endl;
    
    0 讨论(0)
提交回复
热议问题