【问题标题】:Concatenating strings连接字符串
【发布时间】:2010-10-17 17:56:02
【问题描述】:

我想将一个vector<string> 加入一个string,用空格分隔。例如,

sample
string
for
this
example

应该变成"sample string for this example".
最简单的方法是什么?

【问题讨论】:

    标签: c++ string concatenation


    【解决方案1】:
    #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;
    

    【讨论】:

    【解决方案2】:

    提升::加入

    【讨论】:

    • 没有一个好的例子,boost::join 是一个痛苦的世界。该文档假定您对 Boost Range 库至少有一点熟悉。
    猜你喜欢
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 2015-01-13
    相关资源
    最近更新 更多