About dynamic memory and input and output files

Source: Internet
Author: User
Tags strcmp

Recent exchanges with senior subjects, especially these two days focused on the practice. Dynamic allocation of storage space and input and output files is undoubtedly very important.

Here are some examples of C + + that are borrowed today:

Simulation of dynamic memory

structmemo{intstart; intfinish; Memo*next;};voidArrange (memo*Memo) {Memo* Inspos = head, *curpos, *Minpos;  while(Inspos->next! = NULL) {//Direct Insert SortCurPos = Minpos =Inspos;  while(Curpos->next = NULL) {//Find the node with the lowest starting address            if(curpos->next->start<minpos->next->start) Minpos=CurPos; CurPos= CurPosNext; }        //Insert Minimum nodeCurPos = MinposNext; MinposNext = CurPosNext; CurPosNext = InsposNext; InsposNext =CurPos; Intpos= InsposNext; }    //merging contiguous blocks of memory    for(CurPos = head, next; CurPos, next!=null; curpos = CurPosnext) {    //The current block is contiguous with the next one, performing the merge    while(Curpos->finish +1= = Curpos->next->start) {Minpos=curpos->Next; CurPos->finish=minpos->finish; CurPos->next=minpos->Next; DeleteMinpos; if(Curpos->next==null)return; }}

Another very useful program: count the frequency of words used in a file

#include <iostream>#include<iomanip>#include<fstream>using namespacestd; structword{Chardata[ -]; intcount;}; voidGetword (Ifstream &AMP;FP,CharCh[]);//read a word from a file voidDoubleSpace (word* &list,int&size);//Expand Array Space  intMain () {Ifstreaminch("Txt.txt"); Charch[ -]; intSize =Ten, Len =0, I, J; Word* result =Newword[Ten]; if(!inch) {cout <<"File Open Error"<<endl;return 1;}  while(true){//read the file until the endGetword (inch, ch);//read in a word ch         if(ch[0]==' /') Break;  for(i =0; i < Len; ++i)if(strcmp (Result[i].data, ch) >=0) Break; if(I < len && strcmp (result[i].data, ch) = =0) {Result[i].count+=1;         Coutinue; }         if(Size = =len) doubleSpace (result, size);  for(j = len++; j > i;--j) result[j] = result[j-1];         strcpy (Result[i].data, ch); Result[i].count=1; }      for(i =0; i < Len; ++i) cout<< STW ( -) << elft << result[i].data << SETW (5) << Result[i].count <<Endl; inch. Close (); CIN>> ch[0]; return 0;}

About dynamic memory and input and output files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.