头文件
#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>