连接字符串 [英] Concatenating strings

查看:141
本文介绍了连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串的向量,我打算将这些字符串连接成一个单独的字符串,用空格分隔。例如,如果我的向量包含值:
sample
string
for
this
example
我想输出为sample string for this示例。

I have a vector of string and I intend to join these strings into a single string, separated by a space. For example, if my vector contains the values: sample string for this example I want the output to be "sample string for this example".

需要您的任何输入,什么是最简单的实现方式?

Need any of your input on what is the simplest way of achieving this?

/ p>

Thanks

推荐答案

#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;

这篇关于连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆