2012-05-09 15 views
6

Possible Duplicate:
How to convert a number to string and vice versa in C++size_t convert/gettato in stringa in C++

Dow faccio a convertire un valore fondamentale per un string in C++?

Questo è quello che ho provato:

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

E qui è l'errore:

invalid operands of types 'const char*' and 'const char [3]' to binary 'operator+' 
+1

@CodyGray, cercavo per questo, grazie. – chris

+0

la mia prima ipotesi è che tu abbia bisogno di qualcosa, ad es. boost: format ("% lu")% i, ma quale era l'errore? – guinny

+5

'std :: to_string ((int) var)' – refi64

risposta

15

è possibile utilizzare un flusso stringa:

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