如何变换向量< int>成字符串? [英] How to transform a vector<int> into a string?

查看:104
本文介绍了如何变换向量< int>成字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图传递一个值从C ++到TCL。因为我不能传递指针而不使用一些复杂的模块,我想到将一个向量转换为一个char数组,然后将其作为一个null终止的字符串(这是相对容易)。

I am trying to pass a value from C++ to TCL. As I cannot pass pointers without the use of some complicated modules, I was thinking of converting a vector to a char array and then passing this as a null terminated string (which is relatively easy).

我有一个向量如下:

12, 32, 42, 84  

我想转换成类似:

"12 32 42 48"

我想到的方法是使用迭代器迭代通过向量,然后将每个整数转换为其字符串表示,然后将其添加到一个char数组(这是动态创建的,通过传递向量的大小)。

The approach I am thinking of is to use an iterator to iterate through the vector and then convert each integer into its string representation and then add it into a char array (which is dynamically created initially by passing the size of the vector). Is this the right way or is there a function that already does this?

推荐答案

这是什么:

std::stringstream result;
std::copy(my_vector.begin(), my_vector.end(), std::ostream_iterator<int>(result, " "));

然后你可以传递 result.str )

这篇关于如何变换向量&lt; int&gt;成字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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