220 likes | 385 Views
Chapter 1 Arrays, Pointers, and Structures. Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2009. Outline. Arrays (first-class arrays, using vector) Strings (using string) Pointers Dynamic allocation Reference variables and parameter passing mechanisms Structures.
E N D
Chapter 1 Arrays, Pointers, and Structures Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2009
Outline • Arrays (first-class arrays, using vector) • Strings (using string) • Pointers • Dynamic allocation • Reference variables and parameter passing mechanisms • Structures
1.1 What are Arrays, Pointers, and Structures? Aggregate: collection of objects stored in one unit Arrays: indexed collections of identical-type objects. (aggregate) Pointers: objects are used to access other objects. Structures: collections of objects that need not be of the same type. (aggregate)
1.2 Arrays and Strings Arrays: indexed collections of identical-type objects. Array always start on 0 Arrays can be used in two different ways: primitive arrays and vectors. Vectors vs. Primitive Arrays Vector has variable size and supports many manipulation functions Primitive Array has fixed size, and support only a few of manipulation functions.
Vectors #include <vector> Declaration: vector<type> identifiers(size); Example: vector<int> a(3); Vector can be indexed as an array, using [ ]
Vector example #include <iostream> #include <vector> using namespace std; int main() { vector<int> aa(3); cout<<"Hellow World!!!!!!!"<<endl; aa[0]=0; aa[1]=1; aa[2]=2; for(int i=0; i<aa.size(); i++) cout<<aa[i]<<endl; char myLine[100]; cin.getline(myLine,100); }
Vectors Vector member functions: vector<int> v(10); v.at() // equal to v[ ] v.size() // return the number of elements in v v.front() // return the first element in v v.back() // return the last element in v v.clear() // delete all the element in v v.empty() // return 1 if v is empty; else return 0 v.resize( val ) // change size of v to val v.pop_back() // remove the last element in v v.push_back( val ) // appends val to the end of v v.capacity() // return the number of element that vector can hold before it will need to allocate more space
How to do Vector’s resize Example: vector<int> arr(10); arr.resize(12);
Strings #include<string> string s=“Hello World!”; Int size=s.length(); cout<< s[4] <<endl; // result:“o” cout<< s <<endl; +, +=: concatenation
Multidimentional Arrays • A multidimentional array is an array that is accessed by more than one index. • An example to declare a 2D array: int aa[3][3]
2D array example #include <iostream> using namespace std; int main() { int num_i=3, num_j=3; int aa[num_i][num_j]; aa[0][0]=10; aa[0][1]=10; aa[0][2]=10; aa[1][0]=10; aa[1][1]=10; aa[1][2]=10; aa[2][0]=10; aa[2][1]=10; aa[2][2]=10; for(int i=0; i<3; i++) for(int j=0; j<3; j++) cout<<aa[i][j]<<endl; char myLine[100]; cin.getline(myLine,100); }
Pointers • Declare a pointer int *ptr; • & : referencing operator that returns the address of the object. • * : dereferencing operator which can access data through a pointer
Pointers int *ptr; int x = 5; int y = 7; We can make ptr point at x by assigning to ptr the memory location where x is stored ptr = &x
Pointers • Or we can just simply assign a value to ptr by using dereferencing operator *: *ptr = 10;
To help you memory • If we define int *ptr; Then ptr itself is an address, therefore, if ptr = “something”; this “something” must be an address
To help you memory • If we define int *ptr; Then *ptr itself is a value, therefore, if *ptr = “something”; this “something” must be a value
Pointers • Of course, we can have something like (but not meaningful) int *ptr = &x;
Pointer Example #include <iostream> using namespace std; int main() { int x=5, y=7; int *ptr; ptr=&y; cout << *ptr << endl; char myLine[100]; cin.getline(myLine,100); }
Pointer Example #include <iostream> using namespace std; int main() { int x=5, y=7; int *ptr; ptr=&y; cout << *ptr << endl; cout << ptr << endl; *ptr+=1; cout << *ptr << endl; cout << ptr << endl; *ptr++; cout << *ptr << endl; cout << ptr << endl; char myLine[100]; cin.getline(myLine,100); }
Structures Stores a collection of objects that need not be of the same type. Each object in the structure is a member and is accessed by applying the dot (.) member operator, not index as in array. In C, structures are used quite often. But in C++, they are replaced by classes at most cases.
Structures Struct Student{ string firstName; string lastName; … }; Student a; // declaring Student a.firstName = “Mary”;//accessing member data
Linked List struct Node { int value; Node *next };