CS1124 Notes

CS1124 Notes - Willis Thai CS1124 1/24/07 #include...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Willis Thai CS1124 1/24/07 #include <vector> using namespace std; int main( ) { // Declaring a vector. vector<type> name; vector< int > vi; // What size? vi.size( ); // Storing into a new slot in the vector. vi.push_back( int ); vi.push_back(17); // Storing from a file. int num; ifstream ifs (“data.txt”); while ( ifs >> num) vi.push_back (num); // Displaying for ( size_t index = 0; index < vi.size( ); index++ ) cout << vi[index] << endl; // Display the last one. cout << vi[vi.size( )-1] << endl; cout << vi.back( ) << endl; // Passing into functions. void displayMyVector ( vector< int { for ( size_t index = 0; index < vi.size( ); index++ ) cout << vi[index] << endl; } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Willis Thai CS1124 2/05/07 int x = 17; int *p = &x; int *s, *t, *u; *: “pointer of type ___” operator cout << *p << endl;; *: “dereference” operator struct Person { string name; char gender; string dob; Person* spouse; // Takes up the size of an address. }; bool { if { return true; } else if ( (one.spouse != NULL) || (two.spouse != NULL) ) { return false; } else { one.spouse = &two; two.spouse = &one; return true; } } Person* f( ) { Person *pp = new Person; // Allocating memory but forgetting about it. (Memory Leak) return pp; // So you have to return. } void { string name; while ( ifs >> name ) { Person *p = new Person; p->name = name; v.push_back(p); } } int main( )
Background image of page 2
Willis Thai CS1124 { Person a, b; Person* somebody = f( ); delete somebody; // If you create something with new, you get rid of it with delete. a.name = “Jack”; a.gender = “M”; a.dob = “05/29/1917”; a.spouse = NULL; b.name = “Marilyn”; b.gender = “F”; b.dob = “06/01/1926”; b.spouse = NULL; marries( a, b ); Person* personPtr1 = &a; // All three of the following are the same. cout << a.name << endl; cout << (*personPtr1).name << endl; cout << personPtr1->name << endl; // Use this. // Name of Jack’s bride. cout << a.spouse->name << endl; cout << personPtr1->spouse->name << endl; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Willis Thai CS1124 2/7/07 const Person* p1 = &a; // a won’t be changeable. Person* const p1 = &a; // p1 will be a const pointer. p1 = &b; Person* pArray = new Person[17]; // Accessing the array. (pArray + 4)->name = “Felix”; // Fifth person in array. *(pArray+4); p[4]; // Passing arrays into functions. void foo ( int a[ ] ) or void foo ( int * a ) { } // Can’t do this. int main( ) { int bar[17]; int x = 42; bar = &x; } // Advantage of creating onto heap. int n; cin >> n; Person* pArray = new Person[n]; // Deleting an array. delete [ ] pArray;
Background image of page 4
Willis Thai CS1124 2/12/07 // class is private by default. class
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 31

CS1124 Notes - Willis Thai CS1124 1/24/07 #include...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online