In C++, what is the simplest way to convert a vector of ints (i.e. vector<int>
) to a string ?
Asked
Active
Viewed 1.9e+01k times
60
-
4A string of what? How do you intend to convert each integer to a string? Do they represent ASCII character codes, or are you trying to build a comma-separated list of numbers? – user229044 Dec 20 '11 at 20:56
-
What do you want the output to look like? – mmmmmm Dec 20 '11 at 20:56
-
Say the vector is: [1, 4, 7, 4, 9, 7], the string will be "1,4,7,4,9,7" – shn Dec 20 '11 at 20:59
4 Answers
89
I usually do it this way...
#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;
}

Drew Chapin
- 7,779
- 5
- 58
- 84
-
5
-
5This is good for display purpose, but can't be used to join with arbitrary delimiter. – mtk Jun 10 '16 at 11:58
-
4Also this makes sense when converting `vector
`. If you use the same with `vector – Marin Shalamanov Feb 11 '21 at 17:00` the values will get converted to char and i assume that's not the intention.
66
Maybe std::ostream_iterator
and 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;
}

Fred Larson
- 60,987
- 18
- 112
- 174
-
13It's worth noting that `vec.end()-1` is only well-defined if one knows _for sure_ that `vec` is non-empty. – ildjarn Dec 20 '11 at 21:10
-
2
-
with this approach, is it possible to force some kind of padding ? for example, to have all numbers use 4 positions, regardless of the number of digits they have – Ciprian Tomoiagă Dec 03 '15 at 05:03
-
1@CiprianTomoiaga: Not directly, it appears. See the answers to this question: http://stackoverflow.com/q/405039/10077 – Fred Larson Dec 03 '15 at 05:15
5
Here is an alternative which uses a custom output iterator. This example behaves correctly for the case of an empty list. This example demonstrates how to create a custom output iterator, similar to 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";
}

Robᵩ
- 163,533
- 20
- 239
- 308
2
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();
}
This is the str function that can make integers turn into a string and not into a char for what the integer represents. Also works for doubles.

kpgbrink
- 21
- 3
-
I was confused his string constructor call with a call to a function named str. Druciferre will not print a separated comma list of numbers, it will try to turn numbers into a char. – kpgbrink Dec 18 '14 at 06:38
-
Sorry the question wasn't specific enough for me to know exactly what he wanted. I did not read the other comments. – kpgbrink Dec 18 '14 at 18:36