1、数据层次
位 bit
字节 byte
域/记录
将所有记录顺序地写入一个文件---->顺序文件:一个有限字符构成的顺序字符流
C++标准库中:ifsteam,ofstream,fstream三个类
2、文件操作
打开文件---->读/写文件---->关闭文件
class CSstudent{public: char szName[20]; int nScore;};int main(){ CSstudent s; ofstream OutFile("a.dat",ios::out|ios::binary); while(cin>>s.szName>>s.nScore){ if(strcmp(s.szName,"exit")==0) break; OutFile.write((char*)&s, sizeof(s)); } OutFile.close(); ifstream InFile("a.dat",ios::in|ios::binary); if(!InFile){ cout<<"Error"<
改变程序中学生名字:
int main(){ CStudent s; fstream iofile( “c:\\tmp\\students.dat”, ios::in|ios::out|ios::binary); if(!iofile) { cout << "error" ; return 0; } iofile.seekp( 2 * sizeof(s), ios::beg); //定位写指针到第三个记录 iofile.write( “Mike”, strlen("Mike")+1); iofile.seekg(0, ios::beg); //定位读指针到开头 while( iofile.read( (char* ) & s, sizeof(s)) ) cout << s.szName << " " << s.nScore << endl; iofile.close(); return 0;}
文件拷贝:
//用法示例://mycopy src.dat dest.dat//即将 src.dat 拷贝到 dest.dat//如果 dest.dat 原来就有, 则原来的文件会被覆盖#include#include using namespace std;int main(int argc, char * argv[]){ if(argc != 3) { cout << "File name missing!" << endl; return 0; } ifstream inFile(argv[1], ios::binary|ios::in); //打开文件用于读 if(! inFile) { cout << “Source file open error.” << endl; return 0; } ofstream outFile(argv[2], ios::binary|ios::out); //打开文件用于写 if(!outFile) { cout << “New file open error.” << endl; inFile.close(); //打开的文件一定要关闭 return 0; } char c; while(inFile.get(c)) //每次读取一个字符 outFile.put(c); //每次写入一个字符 outFile.close(); inFile.close(); return 0;}
3、函数模板
(1)泛型程序设计
算法实现时不指定具体要操作的数据的类型,适用于多种数据结构,以减少重复代码的编写。
使用模板:函数模板+类模板
(2)函数模板
template返回值类型 模板名 (形参表){函数体}
templatevoid Swap(T & x, T & y){ T tmp = x; x = y; y = tmp; }int main(){ int n = 1, m = 2; Swap(n, m); //编译器自动生成 void Swap(int &, int &)函数 double f = 1.2, g = 2.3; Swap(f, g); //编译器自动生成 void Swap(double &, double &)函数 return 0;}
templateT2 print(T1 arg1, T2 arg2){ cout<< arg1 << " "<< arg2<
函数模板也可以重载,只要他们的形参表不同即可。例如:
templatevoid print(T1 arg1, T2 arg2){ cout<< arg1 << " "<< arg2< void print(T arg1, T arg2){ cout<< arg1 << " "<< arg2<
有函数模板的情况下,C++编译器遵循以下优先原则:
Step 1: 先找参数完全匹配的普通函数(非由模板实例化而得的函数)
Step 2: 再找参数完全匹配的模板函数
Step 3: 再找实参经过自动类型转换后能够匹配的普通函数
Step 4: 上面的都找不到, 则报错
Note:赋值兼容原则引起函数模板中类型参数的二义性
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
templateT myFunction(T arg1, T arg2){ cout< <<“ ”< <<“\n”; return arg1;}…myFunction(5, 7); //ok: replace T with intmyFunction(5.8, 8.4); //ok: replace T with doublemyFunction(5, 8.4); //error: replace T with int or double? 二义性
可以在函数模板中使用多个类型参数, 可以避免二义性
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
templateT1 myFunction( T1 arg1, T2 arg2){ cout< <<“ ”< <<“\n”; return arg1;}…myFunction(5, 7); //ok:replace T1 and T2 with intmyFunction(5.8, 8.4); //ok: replace T1 and T2 with doublemyFunction(5, 8.4); //ok: replace T1 with int, T2 with double
4、类模板
定义一批相似的类---->定义类模板---->生成不同的类
template <类型参数表> class 类模板名 { 成员函数和成员变量}; 类型参数表>
template <型参数表> 返回值类型 类模板名 <类型参数名列表> ::成员函数名(参数表){ ……} 类型参数名列表> 型参数表>
用类模板定义对象的写法如下: 类模板名 <真实类型参数表> 对象名(构造函数实际参数表);
如果类模板有无参构造函数, 那么也可以只写: 类模板名 <真实类型参数表> 对象名;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
templateclass Pair{public: T1 key; //关键字 T2 value; //值 Pair(T1 k,T2 v):key(k),value(v) { }; bool operator < (const Pair & p) const;};template bool Pair ::operator<( const Pair & p) const//Pair的成员函数 operator <{ return key < p.key; }int main(){ Pair student("Tom",19); //实例化出一个类 Pair cout << student.key << " " << student.value; return 0;}
Note:编译器由类模板生成类的过程叫类模板的实例化。
由类模板实例化得到的类叫模板类。
类模板的参数声明中可以包括非类型参数
template<class T, int elementsNum>
• 非类型参数: 用来说明类模板中的属性
• 类型参数: 用来说明类模板中的属性类型, 成员操作的参数类型和返回值类型
类模板与继承:
类模板派生出类模板
模板类 (即类模板中类型/非类型参数实例化后的类)派生出类模板
普通类派生出类模板
模板类派生出普通类
5、string类
(1)string类是一个模板类,使用需加头文件<string>:
typedef basic_stringstring;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
string s1("Hello"); // 一个参数的构造函数string s2(8, ‘x’); // 两个参数的构造函数string month = “March”; //错误的初始化方法:string error1 = ‘c’; // 错,‘c’是单字符不可以;"c"是字符串,后面有结束符,可以string error2(‘u’); // 错 string error3 = 22; // 错string error4(8); // 错//可以将字符赋值给string对象,但不能初始化string s;//可以s = ‘n’;
构造的string太长会抛出length_error异常。
长度用length()读取。
支持流读取运算符:
string str;cin>>str;
支持getline函数:
string s;getline(cin, s);
(2)string赋值:“=”,assign:
string s1("catpig"), s2, s3;s2 = assign(s1);s3 = assign(s1, 0, 2);
单个字符复制:s2[5] = s1[3] = ‘a’;
逐个访问string对象中的字符:成员函数at会做范围检查, 如果超出范围, 会抛出out_of_range异常, 而下标运算符不做范围检查。
string s1("Hello");for(int i=0; i
(3)string连接
用 + 运算符连接字符串,用成员函数 append 连接字符串。
string s1("good "), s2("morning! ");s1.append(s2);cout << s1;s2.append(s1, 3, s1.size()); //s1.size(), s1字符数cout << s2; //下标为3开始, s1.size()个字符//如果字符串内没有足够字符, 则复制到字符串最后一个字符
(4)string比较
用关系运算符比较string的大小 == , >, >=, <, <=, != 。返回值都是bool类型, 成立返回true, 否则返回false。
两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇’\0’为止。
或使用compare成员函数比较大小:
int f1 = s1.compare(s2);
(5)子串成员函数 substr()
string s1("hello world"), s2;s2 = s1.substr(4,5); //下标4开始5个字符cout << s2 << endl; //输出o wor
(6)
交换函数swap()
s1.swap(s2);
成员函数 capacity()
返回无需增加内存即可存放的字符数
成员函数maximum_size()
返回string对象可存放的最大字符数
成员函数length()和size()相同
返回字符串的大小 /长度
成员函数empty()
返回string对象是否为空
成员函数resize()
改变string对象的长度
(7)寻找string中的字符
find()//在s1中从前向后查找 “lo” 第一次出现的地方 //如果找到, 返回 “lo”开始的位置, 即 l 所在的位置下标 //如果找不到, 返回 string::npos (string中定义的静态常量)
rfind()//在s1中从后向前查找 “lo” 第一次出现的地方 //如果找到, 返回 “lo”开始的位置, 即 l 所在的位置下标 //如果找不到, 返回 string::npos
find_first_of()//在s1中从前向后查找 “abcd” 中任何一个字符第一次出现的地方 //如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
find_last_of()//在s1中查找 “abcd” 中任何一个字符最后一次出现的地方 //如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
find_first_not_of()//在s1中从前向后查找不在 “abcd” 中的字母第一次出现的地方 //如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
find_last_not_of()//在s1中从后向前查找不在 “abcd” 中的字母第一次出现的地方 //如果找到, 返回找到字母的位置; 如果找不到, 返回 string::npos
(8)各种成员函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
string s1("hello worlld"); s1.erase(5); cout << s1; cout << s1.length(); cout << s1.size();// 去掉下标 5 及之后的字符
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
string s1("hello worlld");cout << s1.find("ll", 1) << endl;cout << s1.find("ll", 2) << endl;cout << s1.find("ll", 3) << endl;//分别从下标1, 2, 3开始查找 “ll”
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
string s1("hello world");s1.replace(2,3, “haha");cout << s1;//将s1中下标2 开始的3个字符换成 “haha”输出:hehaha worldstring s1("hello world");s1.replace(2,3, "haha", 1,2);cout << s1;//将s1中下标2 开始的3个字符//换成 “haha” 中下标1开始的2个字符输出:heah world
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
string s1(“hello world”);string s2(“show insert”);s1.insert(5, s2); // 将s2插入s1下标5的位置cout << s1 << endl;s1.insert(2, s2, 5, 3);//将s2中下标5开始的3个字符插入s1下标2的位置cout << s1 << endl;输出:helloshow insert worldheinslloshow insert world
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
string s1("hello world");const char * p1=s1.data();for(int i=0; i
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
string s1("hello world");int len = s1.length();char * p2 = new char[len+1];s1.copy(p2, 5, 0);p2[5]=0;cout << p2 << endl;//s1.copy(p2, 5, 0) 从s1的下标0的字符开始,//制作一个最长5个字符长度的字符串副本并将其赋值给p2//返回值表明实际复制字符串的长度输出:hello
6、输入输出
(1)标准流对象
cin对应于标准输入流,用于从键盘读取数据,也可以被重定向为从文件中读取数据。
cout对应于标准输出流,用于向屏幕输出数据,也可以被重定向为向文件写入数据。
cerr对应于标准错误输出流,用于向屏幕输出出错信息。
clog对应于标准错误输出流,用于向屏幕输出出错信息。
cerr和clog的区别在于cerr不使用缓冲区,直接向显示器输出信息;而输出到clog中的信息先会被存放在缓冲区,缓冲区满或者刷新时才输出到屏幕。
(2)重定向
freopen("test.txt","w",stdout); //将标准输出重定向到 test.txt文件
freopen(“t.txt”,“r”,stdin); //cin被改为从 t.txt中读取数据
(3)判断输入流结束
int x;while(cin>>x){…..}
如果从文件输入,比如前面有freopen(“t.txt”,“r”,stdin);那么读到文件尾部,算输入结束。
如果从键盘输入,则在单独一行输入Ctrl+Z代表输入流结束。
(4)istream类的成员函数
istream & getline(char * buf, int bufSize); 从输入流中读取bufSize-1个字符到缓冲区buf,或读到碰到‘\n’ 为止(哪个先到算哪个)。
istream & getline(char * buf, int bufSize,char delim); 从输入流中读取bufSize-1个字符到缓冲区buf,或读到碰到delim字符为止(哪个先到算哪个)。
两个函数都会自动在buf中读入数据的结尾添加\0’。
‘\n’或 delim都不会被读入buf,但会被从输入流中取走。如果输入流中 ‘\n’或delim之前的字符个数达到或超过了bufSize个,就导致读入出错,其结果就是:虽然本次读入已经完成,但是之后的读入就 都会失败了。
可以用 if(!cin.getline(…)) 判断输入是否结束。
bool eof(); 判断输入流是否结束。
int peek(); 返回下一个字符,但不从流中去掉。
istream & putback(char c); 将字符ch放回输入流。
istream & ignore( int nCount = 1, int delim = EOF ); 从流中删掉最多nCount个字符,遇到EOF时结束。
#includeusing namespace std;int main() {int x;char buf[100];cin >> x;cin.getline(buf,90);cout << buf << endl;return 0;}输入:12 abcd↙输出:abcd (空格+abcd)输入12↙程序立即结束,输出:12因为getline读到留在流中的’\n’就会返回