C++程序设计原理与实践 第二十章部分答案
1 //运行错误= = 2 3 #include <iostream> 4 #include <vector> 5 #include <list> 6 using namespace std; 7 8 typedef vector<char> Line; 9 10 11 12 struct Document 13 { 14 list<Line> line; 15 Document(){line.push_back(Line());} 16 Text_iterator begin() 17 { 18 return Text_iterator(line.begin(),(*line.begin()).begin()); 19 } 20 Text_iterator end() 21 { 22 list<Line>::iterator last=line.end(); 23 --last; 24 return Text_iterator(last,(*last).end()); 25 } 26 }; 27 28 class Text_iterator{ 29 public: 30 list<Line>::iterator In; 31 Line::iterator pos; 32 33 Text_iterator(list<Line>::iterator II,Line::iterator pp) 34 :In(II),pos(pp){} 35 36 char& operator*(){return *pos;} 37 Text_iterator&operator++(); 38 Text_iterator&operator+(int); 39 int operator-(Text_iterator&); 40 bool operator==(const Text_iterator&other)const 41 { 42 return In==other.In&&pos==other.pos; 43 } 44 45 bool operator!=(const Text_iterator&other)const 46 { 47 return !(*this==other); 48 } 49 50 }; 51 52 Text_iterator&Text_iterator::operator++() 53 { 54 if(pos==(*In).end()){ 55 ++In; 56 pos=(*In).begin(); 57 } 58 ++pos; 59 return *this; 60 } 61 62 Text_iterator&Text_iterator::operator+(int i) 63 { 64 while(i-->0) 65 pos++; 66 return *this; 67 } 68 69 int Text_iterator::operator-(Text_iterator&ti) 70 { 71 int i=0; 72 Text_iterator ti1=*this; 73 while(ti1!=ti) 74 { 75 i++; 76 ++ti1; 77 } 78 return i; 79 } 80 81 bool match(Text_iterator first,Text_iterator last,const string&s) 82 { 83 return 1; 84 } 85 86 Text_iterator find_replace(Text_iterator first,Text_iterator last,const string&s) 87 { 88 if(s.size()==0) return last; 89 char first_char=s[0]; 90 while(1) 91 { 92 Text_iterator p=find(first,last,first_char); 93 if(p==last||match(p,last,s)) 94 return p; 95 } 96 } 97 98 void find_replace(Text_iterator first,Text_iterator last,const string&s,const string&s1) 99 { 100 if(s.size()==0) return; 101 102 103 char first_char=s[0]; 104 Text_iterator p=find(first,last,first_char); 105 int index=last-first; 106 while(first!=last) 107 { 108 if(match(p,last,s)) 109 { 110 int i=0; 111 list<Line>::iterator p_In=p.In; 112 Line::iterator p_Pos=p.pos; 113 for(i=0;i<s.size();i++) 114 { 115 p_Pos=(*p_In).erase(p_Pos); 116 index--; 117 } 118 for(i=s1.size()-1;i>=0;i--) 119 { 120 p_Pos=(*p_In).insert(p_Pos,s1[i]); 121 index++; 122 } 123 first=Text_iterator(p_In,p_Pos); 124 last=first+index; 125 for(i=0;i<s1.size();i++) 126 first++; 127 128 } 129 p=find(first,last,first_char); 130 } 131 }
1 string fn(vector<string>&vs) 2 { 3 string str=""; 4 int i; 5 for(i=0;i<vs.size();i++) 6 if(vs[i]>str) 7 str=vs[i]; 8 return str; 9 }
1 int count_word1(Document&d) 2 { 3 Text_iterator t1=d.begin(); 4 Text_iterator t2=d.end(); 5 int f=0; 6 int count=0; 7 while(*t1!=*t2) 8 { 9 if(!isspace(*t1)) 10 f=1; 11 else if(f==1) 12 { 13 count++; 14 f=0; 15 } 16 17 ++t1; 18 } 19 return count; 20 } 21 22 int count_word2(Document&d) 23 { 24 Text_iterator t1=d.begin(); 25 Text_iterator t2=d.end(); 26 int f=0; 27 int count=0; 28 while(*t1!=*t2) 29 { 30 if(isalpha(*t1)) 31 { 32 if(f==2) 33 { 34 f=0; 35 count++; 36 } 37 if(isalpha(*(t1+1))) 38 { 39 f=1; 40 } 41 } 42 else if(isdigit(*t1)) 43 { 44 if(f==1) 45 { 46 f=0; 47 count++; 48 } 49 if(isdigit(*(t1+1))) 50 { 51 f=2; 52 } 53 } 54 else 55 if(f!=0) 56 { 57 f=0; 58 count++; 59 } 60 ++t1; 61 } 62 return count; 63 }
1 template<class Elem>struct Link{ 2 Link*prev; 3 Link*succ; 4 Elem val; 5 }; 6 7 8 template<class Elem>struct __iterator{ 9 Link<Elem>* node; 10 11 __iterator(Link<Elem>* x):node(x){} 12 __iterator(){} 13 14 __iterator& operator++(){node=node->succ;return *this;} 15 __iterator& operator--(){node=node->prev;return *this;} 16 Elem&operator*(){return node->val;} 17 18 bool operator==(const __iterator&b)const {return node==b.node;} 19 bool operator!=(const __iterator&b)const {return node!=b.node;} 20 }; 21 22 template<class Elem> class list{ 23 protected: 24 typedef Link<Elem> list_node; 25 public: 26 typedef Elem value_type; 27 typedef value_type* pointer; 28 typedef value_type& reference; 29 typedef list_node* link_type; 30 31 typedef __iterator<Elem> iterator; 32 33 protected: 34 link_type get_node() {return new list_node;} 35 36 link_type create_node(Elem& x) 37 { 38 link_type p = get_node(); 39 p->val=x; 40 41 return p; 42 } 43 44 link_type node; 45 46 void empty_new() 47 { 48 node=get_node(); 49 node->prev=node; 50 node->succ=node; 51 52 } 53 54 public: 55 list() { empty_new(); } 56 57 iterator begin() { return (link_type)((*node).succ); } 58 59 iterator end() { return node; } 60 61 62 bool empty() const { return node->next == node; } 63 64 reference front() { return *begin(); } 65 reference back() { return *(end()--); } 66 67 iterator insert(iterator position, Elem& x) 68 { 69 link_type tmp = create_node(x); 70 71 tmp->succ = position.node; 72 tmp->prev = position.node->prev; 73 (link_type(position.node->prev))->succ = tmp; 74 position.node->prev = tmp; 75 return tmp; 76 } 77 78 void push_front(Elem& x) { insert(begin(), x); } 79 void push_back(const Elem& x) { insert(end(),(Elem)x); } 80 81 iterator erase(iterator position) 82 { 83 link_type next_node = link_type(position.node->succ); 84 link_type prev_node = link_type(position.node->prev); 85 prev_node->succ = next_node; 86 next_node->prev = prev_node; 87 delete position.node; 88 return iterator(next_node); 89 } 90 91 void pop_front() { erase(begin()); } 92 void pop_back() {iterator tmp = end(); erase(--tmp); } 93 94 };
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <list> 5 #include <ctime> 6 using namespace std; 7 8 double random(double start,double end) 9 { 10 return start+(end-start)*rand()/(RAND_MAX+1.0); 11 } 12 13 const long N=10000; 14 15 void f1(vector<int>&vi) 16 { 17 srand((unsigned)time(NULL)); 18 int i=int(random(0,3)); 19 for(vector<int>::iterator j=vi.begin();j!=vi.end();j++) 20 if(*j>i) 21 { 22 vi.insert(j,i); 23 break; 24 } 25 26 } 27 28 void f2(list<int>&li) 29 { 30 srand((unsigned)time(NULL)); 31 int i=int(random(0,3)); 32 for(list<int>::iterator j=li.begin();j!=li.end();j++) 33 if(*j>i) 34 { 35 li.insert(j,i); 36 break; 37 } 38 39 } 40 41 int main() 42 { 43 vector<int> vi; 44 list<int> li; 45 clock_t t1=clock(); 46 if(t1==clock_t(-1)) 47 { 48 cerr<<"sorror"<<endl; 49 exit(1); 50 } 51 for(long i=0;i<N;i++) 52 // f2(li); 53 f1(vi); 54 55 clock_t t2=clock(); 56 if(t2==clock_t(-1)) 57 { 58 cerr<<"sorror2"<<endl; 59 exit(2); 60 } 61 62 cout<<double(t2-t1)/CLOCKS_PER_SEC<<" "<<CLOCKS_PER_SEC<<endl; 63 64 while(1); 65 return 0; 66 67 }
习题8 记得去掉空白 ‘\n‘
习题14 --操作符 back() pop_back()
习题15 16 个人理解是 vector<T> **p
习题17 是指针的指针 = =乱乱 不知道怎么说
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。