C + + Primer (No. 456 chapter)

Source: Internet
Author: User

Arrays and pointers

  1. Array vs vector: The array length is fixed and the array length is not known; arrays cannot be copied directly or assigned vectors can be
  2. Character array particularity: Char chr1[3]={' C ', ' + ', ' + '};char chr2[4]= "C + +"; the latter will put a null character in the last way, representing the end of the character array, requiring a number of characters + 1 lengths
  3. Array Subscript Index: Subscript type is size_t (unsigned integer large enough) pointer index: vector-like iterator index
  4. Should try to avoid using arrays and pointers error-prone C + + should use vector iterator string, etc. instead
  5. pointer: string *str; string* str; both, usually using the former, string *str1, *STR2 (both pointers); srting* Str1,str2 (people mistakenly think that both pointers are actually only str1 Yes );
  6. Pointers: Be sure to initialize, when pointing to the object has not been established can be initially 0/null, NULL is inherited from C language, is a preprocessing variable, compile-time processing as 0,null is not the STD space does not need std::null
  7. void * Pointer: a particular can hold the address of any type of object, but cannot be used to manipulate the object it points to
  8. Pointer reference differences: references must be initialized and point to only one object assignment operation has different meanings
  9. Using an array name in an expression automatically translates to a pointer to the first element of the array
  10. Pointer subtraction to get data type ptrdiff_t (signed integer is large enough)
  11. A const pointer to a const object pointing to a const pointer to a const object
    Const int*a;//the object is a const pointer that can point to another but cannot modify the value of the object by a pointerint*ConstA//pointer to a const pointer to a value that cannot be modified to point to an object can be modifiedConst int*ConstA//const pointer to const objectConst intI=1;int*p=&i;//Error cannot assign the address of a const object to a normal pointer to a non-const objectvoid*p=&i;//ErrorConst int*p=&i;//OKConst void*p=&i;//OK
    String *pstring;   const Pstring CStr; // CStr is a string *const type the following three kinds of expressions are the same meaning
    String *const sctr;
    pstring Const SCTR;
  12. Dynamic arrays: Dynamic arrays are determined at run time. The array-length heap space is used to store dynamically allocated objects The new Delete implementation requires the Delete to free up space before the string *str=new String[n] ();d elete [] str;
  13. compatibility with old and new code: all places using string literals in strings You can use C-style strings instead of vice versa; use arrays to initialize vectors;
     char  *chr=str; // error  const  char  *chr=str.c_str (); //  Returns a const pointer to prevent the CHR pointer from modifying the STR object  int  arr[]={0 , 1 , 2 , 3 , 4 , 5  };vector  <int  > Ivec (arr,arr+< Span style= "color: #800080;" >6 ); //  Pointer to the first element and the last element in an array-initialized vector  
  14. int ia[3[4]; int (*IP) [4]=ia; // IP pointer to int[4] A pointer to an array of 4 elements can ip+2 up to ip=&ia[2]; int *ip[4]; // array array of length four element type is int*     int int_array[4];  typedef simplifies pointer *ip=ia of multidimensional arrays  ;

C-style string

  1. C-style strings are supported by C + + but should not be easy to bring a lot of security issues in C + +
  2. The type of string literal is an array of const char types
  3. C-style strings are not C-type or C + + types are character arrays that end with null characters char chr[]={' C ', ' + ', ' + ', '};char ' chr[]= "C + +", char *chr= "C + +", all C-style strings. This is not true if NULL is not added. (const) char* to manipulate C-style strings standard library CString is a standard library of C + + that handles style characters (same as String library content in String.h C)
  4. The array needs to tell its length otherwise it cannot get its own length; character arrays do not know the length of the array without the null character, but C-style strings can be traversed by a null character to get the length so C-style string is a corresponding function can be called; note that input S is a string that is not 0 empty characters and the target string needs There is enough space; the function at the beginning of the strn is safer than the strcpy strcat and can control the number of characters copied
    Strlen (s); // return length does not contain null characters strcmp (S1,S2); // equal returns 0 S1>S2 returns a positive number otherwise returns a negative number strcat (S1,S2); // stitching to S1 and returning S1strcpy (S1,S2); // Copy to S1strncat (s1,s2,n); // The first n characters of S2 are spliced to S1strncpy (s1,s2,n); // copies the first n characters of a s2 to S1

C + + Primer (No. 456 chapter)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.