1.将源文件的每一行文本添加一个行号输出到目的文件中。
#include#include using namespace std; int main() { char s1[500]; int cnt = 0; //表示行数 ifstream inf("a.cpp"); //创建一个名称为inf的ifstream实例化对象, //与文件a.cpp相关联,该文件为输入 读操作 //即读取 提取 if (!inf.fail()) { //格式正确 ofstream outf("b.cpp"); //创建一个名称为outf的ofstream实例化对象, //与文件b.cpp相关联,该文件为输出 写操作 //即输出到该文件中(显示) while (!inf.eof()) { inf.getline(s1, sizeof(s1) - 1); outf << setfill('0') << setw(4) << ++cnt << " "<< s1 << endl; } outf.close(); inf.close(); } return 0; }
2.复制源文件内容到目的文件
#include#include #include #include using namespace std; int main() { char src[260]; char dest[260]; char buff[16384]; ifstream inf("book.dat", ios_base::in | ios_base::binary); if (!inf.fail()) { ofstream outf("out.dat", ios_base::out | ios_base::binary); while (!inf.eof()) { inf.read(buff, sizeof(buff)); outf.write(buff, inf.gcount()); cout << buff << endl; } outf.close(); inf.close(); } return 0; }
#include#include #include #include using namespace std; struct Book{ char c[5]; char n[11]; int p; int q; }; int main() { Book a; ifstream inf("book.dat"); ios_base::openmode m = ios_base::in | ios_base::out; fstream iof("out.dat", m | ios_base::trunc | ios_base::binary); if (inf.fail() || iof.fail()) return -1; while (!inf.eof()) { inf >> a.c >> a.n >> a.p >> a.q; iof.write((char*)&a, sizeof(Book)); } inf.close(); iof.seekg(0 * sizeof(Book), ios_base::beg); iof.read((char*)&a, sizeof(Book)); cout << a.c << a.n << a.p << a.q; return 0; }
44.1向量举例
#include#include using namespace std; int main() { vector V1, V2; int A[] = { 1945,10,1 }, i; vector ::iterator It; V1.assign(A, A + 3); V2.assign(3, 10); for (i = 1; i <= 5; i++)V1.push_back(i); V1.pop_back(); V1.front() -= V1.back(); //V1 1945 10 1 1 2 3 4 for (It = V1.begin(); It < V1.end(); It++) V2.push_back(*It); //V2 10 10 10 1945 10 1 1 2 3 4 V2.insert(V2.begin(), 2, 300); //V2 300 300 10 10 10 1945 10 1 1 2 3 4 V2.erase(V2.begin() + 5); //V2 300 300 10 10 10 10 1 1 2 3 4 for (i = 0; i < V2.size(); i++) cout << V2[i] << " "; return 0; }
44.2自定义一个动态数组类模板
#include#include using namespace std; enum ErrorType { invalidArraySize,memoryALLocationError,indexOutOfRange }; char* errorMsg[] = { "invalid array size","memory allocation error","invalid index " }; template class Array { private: T* alist; int size; void Error(ErrorType error, int badIndex = 0) const; public: Array(int sz = 50); Array(const Array & A); ~Array(void); Array & operator=(const Array & rhs); T& operator[](int); operator T* () const; int ListSize() const; void Resize(int sz); }; template void Array ::Error(ErrorType error, int badIndex) const { cout << errorMsg[error]; if (error == indexOutOfRange) cout << badIndex; cout << endl; exit(1); } template Array ::Array(int sz) { if (sz <= 0) Error(invalidArraySize); else { size = sz; alist = new T[size]; if (alist == NULL) Error(memoryALLocationError); } } template //拷贝构造函数(深拷贝) Array ::Array(const Array & X) { int n=X.size; size=n; alist=new T[n]; if(alist==NULL) Error(memoryAllocationError); T *srcptr=X.alist; //X.alist是对象X的数组首地址 T *destptr=alist; //本对象数组首地址 while(n--) //逐个复制数组元素 *destptr++=*srcptr++; } template //析构函数 Array ::~Array() { delete [] alist; } template //重载“=”运算符,将一个数组赋值给另一个数组 Array & Array ::operator =(const Array & rhs) { int n=rhs.size; if(size!=n) { delete [] alist; alist=new T[n]; if(alist==NULL) Error(memoryAllocationError); size=n; } //从rhs向本对象复制元素 T* destptr=alist; T* srcptr=rhs.alist; while(n--) *destptr++=*srcptr++; return *this;//返回当前的对象 } template //重载“[]”运算符,实现通过下标访问数组元素 T &Array ::operator [](int n) { if(n<0||n>size-1) //检查下标是否越界 Error(indexOutOfRange,n); return alist[n];//返回下标为n的数组元素 } template //重载类型转换 Array ::operator T*() const { return alist; } template //取当前数组的长度 int Array ::ListSize() const { return size; } template //修改数组的长度为sz void Array ::Resize(int sz) { if(sz<=0) Error(invalidArraySize); if(sz==size) return; T *newlist=new T[sz]; if(newlist==NULL) Error(memoryAllocationError); int n=(sz<=size)?sz:size;//将sz和size中较小的一个赋给n T *srcptr=alist;//原数组的首地址 T *destptr=newlist;//新数组的首地址 while(n--) *destptr++=*srcptr++; delete [] alist; alist=newlist; size=sz;//使alist指向新数组,并更新sz } int main() { int i, * p; Array a(5); for (i = 0; i < 5; i++) cin >> a[i]; for (i = 0; i < 5; i++) cout << a[i] << ' '; cout << endl; Array b = a; for (i = 0; i < 5; i++) cout << b[i] << ' '; cout << endl; a.Resize(10); for (p = a; p < a + 10; p++) cout << *p << ' '; return 0; }
45.1列表举例
#include#include using namespace std; int main() { int i; int A[] = { 15,36,7,17 }; list
::iterator It; list L1, L2, L3(A, A + 4); //L1L2为空表 L3:15 36 7 17 for (i = 1; i <= 6; i++)L1.push_back(i); //L1 1 2 3 4 5 6 for (i = 1; i <= 3; i++)L2.push_back(i * 10); //L2 10 20 30 It = L1.begin(); advance(It, 2); //It指向第3个元素 L1.splice(It, L2); //L1:1 2 10 20 30 3 4 5 6 L2 空了 //It指向3 L2.splice(L2.begin(), L1, It); //L1:1 2 10 20 30 4 5 6 L2: 3 L1.remove(20);//1 2 10 30 4 5 6 L1.sort();// 1 2 4 5 6 10 30 排序 L1.merge(L3);//1 2 4 5 6 10 15 30 36 7 17 L1.push_front(L2.front());// 3 1 2 4 5 6 10 15 30 36 7 17 L1.reverse();//逆序 for (It = L1.begin(); It != L1.end(); ++It) cout << *It << endl; return 0; }
45.2自定义一个单向链表类模板
#include#include using namespace std; template class Node //结点类 { private: Node * next; public: T data; Node(const T& item,Node * ptrnext=NULL); void InsertAfter(Node *p); Node *DeleteAfter(); Node *NextNode() const; }; template //构造函数 Node ::Node(const T& item,Node * ptrnext): data(item),next(ptrnext){} template //求下一个结点的地址 Node *Node ::NextNode() const { return next; } template //在当前结点之后插入一个结点p void Node ::InsertAfter(Node *p) { p->next=next; //p结点指针域指向当前结点的后继结点 next=p; //当前结点的指针域指向p结点 } template //删除当前结点的后继结点,并返回其地址 Node *Node ::DeleteAfter() { Node *tempPtr=next;//将欲删除的结点地址存储到tempPtr中 if(next==NULL) return NULL; //如果当前结点没有后继结点则返回空 next=tempPtr->next;//使当前结点的指针域指向tempPtr的后继结点 return tempPtr;//返回被删除结点的地址 } template //生成结点函数 Node *GetNode(const T& item,Node *nextPtr=NULL) { Node *newNode; newNode=new Node (item,nextPtr); if(newNode==NULL) { cerr<<"Memory allocation failure!"< //生成链表函数 Node *CreateList(Node *head,int n) { Node *currPtr,*newNode,data; currPtr=head=GetNode(0); //创建头结点 for (; n>0; n--) { //创建n个结点链表 cin>>data; newNode=GetNode(data);//创建新结点 currPtr->next=newNode, currPtr=newNode; } currPtr->next=NULL; //尾结点 return head; } enum Appendnewline{nonewline,addnewline}; //控制输出结点后是 否换行 template //输出链表函数 void PrintList(Node *head,Appendnewline addnl=nonewline) { Node *currPtr=head; while(currPtr!=NULL) { if(addnl==addnewline) cout< data< data<<" "; currPtr=currPtr->NextNode(); } } template //查找结点函数 int FindNode(Node *head,T &item,Node *prevPtr) { Node *currPtr=head; prevPtr=NULL; while(currPtr!=NULL) { if(currPtr->data=item) return 1; prevPtr=currPtr; currPtr=currPtr->NextNode(); } return 0; }
46.1队列举例
#include#include using namespace std; int main() { queue Q; for (int i = 1; i <= 6; i++) Q.push(i); //Q 1 2 3 4 5 6 Q.front() -= Q.back(); //Q -5 2 3 4 5 6 while (!Q.empty()) { cout << Q.front() << " "; Q.pop(); //出队离开 } return 0; }
46.2列表举例
#include#include using namespace std; int main() { queue q; int n; cin >> n; for (int i = 1; i <= n; i++)q.push(i); while (q.size() > 2) { cout << q.front() << " "; q.pop(); q.push(q.front()); q.pop(); } cout << endl; while (!q.empty()) { cout << q.front() << " "; q.pop(); } return 0; }
46.3栈举例
#include#include using namespace std; int main() { stack s; for (int i = 1; i <= 6; i++)s.push(i); while (!s.empty()) { cout << s.top() << " "; s.pop(); } return 0; }
46.4利用栈检查字符串里边括号是否匹配(成双出现)
#include#include #include using namespace std; bool isbalance(const string& str) { int len = str.size(); stack mystack; for (int i = 0; i < len; ++i) { if (str[i] == '[' || str[i] == '{' || str[i] == '(' || str[i] == '<') mystack.push(str[i]); if (str[i] == ']' || str[i] == '}' || str[i] == ')' || str[i] == '>') { if (mystack.empty()) { cout << "the string is unbalanced" << endl; return false; } switch (str[i]) { case ']':{ if ('[' != mystack.top()) return false; mystack.pop(); break; } case '}':{ if ('{' != mystack.top()) return false; mystack.pop(); break; } case ')':{ if ('(' != mystack.top()) return false; mystack.pop(); break; } case '>':{ if ('<' != mystack.top()) return false; mystack.pop(); break; } } } } if (mystack.empty()) return true; else { mystack.pop(); return false; } } int main() { bool bal; string str; cin >> str; bal = isbalance(str); if (bal == true)cout << "string is balance"; else cout << "错误" << endl; return 0; }
47.1异常处理举例
fun正常调用结束
caller调用正常结束
caller2调用正常结束
caller2捕获所有未知异常->caller2调用正常结束
caller2捕获double->caller2调用正常结束
caller1捕获int->caller调用正常结束
caller2调用正常结束
#include#include #include using namespace std; void fun(int test) { if (test == 0) throw test; if (test == 1) throw 1.5; if (test == 2) throw "abc"; cout << "fun正常调用结束" << endl; } void caller1(int test) { try { fun(test); } catch (int) { cout << "caller1捕获int->"; } cout << "caller1调用正常结束" << endl; } void caller2(int test) { try { caller1(test); } catch (double) { cout << "caller2捕获double->"; } catch (...) { cout << "caller2捕获所有未知异常->"; } cout << "caller2调用正常结束" << endl; } int main() { for (int i = 3; i >= 0; i--) caller2(i); return 0; }



