C++ 如何将 vector<int> 转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2518979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to transform a vector<int> into a string?
提问by Legend
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).
我正在尝试将值从 C++ 传递给 TCL。由于我不能在不使用一些复杂模块的情况下传递指针,因此我正在考虑将向量转换为 char 数组,然后将其作为空终止字符串传递(这相对容易)。
I have a vector as follows:
我有一个向量如下:
12, 32, 42, 84
which I want to convert into something like:
我想转换成这样的东西:
"12 32 42 48"
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?
我想到的方法是使用迭代器遍历向量,然后将每个整数转换为其字符串表示形式,然后将其添加到 char 数组中(最初通过传递向量的大小动态创建)。这是正确的方法还是有一个功能已经这样做了?
回答by fbrereto
What about:
关于什么:
std::stringstream result;
std::copy(my_vector.begin(), my_vector.end(), std::ostream_iterator<int>(result, " "));
Then you can pass the pointer from result.str().c_str()
然后你可以传递指针 result.str().c_str()
回答by John Dibling
You can use copy
in conjunction with a stringstream
object and the ostream_iterator
adaptor:
您可以copy
与stringstream
对象和ostream_iterator
适配器结合使用:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v;
v.push_back(12);
v.push_back(32);
v.push_back(42);
v.push_back(84);
stringstream ss;
copy( v.begin(), v.end(), ostream_iterator<int>(ss, " "));
string s = ss.str();
s = s.substr(0, s.length()-1); // get rid of the trailing space
cout << "'" << s << "'";
return 0;
}
Output is:
输出是:
'12 32 42 84'
'12 32 42 84'
回答by Jon Benedicto
I'd use a stringstream to build the string. Something like:
我会使用 stringstream 来构建字符串。就像是:
std::vector<int>::const_iterator it;
std::stringstream s;
for( it = vec.begin(); it != vec.end(); ++it )
{
if( it != vec.begin() )
s << " ";
s << *it;
}
// Now use s.str().c_str() to get the null-terminated char pointer.
回答by Bj?rn Pollex
You got it right, but you can use std::ostringstream
to create your char array.
你做对了,但你可以std::ostringstream
用来创建你的字符数组。
#include <sstream>
std::ostringstream StringRepresentation;
for ( vector<int>::iterator it = MyVector.begin(); it != MyVector.end(); it++ ) {
StringRepresentation << *it << " ";
}
const char * CharArray = StringRepresentation.str().c_str();
In this case, CharArray
is only for reading. If you want to modify the values, you will have to copy it. You can simplify this by using Boost.Foreach.
在这种情况下,CharArray
仅用于阅读。如果要修改这些值,则必须复制它。您可以使用Boost.Foreach来简化这一过程。