g++ 003.cpp -DCPP98
g++ 003.cpp -DCPP11 -std=c++11
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
int iUuid = 10000;
string strUuid = "";
#ifdef CPP11
cout << "USE CPP11" << endl;
strUuid = std::to_string(iUuid);
#endif
#ifdef CPP98
cout << "USE CPP98" << endl;
stringstream sstream;
sstream << iUuid;
sstream >> strUuid ;
#endif
cout << "strUuid = " << strUuid.c_str() <<endl;
}
这篇博客展示了如何在C++中将整型(int)转换为字符串(string)。在C++98中,使用stringstream进行类型转换;而在C++11中,引入了std::to_string()函数,使得转换更为简便。代码示例中通过预处理宏判断并演示了两种方法的使用。
1万+

被折叠的 条评论
为什么被折叠?



