c/c + + arrays and pointers

c/c + + arrays and pointers

Knowledge points

1. Array is a pointer, corresponding to test1 in the code

2. Declare with auto to get a pointer, corresponding to test2 in the code

3. Declare with decltype. The result is not a pointer, corresponding to test3 in the code

4. Use the pointer to simulate end, corresponding to test4 in the code

5. Standard library functions std::begin, std::end, corresponding to test5 in the code

6. ptrdiff_t and size_t. ptrdiff is the type of the added and subtracted value of array subscript, and size_t is the type of array subscript, corresponding to test6 in the code

7. The subscript of array can be negative, the subscript of standard library string and vector can also be negative, corresponding to test7 in the code

8. Initialize the vector with an array. Note that the elements in the generated vector do not include the second parameter, which corresponds to test8 in the code

#include <iostream>
#include <vector>

using namespace std;

int main(){

  //test1 array is a pointer                                                          
  /*                                                                            
  string ar[] = {"aa","bb"};                                                    
  string* p = ar;                                                               
  *p = "cc";                                                                    
  string* p1 = &ar[1];                                                          
  *p1 = "dd";                                                                   
  for(auto s : ar){                                                             
    cout << s;                                                                  
  }                                                                             
  cout << endl;                                                                 
  */

  //test2 is declared with auto, and the result is a pointer                                              
  /*                                                                            
  int ia[] = {1,2,3,5};                                                         
  //ia1 Is an integer pointer                                                               
  auto ia1(ia);                                                                 
  *ia1 = 9;                                                                     
  auto ia2(&ia[3]);                                                             
  *ia2 = 8;                                                                     
  for(auto s : ia){                                                             
    cout << s << ",";                                                           
  }                                                                             
  cout << endl;                                                                 
  */

  //test3 is declared with decltype and the result is not a pointer                                        
  /*                                                                            
  int ia[3];                                                                    
  decltype(ia) ia3 = {2,3,4};                                                   
  //ia3 = &ia[0];//Compile error                                                     
  ia3[2] = 9;                                                                   
  for(auto s : ia3){                                                            
    cout << s << ",";                                                           
  }                                                                             
  cout << endl;                                                                 
  */

  //test4 simulate end with pointer                                                         
  /*                                                                            
  int arr[] = {0,1,2};                                                          
  int* end = &arr[3];                                                           
  for(int* beg = arr;beg != end; ++ beg){                                       
    cout << *beg;                                                               
  }                                                                             
  cout << endl;                                                                 
  */

  //test5 standard library functions std::begin, std::end                                        
  /*                                                                            
  int arr[] = {0,1,2};                                                          
  int* beg = std::begin(arr);                                                   
  //int* end = &arr[3];                                                         
  int* end = std::end(arr);                                                     
  for(;beg != end; ++ beg){                                                     
    cout << *beg;                                                               
  }                                                                             
  cout << endl;                                                                 
  */

  //TEST6 ptrdiff and size                                                     
  //ptrdiff is the type of the value added and subtracted by the array subscript, and size Φ t is the type of the array subscript                   
  /*                                                                            
  int arr[] = {1,2,3,4,5};                                                      
  int* b = std::begin(arr);                                                     
  int* e = std::end(arr);                                                       
  ptrdiff_t juli = e - b - 1;                                                   
  cout << juli << endl;                                                         
  size_t sz = juli;                                                             
  cout << arr[sz] << endl;                                                      
  */

  //The subscript of test7 array can be negative, the subscript of standard library string and vector can also be negative         
  /*                                                                            
  int ia[] = {1,2,3,4,5};                                                       
  int* p = &ia[2];                                                              
  int j = p[1];//Equivalent to * (p + 1), i.e. ia[3]                                     
  cout << j << endl;                                                           
  int k = p[-2];//Equivalent to * (p - 2), i.e. ia[0]                                   
  cout << k << endl;                                                            
  string s("abcde");                                                            
  char* p1 = &s[2];                                                             
  cout << p1[-1] << endl;                                                       
  vector<int> v{1,2,3,4};                                                       
  int* p2 = &v[3];                                                              
  cout << p2[-2] << endl;                                                       
  */

  //test8 initializes the vector with an array. Note that the elements in the generated vector do not include the second parameter           
  /*                                                                            
  int ia[] = {0,1,2,3,4,5,6};                                                   
  vector<int> v(std::begin(ia), std::end(ia));                                  
  for(auto s : v){                                                              
    cout << s << ",";                                                           
  }                                                                             
  cout << endl;                                                                 
  vector<int> v1(ia + 1, ia + 4);                                               
  for(auto s : v1){                                                             
    cout << s << ",";                                                           
  }                                                                             
  cout << endl;                                                                 
  */

}

QQ group of mutual learning in c/c + +: 877684258

My wechat: xiaoshitou5854

Keywords: C++

Added by phpflixnewbie on Wed, 01 Jan 2020 13:41:09 +0200