tags:

views:

102

answers:

3

How can << be used to construct a string ala

int iCount;
char szB[128];
sprintf (szB,"%03i", iCount);
+4  A: 
#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;
}
Sean
+5  A: 

How can << be used to construct a string ala

This doesn't make any sense.

Use std::ostringstream in C++ if you want to do the similar thing.

 std::ostringstream s;
 int x=<some_value>;
 s<< std::setw(3) << std::setfill('0') <<x;
 std::string k=s.str();
Prasoon Saurav
+3  A: 
using namespace std;    
stringstream ss;
ss << setw(3) << setfill('0') << iCount;
string szB = ss.str();
Peter Alexander