开发者

iostream use of << to construct string

How can << be used to construct a string 开发者_开发百科ala

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


using namespace std;    
stringstream ss;
ss << setw(3) << setfill('0') << iCount;
string szB = ss.str();


#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;
}


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜