Lecture 3 Arrays, Vectors & Strings
Lecture 3 Arrays, Vectors & Strings
Built-inarrays 数组inta[5] = (1,2, 4,8, 16];aAn array is a container of unnamed objects of a single type that we access by position. ContainerUnnamed objects.Single typeint b = a[2];a[3]=7;Accessbyposition
Built-in arrays 数组 int a[5] = {1, 2, 4, 8, 16}; An array is a container of unnamed objects of a single type that we access by position. • Container • Unnamed objects • Single type • Access by position a int b = a[2]; a[3]=7;
Built-in arraysDefinition/initializationint a[5] = [1, 2, 4, 8, 16];int b[5] = [1, 2, 4];//ok.equivalenttointb[5=1,2,4,0,0:intc[]=(1,2,4,8,16];//ok.equivalent toint c[5]=(1,2,4,8,16);const size_tN=5;int d[N] = {1, 2, 4, 8, 16];
Built-in arrays int a[5] = {1, 2, 4, 8, 16}; Definition / initialization int b[5] = {1, 2, 4}; // ok. equivalent to int b[5] = {1, 2, 4, 0, 0}; int c[ ] = {1, 2, 4, 8, 16}; // ok. equivalent to int c[5] = {1, 2, 4, 8, 16}; const size_t N = 5; int d[N] = {1, 2, 4, 8, 16};
Built-in arraysYou CANNOTdothefollowingint a[4] =(1, 2, 4, 8, 16];/l error: excess elements in array initializerint b[5] = [1, 2, 4, 8, 16];int c[5] = b;ll error: array initializer must be an initializer list
Built-in arrays int a[4] = {1, 2, 4, 8, 16}; You CAN NOT do the following. // error: array initializer must be an initializer list int b[5] = {1, 2, 4, 8, 16}; int c[5] = b; // error: excess elements in array initializer
Built-in arraysArray names are converted to (constant)pointersinta[5]=(1,2,4,8,16];I/okaint *p = a;//ok.constint*q=a;int b=3;a=&b;I/ error: array type "int [5]'is not assignable
Built-in arrays int a[5] = {1, 2, 4, 8, 16}; Array names are converted to (constant) pointers. a a int *p = a; // ok. const int *q = a; // ok. int b = 3; a = &b; // error: array type ‘int [5]’is not assignable