5

How can << be used to construct a string ala

int iCount;
char szB[128];
sprintf (szB,"%03i", iCount);
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
Mike D
  • 2,753
  • 8
  • 44
  • 77

3 Answers3

7
using namespace std;    
stringstream ss;
ss << setw(3) << setfill('0') << iCount;
string szB = ss.str();
Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
3
#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
  • 29,130
  • 4
  • 80
  • 105
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
  • 91,295
  • 49
  • 239
  • 345