10.1
#include<iostream> #include<vector> #include<algorithm> #include<fstream> using namespace std; int main(int argc,const char*argv[]){ /*ifstream in(argv[1]); if(!in){ cout << "Failed to open input file "< < endl; exit(1); }*/ //The meaning of this paragraph is completely incomprehensible vector<int> nums; int number; while (cin >> number) { nums.push_back(number); } cout << count(nums.begin(),nums.end(),4) << endl; return 0; }
Confusion:
The meaning of this program is not understood
ifstream in(argv[1]); if(!in){ cout << "Failed to open input file" << endl; exit(1); }
10.5
#include<iostream> #include<vector> #include<algorithm> #include<fstream> #include<string> #include<list> #include<numeric> #include<string.h> using namespace std; int main(void){ char *p[] = {"Hello","World","!"}; char *q[] = {strdup(p[0]),strdup(p[1]),strdup(p[2])}; //strdup() is a string copying function, which allocates space for the copied string and returns the pointer after the allocated space //After the space is no longer used, the function free() is used to free the allocated memory char *r[] = {p[0],p[1],p[2]}; cout << equal(begin(p),end(p),q) << endl; cout << equal(begin(p),end(p),r) << endl; //begin() returns the pointer to the first element of the array, and end() returns the pointer to the last element of the array return 0; }
10.27
#include<iostream> #include<vector> #include<list> #include<algorithm> #include<iterator> using namespace std; int main(void){ vector<int> nums; int number; while (cin >> number) { nums.push_back(number); } sort(nums.begin(),nums.end()); // int size = nums.size(); //list<int> lt(size); // unique_copy(nums.begin(),nums.end(),lt.begin); //Tangled for a long time, I don't know how to determine the size of the lt container list<int> lt; unique_copy(nums.begin(),nums.end(),back_inserter(lt)); for(auto v : lt){ cout << v << " "; } cout << endl; return 0; }
Iterators such as begin() and end() indicate the position of the element. They cannot be used to implement insertion, but back_ Functions such as inserter () can both indicate the location and implement insertion.
10.28
#include<iostream> #include<vector> #include<list> #include<algorithm> #include<iterator> using namespace std; int main(void){ vector<int> nums = {1,2,3,4,5,6,7,8,9}; list<int> lt1,lt2,lt3; copy(nums.begin(),nums.end(),front_inserter(lt1)); copy(nums.begin(),nums.end(),back_inserter(lt2)); copy(nums.begin(),nums.end(),inserter(lt3,lt3.begin())); for(auto it1 = lt1.begin();it1 != lt1.end();it1++){ cout << *it1; } cout << endl; for(auto it2 = lt2.begin();it2 != lt2.end();it2++){ cout << *it2; } cout << endl; for(auto it3 = lt3.begin();it3 != lt3.end();it3++){ cout << *it3; } cout << endl; return 0; }
front_iterator () adds elements to the container upside down.
10.34
#include<iostream> #include<vector> #include<list> #include<algorithm> #include<iterator> using namespace std; int main(void){ vector<int> nums = {1,2,3,4,5,6,7,8,9}; // vector<int>::reverse_iterator it; for(auto it = nums.rbegin();it != nums.rend();it++){ cout << *it; } cout << endl; }
10.35
#include<iostream> #include<vector> #include<list> #include<algorithm> #include<iterator> using namespace std; int main(void){ vector<int> nums = {1,2,3,4,5,6,7,8,9}; // vector<int>::reverse_iterator it; for(auto it = nums.end()-1;it != nums.begin()-1;it--){ cout << *it; } cout << endl; //Answer approach for(auto r_it = nums.end();r_it != nums.begin();){ cout << *(--r_it); //Decrease before output } cout << endl; }
10.36
#include<iostream> #include<vector> #include<list> #include<algorithm> #include<iterator> using namespace std; int main(void){ list<int> nums = {1,2,0,4,5,0,7,0,9}; auto last_z = find(nums.rbegin(),nums.rend(),0); //How do I know which order the reverse iterator points to last_z++;//Ensure that when using the forward iterator, it can point to the last 0 //int p = 0; This is not initialized properly. The number of moves will be one less than the number of all elements int p = 1; for(auto it = nums.begin();it != last_z.base();it++){ p++; } if(p >= nums.size()) cout << "0 does not exist" << endl; else cout << "the position of last zore is " << p << endl; return 0; }
10.37
#include<iostream> #include<vector> #include<list> #include<algorithm> #include<iterator> using namespace std; int main(void){ ostream_iterator<int> out_iter(cout," "); //don't understand vector<int> vi = {0,1,2,3,4,5,6,7,8,9}; copy(vi.begin(),vi.end(),out_iter); cout << endl; list<int> li; vector<int>::reverse_iterator re(vi.begin()+2); //using iterator to initialize reverse iterator vector<int>::reverse_iterator rb(vi.begin()+7); copy(rb,re,back_inserter(li)); copy(li.begin(),li.end(),out_iter); cout << endl; return 0; } /*int main(void){ vector<int> nums = {1,2,3,4,5,6,7,8,9,10}; list<int> lt; copy(nums.rbegin()+3,nums.rbegin()+8,back_inserter(lt)); //Copy the number [3, 7]. Since copy is a left open and right closed interval, the number that should be added is 8 for(auto it = lt.begin();it != lt.end();it++){ cout << *it; } cout << endl; return 0; }*/
1)ostream_iterator (os,str)
Output T-type elements to the output stream, and append a C-element style string after each element,
2) You can initialize a reverse iterator with a forward iterator, which has the same effect as moving the position the iterator points to one bit forward
10.38
Lists the categories of the five iterators and the operations supported by each iterator.
1. Input iterator -- used to read the contents of the element pointed to by the iterator
1) Can compare iterators. (used to determine the range of inputs and outputs)
2) It can dereference and obtain the specific content of the element through the pointer
3) The position can be moved by increasing and decreasing elements
(determine the scope, move the location, and dereference to obtain the specific content)
2. Output iterator -- used to write the specified element into the container
1) It can be positioned by increasing and decreasing to determine the position of input elements
2) Dereference operator, specifying the location of its input
3. Forward iterator - supports one-way scanning of elements (if it is from back to front, is it also a one-way iterator?
1) Both input and output iterator operations are supported
2) Elements can only be read and written in one direction
4. Bidirectional iterator -- able to read and write elements in both directions
1) Except forward_list, other containers support bidirectional iterators
5. Random access iterator -- random read and write elements
1) Has a comparison operator that can compare the relationship between two iterators
2) Iterators can add and subtract numbers
3) The iterators can be subtracted to get the relative distance
4) Operator with subscript
don't get it