12

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+'
user881703
  • 1,111
  • 3
  • 19
  • 38
  • my first guess is that you need something, e.g. boost:format("%lu") % i, but what was the error? – guinny May 09 '12 at 05:00
  • 9
    `std::to_string((int)var)` – kirbyfan64sos Sep 03 '13 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. – sehe Jul 05 '16 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". – 7heo.tk Jul 05 '16 at 14:54

1 Answers1

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
}
dreamlax
  • 93,976
  • 29
  • 161
  • 209