60

In C++, what is the simplest way to convert a vector of ints (i.e. vector<int>) to a string ?

jww
  • 97,681
  • 90
  • 411
  • 885
shn
  • 5,116
  • 9
  • 34
  • 62
  • 4
    A 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 Answers4

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