13

Possible Duplicate:
How to convert a number to string and vice versa in C++

How do I convert an integral value to a string in C++?

This is what I've tried:

for(size_t i = 0;vecServiceList.size()>i;i++)
{
    if ( ! vecServiceList[i].empty() )
    {
        string sService = "\t" + i +". "+  vecServiceList[i] ;
    }
}

And here is the error:

invalid operands of types 'const char*' and 'const char [3]' to binary 'operator+'
4
  • my first guess is that you need something, e.g. boost:format("%lu") % i, but what was the error? Commented May 9, 2012 at 5:00
  • 10
    std::to_string((int)var) Commented Sep 3, 2013 at 18:05
  • @7heo.tk I don't see what that has to do with things /at all/. Also, the marked duplicate mentions all integral types perfectly fine, so that's not a problem. Commented Jul 5, 2016 at 14:46
  • @sehe my bad, I found this question searching for size_t safe alternative to atoi. I didn't bother to check if the question was written with "to" or "from". Commented Jul 5, 2016 at 14:54

1 Answer 1

23

You could use a string stream:

#include <sstream>

...


for (size_t i = 0; ... ; ...)
{
    std::stringstream ss;

    ss << "\t" << i << vecServiceList[i];

    std::string sService = ss.str();

    // do what you want with sService
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.