iostream использует << для построения строки - PullRequest
5 голосов
/ 30 января 2010

Как << можно использовать для построения строки ala </p>

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

Ответы [ 3 ]

7 голосов
/ 30 января 2010
using namespace std;    
stringstream ss;
ss << setw(3) << setfill('0') << iCount;
string szB = ss.str();
4 голосов
/ 30 января 2010
#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 голосов
/ 30 января 2010

Как << можно использовать для построения строки ala </p>

Это не имеет никакого смысла.

Используйте std::ostringstream в C ++, если вы хотите сделать то же самое.

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