获取一行
以换行为准的
#include <string>
string s;
getline(cin, s);
以逗号或空格或什么分隔的值
讲stringstream的文章
http://www.cppblog.com/Sandywin/archive/2007/07/13/27984.html
#include <sstream>
#include <string>
string s;
getline(cin, s);//输入字符串,获取到一行
stringstream ss(s);//初始化字符串流
string tmp;
while (getline(ss, line, ' '))//按空格分割
{
}
转换函数
字符串转数据 可以利用函数
int stoi(const strings str, size_t* pos = 0, int base = 10)
long stol(const strings str, size_t* pos = 0, int base = 10)
float stof(const strings str, size_t* pos = 0)
double stod(const strings str, size_t* pos = 0)
int a=stoi(tmp,0,2);
还可以利用字符串流
string tmp;
stringstream ss(s);
int a;
a<<ss;
输入到末尾
cin是个对象,重载<<运算符有返回值的
while(cin>>str){
}
保留小数
#include <iomanip>//需要头文件
double a=1.2232
cout<<setiosflags(ios::fixed)<<setprecision(2)<<;
//输出 1.22
然后再输出实数类型变量即可以保留2位小数输出了
整体保留
cout<<setiosflags(ios::fixed);
//输出1.2