c++ basic 1

string:

#include<strlib.h>
getline(cin,str);<span style="white-space:pre">	</span>//get the whole line include the space
realToString()<span style="white-space:pre">		</span>//change real to string
trim();            <span style="white-space:pre">	</span>//return a new string without space;

char:

<cctype>     
    isalpha()
    isupper()
    isdigit()
    isspace()            //'\t'  '\n'  '\f' '\v'  '\r'
    toupper()
    tolower()

stream:

获取文件流

get() put()


获取文件名

int next;
ifstream infile(filename);
while((next=infile.get())!=EOF){
		char nexzc=char(next);
		cout.put(nexzc);
	}

void getFileName(ifstream &infile){
	while(true){
		cout<<"input filename:"<<endl;
		string name;
		getline(cin,name);           //get whole line
		infile.open(name.c_str());   
		if(!infile.fail())return ;	 //如果获取失败
		infile.clear();				 //清除文件流
		cout<<"unable to access the file try again"<<endl;
	}
}

ifstream直接转化为 integer

直接使用文件流读取数据不同于 getline    读取到空格会算作下一次读取

ifstream infile(filename);
if(infile.fail())
        cerr<<"can't open"<<endl;
int score;
while(true){
        infile>>score; 
        if(infile.fail()||!infile.eof())break;
}

getline() cout

ifstream infile;
	infile.open(filename.c_str());
	while(true){
		getline(infile,line);
		if(infile.fail())break;
		cout<<line<<endl;
	}


将string 类型转化为 integer

//string to integer
int sti(string str){
	int value;	
	istringstream stream(str);
	stream>>value>>ws;//读入 value  ws:格式控制 无论后面与多少空格
	if(stream.fail()||stream.eof()){
		cerr<<"illegal integer";
	}
	return value;
}

输入输出: 获取一个整型

int getinteger(void){
	string line;
	int value;
	while(true){
		getline(cin,line);
		istringstream stream(line);
		stream>>value>>ws;
		if(!stream.fail()&&!stream.eof())break;
		cout<<"error: some format is illegal";
	}
	return value;
}

复制流

copystream(infile,cout);

void copystream(istream & is, ostream & os) {
	while (true) {
		int ch = is.get();
		if (ch == EOF) break;
		os.put(ch);
	}
}

小数点转换 四舍五入 利用流
<iomanip>
<sstream>
double digit(double value,int sign){
	stringstream str;
	str<<setprecision(sign)<<value;
	str>>value;
	return value;
}


int 转化为 string

string intToString(int x){
   stringstream ss;
   string str;
   ss<<x;
   ss>>str;
   return str; 
}



部分代码从 programming abstraction in c++ 转载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值