5

How can << be used to construct a string ala

int iCount;
char szB[128];
sprintf (szB,"%03i", iCount);
Khaled Alshaya
  • 91,294
  • 38
  • 174
  • 232
Mike D
  • 2,691
  • 6
  • 41
  • 75

3 Answers3

7
using namespace std;    
stringstream ss;
ss << setw(3) << setfill('0') << iCount;
string szB = ss.str();
Peter Alexander
  • 51,762
  • 11
  • 116
  • 167
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;
}
Sean
  • 28,226
  • 4
  • 76
  • 103
2

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
  • 88,492
  • 46
  • 234
  • 343