The second half mainly introduces iterators and arrays.
iterator
Iterators are like pointers and can access objects indirectly.
Use of iterators
The iterator has begin and end members. Begin is the iterator responsible for pointing to the first element, end is the "tail" that does not exist in a container, and the iterators returned by begin and end members are the same iterator, which is called the tail iterator.
Operators for standard container iterators
*iter Return iterator iter Reference to the element pointed to iter->men Dereference iter And get the element name men Member of, equivalent to(*iter).men,Note that parentheses cannot be less. ++iter order iter Points to the next element of the container --iter order iter Points to the previous element of the container iter1==iter2 Determines whether two iterators are equal iter1!=iter2
Two new functions are introduced into C++11, cbegin and cend. The difference between these two functions and the original begin and end is that whether the object (vector, string or other) itself is a constant or not, the returned value is a constant type.
Iterator operation
iter+n The iterator plus an integer value is still an iterator, and the new position of the iterator moves several elements forward compared with the original. The result iterator either indicates an element in the container or indicates the next position of the element at the end of the container iter-n Similar to the above meaning iter+=n take iter plus n The results are returned to iter iter-=n Similar to the above meaning iter1-iter2 Direct distance between two iterators
3.21 enter a set of integers and use the iterator to output the contents of the vector container
#include<iostream> #include<vector> #include<string> using namespace std; int main() { vector<string> v7{10,"hi"}; for(auto a=v7.cbegin();a!=v7.cend()&&!a->empty();a++) cout<<*a<<" "; //*Dereference }
3.23 input a set of numbers and output twice the number
#include<iostream> #include<vector> using namespace std; int main() { vector<int> v; int temp; cout<<"please input ten characters:"<<endl; for(int i=0;i<10;i++) { while(cin>>temp) v.push_back(temp); } for(auto it=v.begin();it != v.end();it++) cout<<*it*2<<" "; }
3.24 read in a group of integers, make the first array plus the last number, the second number plus the penultimate number, and so on, and finally output the result
// Iterators have no addition, only subtraction. Subtraction means distance #include<iostream> #include<vector> using namespace std; int main() { vector<int> vInt; cout<<"Please input charaters"<<endl; int i; while(cin>>i) vInt.push_back(i); if(vInt.begin()==vInt.end()) { cout<<"empty"<<endl; return -1; } auto beg=vInt.begin(); auto end=vInt.end(); for(auto it=beg;it!=beg+(end-beg)/2;it++) cout<<*it+*(beg+(end-it)-1)<<" "; if(vInt.size()/2!=0) //If it is an odd number, process the middle number separately cout<<*(beg+(end-beg)/2);//First calculate the number of elements from beginning to end, and then control the iterator to move 1 / 2 elements to the right from the beginning to the middle }
In this problem, we use an algorithm, binary search. There is a small detail. The binary search uses mid=(beg+end)/2, but the program uses mid = beg + (end beg) / 2. Why do you write this?
Because C + + does not have the addition operation of two iterators, it is meaningless to add two iterators. However, iterators have subtraction operation, and the result of subtraction is their distance, so mid=(beg+end)/2 is illegal, mid = beg + (end beg) /2 means to first calculate the direct distance between end and beg, that is, the value of the number of container elements, and then move one-half of the distance to the right from the beginning of the iterator to the middle position.
array
Array is also a container for storing objects of the same type. These objects have no name and need to be accessed through their location. Unlike vector, the size of the array remains unchanged, and elements cannot be added to the array at will.
Array declaration
a[b]
Where a is the name of the array and b is the dimension of the array. The dimension describes the number of elements in the array, so it must be greater than 0.
How to initialize an array
Note: there is no array of references, but there are references to arrays.
int &refs[10]=/*?*/ Error this is a referenced array int (&refs)[10]=arr; The correct array reference refers to an array containing 10 integers
3.31 create an array so that the value of each element is the value of its subscript
#include<iostream> using namespace std; int main() { int a[10]; for(int i=0;i<10;i++) a[i]=i; for(int j=0;j<10;j++) cout<<a[j]<<" "; cout<<endl; }
3.32 copy the array in the above question to another array
#include<iostream> using namespace std; int main() { int a[10],b[10]; for(int i=0;i<10;i++) { a[i]=i; b[i]=a[i]; } for(int j=0;j<10;j++) cout<<b[j]<<" "; cout<<endl; }
Implemented with vector
#include<iostream> #include<vector> using namespace std; int main() { vector<int> a,b; for(int i=0;i<10;i++) { a.push_back(i); } for(int i=0;i<10;i++) { b.push_back(a[i]); } for(auto &i:b) cout<<i<<" "; cout << endl; return 0; }
Pointers and arrays
When using an array, the compiler will convert it into a pointer. Using the address fetcher on the array can obtain a pointer to the element:
string *p=&num[0]; Where an array name is used, the compiler automatically replaces it with a pointer to the first address of the array string *p2=num;//Equivalent P2 = & num [0] auto ia2(num); //ia2 is a string pointer to the first element of num
Pointers are also iterators. In order to make pointers safer to use, C++11 introduces two functions called begin and end
int a[]={1,2,3,4}; int *beg=begin(a); //Point to first address int *last=end(a); //Points to the next address of the tail element The main tail pointer does not participate in dereference and increment operations
The operation of pointer is completely consistent with that of iterator. Please refer to the iterator above for specific usage.
Dereference and pointer
Pointer plus an integer results in a pointer. If the result pointer points to an element, dereference of the result pointer is allowed:
int a[]={1,2,3,4}; int last=*(a+2);
Expression * (a+2) calculates the new address after the first two elements of A. dereference is to take the contents, which is equivalent to a[2];
Subscript and pointer
The pointer points to the elements in the array, which can perform subscript operation:
int *p=&a[2]; //p points to the element with index 2 int j=p[1]; //p[1] is equivalent to * (p+1), which is the element represented by a[3] int k=p[-2]; //p[-2] represents the element represented by a[0]
3.35 write a program to set all the values in the array to zero with a pointer
#include<iostream> using namespace std; int main() { const int sz=100; int a[sz],*p; cout<<"The original number is"<<endl; for(int i=0;i<sz;i++) { a[i]=rand()%100; cout<<a[i]<<" "; } p=begin(a); while(p !=end(a)) { *p=0; p++; } cout<<endl<<"The revised content is" <<endl; for(auto &i:a) { cout<<i<<" "; } return 0; }
C style string
Not recommended