Concatenating strings

前端 未结 2 1558
时光取名叫无心
时光取名叫无心 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:53

    #include 
    #include 
    #include 
    #include 
    #include 
    
    std::vector v;
    ...
    
    std::stringstream ss;
    std::copy(v.begin(), v.end(), std::ostream_iterator(ss, " "));
    std::string result = ss.str();
    if (!result.empty()) {
        result.resize(result.length() - 1); // trim trailing space
    }
    std::cout << result << std::endl;
    

提交回复
热议问题