954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to convert a vector<int> to vector<string>

Hi

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

mod_motox
Newbie Poster
5 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

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>

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You