C++中int转为string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //c++中int转为string #include <iostream> #include <string> using namespace std; int main() { int i,j,k; string s( "" ); char *temp = new char ; sprintf (temp, "%d" ,1234); s += string(temp); cout<<s<<endl; cout<<s[0]<< " " <<s[1]<< " " <<s[2]<<endl; while (1); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> #include <string> #include <sstream> using namespace std; int main() { int i,j,k; stringstream s; //包含在sstream头文件里 s<<1234; string ch = s.str(); cout<<ch<<endl; while (1); return 0; } /* 输入输出那些事: cout.width(12); //设置输出宽度为12 cout<<f[i]; 要使 cin.get() 大法还有用,可以配对使用 cin.clear() 与 cin.sync()。 两个函数:cin.clear() 用于清除流的失败状态(如果有);而 cin.sync() 用于清空输入流。 */ |
#include <iostream>
using namespace std;
void main()
{ double values[]={1.23,35.36,653.7,4358.24};
for(int i=0; i<4; i++)
{ cout.width(10);
cout.fill('*');
cout<<values[i]<<'\n';
}
}
输出结果:
******1.23
*****35.36
*****653.7
***4358.24
#include <iostream>
#include <iomanip>
using namespace std;
void main()
{ double values[]={1.23,35.36,653.7,4358.24};
char *names[]={"Zoot","Jimmy","Al","Stan"};
for(int i=0;i<4;i++)
cout<<setw(6)<<names[i]
<<setw(10)<<values[i]
<<endl;
}
输出结果:
Zoot 1.23
Jimmy 35.36
Al 653.7
Stan 4358.24
作者:火星十一郎
本文版权归作者火星十一郎所有,欢迎转载和商用,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步