1、数字转字符串
C++ string与int互转
https://www.cnblogs.com/smile233/p/8379802.html
//字符串转数字
string str = "123";
int n = atoi(str.c_str());
str = "123.123";
float n = atof(str.c_str());
//数字转字符串
#include<string>
s=to_string(1.123)
C++输入输出
int tp;
while(cin>>tp){
}
int a[200];
while(cin>>x){
a[++n]=x;
}
#include<iostream>
using namespace std;
int main(){
int a,b;
while(cin>>a>>b)
cout<<a+b<<endl;
return 0;
}
#读入一整行
string str; getline(cin,str);
int main()
{
int n, m;
vector<double> test;
while (cin>>n>>m)//循环多行输入,回车换行,Ctrl+Z可结束循环
{
test.push_back(qiuhe(n, m));
}
for (int i = 0; i < test.size(); ++i)
{
cout << fixed << setprecision(2) << test[i] << endl;
}
return 0;
}
#include<iostream>
#include<string>
#include<cstdio>
using namespace std;
int main(){
string s;
getline(cin, s);
int n = atoi(s.c_str());
string now[n];
for (int i = 0; i < n; i++)
{
getline(cin, now[i]);
}
for (int i = 0; i < n;i++){
cout << now[i] << endl;
}
printf("%.2f", 0.21314);
return 0;
}
常见问题
1、输入vector
#include "stdlib.h"
#include <iostream>
#include<vector>
#include <iomanip>
using namespace std;
int main()
{
int m, n;
//cout << "请输入行和列:";
cin >> m >> n;
//注意下面这一行:vector <int后两个 "> "之间要有空格!否则会被认为是重载 "> > "。
vector<vector<int> > p(m, vector<int>(n));
//cout << "请输入数据:";
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
cin >> p[i][j];
//cout << "输出数据:" << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
cout << setw(3) << p[i][j];
cout << endl;
}
system("pause");
return 0;
}
保留小数
printf("%.2f\n", test[i]);
//cout << fixed << setprecision(2) << test[i] << endl;
%f 对应 float
%lf 对应 double
%Lf 对应 long double
%e以科学计数法显示
%g在%e和%f中择短显示
网友评论