在C++中,将整数的矢量(即vector<int>
)转换为字符串的最简单方法是什么?将矢量转换为字符串
回答
也许std::ostream_iterator
和std::ostringstream
:
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <iterator>
#include <iostream>
int main()
{
std::vector<int> vec;
vec.push_back(1);
vec.push_back(4);
vec.push_back(7);
vec.push_back(4);
vec.push_back(9);
vec.push_back(7);
std::ostringstream oss;
if (!vec.empty())
{
// Convert all but the last element to avoid a trailing ","
std::copy(vec.begin(), vec.end()-1,
std::ostream_iterator<int>(oss, ","));
// Now add the last element with no delimiter
oss << vec.back();
}
std::cout << oss.str() << std::endl;
}
值得注意的是'vec.end() - 1 '只有在确实知道'vec'非空时才有明确的定义。 – ildjarn 2011-12-20 21:10:46
@ildjarn:好点。 'vec.back()'相同。 – 2011-12-20 21:11:30
用这种方法,是否可以强制某种填充?例如,让所有的号码都使用4个位置,不管他们有多少位数 – 2015-12-03 05:03:10
这里是使用定制的输出迭代的替代方案。这个例子对于空列表来说行为正确。此示例演示如何创建自定义输出迭代器,类似于std::ostream_iterator
。
#include <iterator>
#include <vector>
#include <iostream>
#include <sstream>
struct CommaIterator
:
public std::iterator<std::output_iterator_tag, void, void, void, void>
{
std::ostream *os;
std::string comma;
bool first;
CommaIterator(std::ostream& os, const std::string& comma)
:
os(&os), comma(comma), first(true)
{
}
CommaIterator& operator++() { return *this; }
CommaIterator& operator++(int) { return *this; }
CommaIterator& operator*() { return *this; }
template <class T>
CommaIterator& operator=(const T& t) {
if(first)
first = false;
else
*os << comma;
*os << t;
return *this;
}
};
int main() {
// The vector to convert
std::vector<int> v(3,3);
// Convert vector to string
std::ostringstream oss;
std::copy(v.begin(), v.end(), CommaIterator(oss, ","));
std::string result = oss.str();
const char *c_result = result.c_str();
// Display the result;
std::cout << c_result << "\n";
}
我通常做这种方式...
#include <string>
#include <vector>
int main(int argc, char* argv[])
{
std::vector<char> vec;
//... do something with vec
std::string str(vec.begin(), vec.end());
//... do something with str
return 0;
}
template<typename T>
string str(T begin, T end)
{
stringstream ss;
bool first = true;
for (; begin != end; begin++)
{
if (!first)
ss << ", ";
ss << *begin;
first = false;
}
return ss.str();
}
这是STR功能,可以使整数变成一个字符串,而不是到什么整数代表一个字符。也适用于双打。
- 1. vhdl:将矢量转换为字符串
- 2. 字符串转换为数字矢量
- 3. 如何将带有字符串的矢量转换为数字?
- 4. 将矢量转换为字符串并拆分为JSP的字符串
- 5. 将字符串[]转换为矢量<String>
- 6. 如何将矢量转换为java中的字符串?
- 7. 将矢量转换为c中的字符串
- 8. 将字符串数组转换为矢量的最佳方法?
- 9. 如何将字符串转换矢量字符数组
- 10. 将1D矢量转换为2D矢量
- 11. 将数字矢量转换为其他数字矢量
- 12. 将字符串转换为变量
- 13. 将字符串转换为变量
- 14. 将字符串转换为python数量
- 15. 将字符串变量转换为长
- 16. 将字符串转换为变量
- 17. Ruby:将常量转换为字符串
- 18. 将csv转换为字符串变量
- 19. 将字符串转换为字符串
- 20. 将位图转换为无符号字符的矢量
- 21. 将字符串转换为
- 22. 将字符串转换为“_”
- 23. 将字符串转换为?
- 24. C:将常量字符串转换为常规字符串
- 25. [R字符串转换为记号化使用,以矢量“”
- 26. 从矢量< char >转换为字符串
- 27. 分解一个字符串,然后转换为矢量?
- 28. 将未知大小的矢量<矢量<string>>转换为字符
- 29. 将字符串转换为字符
- 30. 将字符串转换为字符
一串什么?你打算如何将每个整数转换为字符串?他们代表ASCII字符代码,还是你想要建立一个以逗号分隔的数字列表? – meagar 2011-12-20 20:56:23
你想要输出看起来像什么? – Mark 2011-12-20 20:56:36
说向量是:[1,4,7,4,9,7],字符串将是“1,4,7,4,9,7” – shn 2011-12-20 20:59:23