2010-01-30 8 views
4

Comment peut < < être utilisé pour construire une chaîne alautilisation iostream de << pour construire la chaîne

int iCount; 
char szB[128]; 
sprintf (szB,"%03i", iCount); 
+0

Vous pouvez être intéressé par Boost.Format: http://www.boost.org/doc/libs/1_41_0/libs/format/index.html –

+0

Voir: http: // stackoverflow. com/questions/119098/qui-bibliothèque-do-vous-utilisez-dans-votre-c-code/119194 # 119194 –

Répondre

6
using namespace std;  
stringstream ss; 
ss << setw(3) << setfill('0') << iCount; 
string szB = ss.str(); 
4
#include <iostream> 
#include <sstream> 
#include <iomanip> 
#include <string> 

using namespace std; 

int main() { 
    int iCount = 42; 
    ostringstream buf; 
    buf << setw(3) << setfill('0') << iCount; 
    string s = buf.str(); 
    cout << s; 
} 
2

Comment peut < < être utilisé pour construire une chaîne ala

Cela n'a aucun sens. Utilisez std::ostringstream en C++ si vous voulez faire la même chose.

std::ostringstream s; 
int x=<some_value>; 
s<< std::setw(3) << std::setfill('0') <<x; 
std::string k=s.str();