Hi

In my program one of my function returns a vector<int> ... I want to save it in to a vector<string>.
itoa function does not seem to work here. Is there anything that can be done regarding this.
Thanks in Advance.

Recommended Answers

I would imagine you could create a vector of char*. Go through your vector of integers, use itoa on each individual integer to create a string, then push that new string onto your vector. Note that itoa converts to a char*, not a C++-style string. itoa wouldn't work on an …

Jump to Post

All 2 Replies

I would imagine you could create a vector of char*. Go through your vector of integers, use itoa on each individual integer to create a string, then push that new string onto your vector. Note that itoa converts to a char*, not a C++-style string. itoa wouldn't work on an entire vector directly. You have to convert the elements one by one and push them onto the new vector of type char*. If you want a vector of C++-style strings instead, you'd have to then create one from the char* returned by itoa for each element of the integer vector.

Just use the function

string IntToString ( int number )
{
  std::ostringstream oss;

  // Works just like cout
  oss<< number;

  // Return the underlying string
  return oss.str();
}

and include the header file: #include <sstream>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.