Arrays Vectors and Strings Allocation and referencing Array

  • Slides: 18
Download presentation
Arrays, Vectors, and Strings Allocation and referencing

Arrays, Vectors, and Strings Allocation and referencing

Array Type • Arrays are a compound type • Base_type name[size]; • Array dimension

Array Type • Arrays are a compound type • Base_type name[size]; • Array dimension specifies the number of elements • Dimension must be a positive integer literal OR a constant expression (i. e. , known at compile time)

Array Examples cnt = 100; // not a const expr constexpr max = 100;

Array Examples cnt = 100; // not a const expr constexpr max = 100; // a const expr int array 1[100]; // good int array 2[max]; // good int array 3[cnt]; // error – not const int array 4[getsize(something)]; // good iff getsize() is constexpr

Arrays • Array indices run from 0 to Max-1 • Always check your index!!

Arrays • Array indices run from 0 to Max-1 • Always check your index!! • Can reference element by index or by pointer dereference • A[i] or *(A + i) • Type allows compiler to compute correct offset into memory

Arrays int array 1[5]={1, 2, 4, 8, 16}; RAM char name[5]=“Mary”; int *ptr =

Arrays int array 1[5]={1, 2, 4, 8, 16}; RAM char name[5]=“Mary”; int *ptr = &array 1[0]; Symbol Table Identifier Type Location array 1 int* 0 xfa 18 name char* 0 xfa 2 c ptr int* 0 xfa 18 Note: null terminated ‘’ ‘y’ ‘a’ ‘r’ ‘M’ 0 x 00000010 0 x 00000008 0 x 00000004 0 x 00000002 0 x 00000001 Address 0 xfa 58 0 xfa 54 0 xfa 50 0 xfa 4 c 0 xfa 48 0 xfa 44 0 xfa 40 0 xfa 3 c 0 xfa 38 0 xfa 34 0 xfa 30 0 xfa 2 c 0 xfa 28 0 xfa 24 0 xfa 20 0 xfa 1 c 0 xfa 18 0 xfa 14 0 xfa 10

2 -D Arrays • C++ does not really have multi-D arrays • Looks kind

2 -D Arrays • C++ does not really have multi-D arrays • Looks kind of like it: int A[M][N]; • Arrays are really pointers to the first element in a consecutively stored sequence of elements of same type • 2 -D array is really pointer to a 1 -D array of pointers to first row elements

C-Style Strings • C++ has a String class • Can be referenced by index

C-Style Strings • C++ has a String class • Can be referenced by index like array • But it is a true object • C strings are not a primitive type, nor are they a struct • A C-style string is just a 1 -D array of char, with NULL termination • NOTA BENE: always a '' at end!!!!

2 -D Arrays char names[3]={“Julian”, “James”, “John”}; Symbol Table Identifier Type Location array 1

2 -D Arrays char names[3]={“Julian”, “James”, “John”}; Symbol Table Identifier Type Location array 1 int* 0 xfa 18 name char* 0 xfa 2 c ptr int* 0 xfa 18 names char*[] 0 xfa 34 RAM ? ? ‘’ ‘n’ ‘h’ ‘o’ ‘J’ ‘’ ‘s’ ‘e’ ‘m’ ‘a’ ‘J’ ‘’ ‘n’ ‘a’ ‘i’ ‘l’ ‘u’ ‘J’ 0 x 0000 fa 4 d 0 x 0000 fa 47 0 x 00000 fa 40 ‘’ ‘y’ ‘a’ ‘r’ ‘M’ 0 x 00000010 0 x 00000008 0 x 00000004 0 x 00000002 0 x 00000001 Address 0 xfa 58 0 xfa 54 0 xfa 50 0 xfa 4 c 0 xfa 48 0 xfa 44 0 xfa 40 0 xfa 3 c 0 xfa 38 0 xfa 34 0 xfa 30 0 xfa 2 c 0 xfa 28 0 xfa 24 0 xfa 20 0 xfa 1 c 0 xfa 18 0 xfa 14 0 xfa 10

C-Style Strings and Chars • Remember, char and string are not the same •

C-Style Strings and Chars • Remember, char and string are not the same • 'a' is a char literal – uses one byte of RAM • “a” is a string literal – uses two bytes of RAM • Name[] = “Joe”; - uses. . . 4 bytes for the 3 characters plus null character • Char *name – allocate pointer ONLY!! • <strings. h> library – many functions

C String Library #include <strings. h> i=strlen(p); // string length less null i=strcmp(p 1,

C String Library #include <strings. h> i=strlen(p); // string length less null i=strcmp(p 1, p 2); // neg if p 1<p 2, etc. p 1=strcat(p 1, p 2); // appends p 2 to p 1=strcpy(p 1, p 2); // copies p 2 to p 1 /*** WARNING – NO BOUNDS CHECKING! ***/ /* use these safe versions below! */ i=strncmp(p 1, p 2, n); // … only up to n p 1=strncat(p 1, p 2, n); // … only up to n p 1=strncpy(p 1, p 2, n); // … only up to n

C String Library #include <strings. h> size_t strlen(char s[])// not int! int atoi(char s[])

C String Library #include <strings. h> size_t strlen(char s[])// not int! int atoi(char s[]) // int value of s[] double atof(char s[]) // double value long atol(char s[]) // long value void itoa(int val, char s[], int radix) // converts integer value to string

C++ Strings • C++ has string type #include <string> using std: : string; string

C++ Strings • C++ has string type #include <string> using std: : string; string s 1; // default to empty string s 2=s 1; // s 2 is a *copy* of s 1 string s 3=“Joe”; // s 3 is copy of literal string s 4=(10, ’c’); // s 4 is ccccc while (cin >> word) …// whitespace delim while (getline(cin, line)) … // n delim

Strings & C-Strings string s(“Hello World”); char *str=s; // error! const char *str =

Strings & C-Strings string s(“Hello World”); char *str=s; // error! const char *str = s. c_str(); // OK /* Achtung! Contents of *str may change * Copy into local version if need * continued access to contents */ Use s. insert(), s. erase(), s. assign(), s. append(), and s. replace() on strings

Vectors • Vectors very similar to arrays • Except they are class templates, not

Vectors • Vectors very similar to arrays • Except they are class templates, not a type – must include type in declaration • Take care of memory management • Use push_back() to expand

Vector Initialization vector<double> dvec; // ivec empty vector<int> ivec(10, 5); /* 10 ints all

Vector Initialization vector<double> dvec; // ivec empty vector<int> ivec(10, 5); /* 10 ints all 10 have value 5 */ vector<int> ivec 2(ivec); // copy ivec vector<int> ivec 3 = ivec; // also copy vector<T> vec 4(10); /* 10 item vector of type T objs default init */ vector<int> ivec 5{2, 3, 5, 7, 11, 13} // 6 elements with values given vector<int> ivec 6 = {1, 2, 4, 8, 16} // 5 elements with values given

Vector Initialization vector<int> ivec 1(10); // 10 ints all value 0 vector<int> ivec 2{10};

Vector Initialization vector<int> ivec 1(10); // 10 ints all value 0 vector<int> ivec 2{10}; // one int value 10 vector<int> ivec 3(10, 5); // 10 ints all value 5 vector<int> ivec 4{10, 5}; // 2 ints, values 10 & 5 vector<string> sv 1{“Al”, “Mo”, “Jo”}; // list initialization vector<string> sv 2(“Al”, “Mo”, “Jo”); // error

Adding to a Vector • Vector size may not be known • Vector may

Adding to a Vector • Vector size may not be known • Vector may be large • Use push_back member function vector<int> iv; for (int i=0; i != MAX; ++i) iv. push_back(i*i); vector<string> text; while (cin >> word) // word is string text. push_back(word); Subscripting does not add elements!

Range For • Auto type definition if unknown • Range for – like “foreach”

Range For • Auto type definition if unknown • Range for – like “foreach” vector<int> iv 2{1, 2, 3, 4, 5, 6, 7, 8, 9}; for (auto &i=0 : iv 2) // reference! i *= i; // square elements for (auto i : iv 2) // non-reference cout << i << endl;