c++实践复习

头文件

#include< iostream >

#include< fstream >

#include< sstream>

#include<stdio.h>

#include< string>

#include< cmath>

#include< alogrithm>

#include< cstdlib>

#include< iomanip>

using namespace std;

字符串格式化

#include< iomanip >

精确到小数点后两位:cout<<fixed<<setprecision(2)<<x<<endl;

设置填充字符和宽度:cout<<setw(2)<<setfill(‘0’)<<x<<endl;

sort()函数

#include< algorithm >

比较函数

bool comp(int a,int b){
	return a>b;
}

bool Less(const Student& s1, const Student& s2)
{
    return s1.name < s2.name; //从小到大排序
}
抛出异常
//抛出异常
throw NegativeNumberException("Invalid argument!");
try {
    语句组
}
catch(异常类型) {
    异常处理代码
}
//自定义异常
class TriangleException{
	private:
		string message;
	public:
		TriangleException(){
			
		}
		TriangleException(string m){
			message=m;
		}
		string what(){
			return message;
		}
};
拷贝构造函数
Student(const Student &s){
    id=s.id;
    Student::count++;
}
重载运算符
bool operator<(Pair p){
			if(first<p.getFirst()||first>=p.getFirst()&&second<p.getSecond()){
				return true;
			}
			else return false;
		}
		//前置 
		Time &operator++(){
			if(second==59){
				second=0;
				if(minute==59){
					minute=0;
					if(hour==23){
						hour=0;
					}else{
						hour++;
					}
				}else{
					minute++;
				}
			}else{
				second++;
			}
			Time ans(hour,minute,second);
			return ans;
		}
		//后置 
		Time operator++(int){
			Time ans(hour,minute,second);
			if(second==59){
				second=0;
				if(minute==59){
					minute=0;
					if(hour==23){
						hour=0;
					}else{
						hour++;
					}
				}else{
					minute++;
				}
			}else{
				second++;
			}
			return ans;
		}

operator int(){
    if(err(hour,minute,second)){
        return hour*60*60+minute*60+second;
    }
} 
//输出
friend ostream& operator <<(ostream& output,Time& t){
    if(t.err(t.hour,t.minute,t.second))
    //printf("%02d:%02d:%02d\n",t.hour,t.minute,t.second);
    cout<<setw(2)<<setfill('0')<<t.hour<<":"<<setw(2)<<setfill('0')<<t.minute<<":"<<setw(2)<<setfill('0')<<t.second<<endl;
}
//输入 
friend std::istream& operator >>(istream& input,Rational& t){

    input>>t.numerator>>t.denominator;	
    return input;
} 
虚函数
class A {	
	public:
		A(int x=0){
			
		}
	    virtual void print(){
	        cout << "print come form class A" << endl;
	    }
	    virtual ~A(){
	    	cout<<"A::~A() called"<<endl;
		}
};

class studentplus :public student
{

public:
	studentplus()
	{
		salary = 500;
	}
	
	studentplus(int n, int a, int s) :student(n,a)						//对于继承类的初始化构造函数有两种方式,一种是内部赋值的方式
	{																	//一种是用父类对象初始化子类对象。
		salary = s;														//这里是内部赋值的方式			
	}
    //调用父类函数
    父类::函数名
};

文件操作
#include<fstream>
//读取文件
std::ifstream inFile;
inFile.open("log.txt");
//输出文件
std::ofstream outFile;
outFile.open("student.txt");
//可输入输出
ifstream fFile;
fFile.open("image.jpg",ios::binary);

int x=0;
//移动游标位置
fFile.seekg(12L);
//读取文件
fFile.read((char *)&x,sizeof(x)/4);


getline(inFile,a)
while(inFile>>a>>b>>c>>d){
    s.insert(c);	
}
inFile.close();
从文件中读取一行,并且分割字符串
#include <sstream>
#include<fstream>
std::ifstream inFile;
inFile.open("gpa.dat");
string a,b,c,d;
while(getline(inFile,a)){
    getline(inFile,b);
    istringstream iss(b);
    while(getline(iss,temp,' ')){
        count++;	
    }
类模板
template <class T>
void Swap(T & x, T & y)
{
    T tmp = x;
    x = y;
    y = tmp;
}
template <class T1,class T2>
class Pair
{
public:
    T1 key;  //关键字
    T2 value;  //值
    Pair(T1 k,T2 v):key(k),value(v) { };
    bool operator < (const Pair<T1,T2> & p) const;
};
template<class T1,class T2>
bool Pair<T1,T2>::operator < (const Pair<T1,T2> & p) const
//Pair的成员函数 operator <
{ //"小"的意思就是关键字小
    return key < p.key;
}
Pair<string,int> student("Tom",19); //实例化出一个类 Pair<string,int>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值