Today, when doing exercise 9.22, segments fault is always present. Finally found that the "container insert after the iterator will fail" understanding is not thorough enough.
The topics are as follows:
Assuming that IV is a vector of int, what error is there in the program below? How will you modify it?
1 Auto iter = iv.begin (); 2 2 ; 3 while (ITER! = mid) {4 if (*iter = = some_val) 5 2 * some_val); 6 }
The code I wrote at the beginning is as follows:
1 /*************************************************************************2 > File name:9.22.cpp3 > Author:wanchouchou4 > Mail: [email protected]5 > Created time:2014 November 02 Sunday 16:34 20 seconds6 ************************************************************************/7 8#include <iostream>9#include <vector>Ten using namespacestd; One A intMain () { -vector<int> Vint = {1,1,1,1,1,3,4,1}; - Const intval =1; theAuto Vibegin =Vint.begin ();
/* Note here that if vint.size is less than or equal to 1, Vimid = Vibegin will not go into the while loop, so we should consider this situation separately * / -Auto Vimid = Vint.begin () + vint.size ()/2;
+ if(Vint.empty ()) { -cout <<"This vector is empty!"<<Endl; + return 0; A } at if(vint.size () = =1){ - if(*vibegin = =val) { -Vint.insert (Vibegin,2*val); - } - GotoPrint; - } in - while(Vibegin! =Vimid) { to if(*vibegin = =val) { +Vint.insert (Vibegin,2*val); * } $++Vibegin; - } the + Print: AAuto Viend =vint.end (); theVibegin =Vint.begin (); + while(Vibegin! =viend) { -cout << *vibegin <<", "; $++Vibegin; $ } - -cout <<Endl; the -}
When running, segmentation faulted appears.
Logically speaking, there should be no problem, then why is it wrong? It turns out that I forgot the important effect of inserting operations on containers "All iterators will fail except end!!!" ”。 When the first insert is completed, the Vibegin and Vimid are invalidated at this point, then all operations on it are illegal. So we have to re-assign two iterators after each insert operation. Since the assignment to the vimid is cumbersome, a different way is used to record whether the current iterator reaches the midpoint of the container, as shown in the following code:
/************************************************************************* > File name:9.22.cpp > Author:w Anchouchou > Mail: [email protected] > Created time:2014 November 02 Sunday 16:34 20 seconds ******************************* *****************************************/#include<iostream>#include<vector>using namespacestd;intmain () {vector<int> Vint = {1,1,1,1,3,4,1}; Const intval =1; Auto Vibegin=Vint.begin ();
/* Note here that if vint.size is less than or equal to 1, Vimid = Vibegin will not go into the while loop, so we should consider this situation separately * /Auto mid = Vint.size ()/2; if(Vint.empty ()) {cout<<"This vector is empty!"<<Endl; return 0; } if(vint.size () = =1){ if(*vibegin = =val) {Vint.insert (Vibegin,2*val); } GotoPrint; } while(Distance (Vibegin, Vint.end ()) >mid) { if(*vibegin = =val) {Vibegin= Vint.insert (Vibegin,2*val); ++Vibegin; } ++Vibegin; }print:auto Viend=Vint.end (); Vibegin=Vint.begin (); while(Vibegin! =viend) {cout<< *vibegin <<", "; ++Vibegin; } cout<<Endl;}
The results are as follows:
[Email protected] Virtual-machine:~/c++/9. *$./9.2221212 121341
C + + Primer Learning notes Container Insert