DOC PREVIEW
Saddleback CS 1C - Iterators, Lists & Stacks

This preview shows page 1-2-3-4 out of 13 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 13 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS1C – Saddleback CollegeCS1C – Advanced Programming in C++Saddleback College Spring 2008 – J. TateyamaSaddleback College Fall 2011 – J TateyamaIterators, Lists & Stacksstructs to classesConcepts of OOP (sw chapter 3)CS1C – Saddleback College2IteratorsIterators–Are a means of accessing elements in the container including finding the predecessor and successor of an element–Iterators provide a way in which the algorithms can act upon the containers–Iterators are objects that act, more or less, like pointersIterator TypesRandom Access Store and retrieve values. Elements may be accessed randomly.Bidirectional Store and retrieve values. Forward and backward moving.Forward Store and retrieve values. Forward moving only.Input Retrieve, but not store values. Forward moving only.Output Store, but not retrieve values. Forward moving only.CS1C – Saddleback College3IteratorsThe following iterator operations are supported–*iter - Returns a reference to the element that is referenced by the iterator iter–++iter - Increment iter (move to the next element)–iter++–--iter - Decrement iter (move to the previous element)–iter--–iter1 == iter2 - Compare two iterators for equality or inequality. Two iterators are equal if they both reference the same element in the same container or if they are positioned "one past the end" of the container. An iterator positioned "one past the end" is sometimes called the off-the-end iterator Note: Iterators for different container types do behave differently!–For example: the end() of a vector is not the same as the end() of a list object. A vector is treated more as an array while a list is treated as a linked list like you coded in 1B. CS1C – Saddleback College4Iterator Example with Vector#include <iostream>#include <iomanip>#include <vector>using namespace std;// This program demonstrates the use of an iterator with a vectorint main(){ // Declare vector of ints and an iterator vector<int> vals;vector<int> :: iterator p; // Loading the vector with some calculated valuesfor(int i = 0; i < 10; ++i)vals.push_back(i*2+1);// Output contents using an iterator from begin to endfor(p = vals.begin( ); p < vals.end( ); ++p)cout << *p << endl;}CS1C – Saddleback College5Iterator Example with List#include <iostream>#include <iomanip>#include <list> <<-- Note the include change!!using namespace std;// This program demonstrates the use of an iterator with a listint main(){ // Declare linked list of ints using a list object and an iterator list<int> vals; list<int> :: iterator p; // Loading the list with some calculated values for(int i = 0; i < 10; ++i) vals.push_front(i*2+1); // Output list using an iterator from begin to end for(p = vals.begin( ); p !=vals.end( ); ++p) cout << *p << endl;}Note the difference → p !=vals.end() vs. p < vals.end() used for the vector. CS1C – Saddleback College6StacksStacks are similar to Vectors except they are designed for LIFO operations (Last In, First Out)Elements are pushed/popped from the "back", which is known as the top of the stackThe ordering is kept by the association to each element of a link to the element preceding it and a link to the element following itMember functions:empty Returns whether the stack is empty, i.e. whether its size is 0.size Returns the number of elements in the stack.top Returns a reference to the last element pushed into the stack.push Adds a new element at the top of the stack, above its current top element.pop Removes the element on top of the stack; you must use top to retrieve the values before poppingCS1C – Saddleback College7 Structs to Classes In CS1B we emphasized procedural programming–We declared variables (objects to act upon) and a set of separate and independent operations (functions)–The functions could act upon any set of variables– The objects were passive - they did not have any particular set of operations defined for them–see SW 3-2 In CS 1C we are going to look at a type of programming called object-oriented programming (OOP). – One of the key concepts in OOP is that of encapsulation. –The objects (variables) in OOP are active. –They have not only data but also a set of actions that may be performed on the data.CS1C – Saddleback College8 Structs to Classes Let’s try to put the functions that operate on the data into the struct itself – see SW 3-3 What’s wrong with this?–Violates the separation of specification from implementation (black box) What will happened when we put the definitions outside of the struct and place the prototype inside the struct? see SW 3-4– Compiler error – function has no prototypeNeed to tell the compiler which structure the definitions are associated with–Format is struct name :: function name where the :: is call the scope resolution operator–Since the functions “belong” to variables of a specific type then there is no need for a formal parameter list–Using dot notation with the function calls and a call to a member function is call “message passing” – see SW3-5CS1C – Saddleback College9 Member Access What if we modified the main as follows:int main(void){DataRec theData;theData.GetData( );cout << "\nEnter name: ";cin.ignore();getline(cin,theData.name);theData.OutputData( );}  Anyone declaring a variable of type DataRec could do anything they wished to the name and age because by default all members of a struct are public CS1C – Saddleback College10 Member Access Goal: Restrict access to member variables by using member functions only  To do so, place the public member functions at the top and then specify that the data members are priviatestruct DataRec{void GetData();void OutputData();private:string name;int age;}; DataRec theData; Then the statementgetline(cin,theData.name); would produce an error message (illegal access to protected/private member)The only way the user of the struct may gain access to the private data is through the member functionsCS1C – Saddleback College11 Concepts of Object-Oriented Programming Procedural Programming Languages–Procedural languages are used to code algorithms using functions and procedures (void functions in C++)–Early languages such as FORTRAN, BASIC, and Pascal were designed to use standard algebraic formulas and this method of programming worked very well Object-Oriented Programming


View Full Document
Download Iterators, Lists & Stacks
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Iterators, Lists & Stacks and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Iterators, Lists & Stacks 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?