1 / 18

Arrays, Vectors, and Strings

Arrays, Vectors, and Strings. Allocation and referencing. 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.

adonis
Télécharger la présentation

Arrays, Vectors, and Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays, Vectors, and Strings Allocation and referencing

  2. 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)

  3. Array Examples cnt = 100; // not a const expr constexpr max = 100; // a const expr int array1[100]; // good int array2[max]; // good int array3[cnt]; // error – not const int array4[getsize(something)]; // good iff getsize() is constexpr

  4. 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

  5. Arrays RAM Address int array1[5]={1,2,4,8,16}; char name[5]=“Mary”; int *ptr = &array1[0]; 0xfa58 0xfa54 0xfa50 0xfa4c 0xfa48 0xfa44 0xfa40 Note: null terminated 0xfa3c Symbol Table 0xfa38 0xfa34 0xfa30 ‘\0’ ‘\0’ ‘\0’ ‘\0’ Identifier Type Location ‘y’ ‘a’ ‘r’ ‘M’ 0xfa2c array1 int* 0xfa18 0x00000010 0xfa28 0xfa24 0x00000008 name char* 0xfa2c 0x00000004 0xfa20 0x00000002 0xfa1c ptr int* 0xfa18 0x00000001 0xfa18 0xfa14 0xfa10

  6. 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

  7. 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 '\0' at end!!!!

  8. 2-D Arrays RAM Address char names[3]={“Julian”, “James”,“John”}; 0xfa58 0xfa54 ? ? ‘\0’ ‘n’ 0xfa50 ‘h’ ‘o’ ‘J’ ‘\0’ 0xfa4c ‘s’ ‘e’ ‘m’ ‘a’ 0xfa48 0xfa44 ‘J’ ‘\0’ ‘n’ ‘a’ ‘i’ ‘l’ ‘u’ ‘J’ 0xfa40 0xfa3c 0x0000fa4d Symbol Table 0xfa38 0x0000fa47 0x00000fa40 0xfa34 0xfa30 ‘\0’ ‘\0’ ‘\0’ ‘\0’ Identifier Type Location ‘y’ ‘a’ ‘r’ ‘M’ 0xfa2c array1 int* 0xfa18 0x00000010 0xfa28 0xfa24 0x00000008 name char* 0xfa2c 0x00000004 0xfa20 0x00000002 0xfa1c ptr int* 0xfa18 0x00000001 0xfa18 names char*[] 0xfa34 0xfa14 0xfa10

  9. 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

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

  11. 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

  12. C++ Strings • C++ has string type • #include <string> • using std::string; • string s1; // default to empty • string s2=s1; // s2 is a *copy* of s1 • string s3=“Joe”; // s3 is copy of literal • string s4=(10,’c’); // s4 is cccccccccc • while (cin >> word) …// whitespace delim • while (getline(cin, line)) … // \n delim

  13. 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(), ands.replace() on strings

  14. 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

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

  16. Vector Initialization vector<int> ivec1(10); vector<int> ivec2{10}; vector<int> ivec3(10, 5); vector<int> ivec4{10, 5}; vector<string> sv1{“Al”,“Mo”, “Jo”}; vector<string> sv2(“Al”,“Mo”,“Jo”); // 10 ints all value 0 // one int value 10 // 10 ints all value 5 // 2 ints, values 10 & 5 // list initialization // error

  17. 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!

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

More Related