New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here


by: Santos Fadel


Santos Fadel
GPA 3.76


Almost Ready


These notes were just uploaded, and will be ready to view shortly.

Purchase these notes here, or revisit this page.

Either way, we'll remind you when they're ready :)

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Class Notes
25 ?




Popular in Course

Popular in ComputerScienence

This 9 page Class Notes was uploaded by Santos Fadel on Monday October 19, 2015. The Class Notes belongs to CSCI 1200 at Rensselaer Polytechnic Institute taught by Staff in Fall. Since its upload, it has received 13 views. For similar materials see /class/224865/csci-1200-rensselaer-polytechnic-institute in ComputerScienence at Rensselaer Polytechnic Institute.

Similar to CSCI 1200 at RPI

Popular in ComputerScienence




Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 10/19/15
Computer Science II i CSci 1200 Lecture 8 i Iterators Programming Examples Test 1 o 102 total points on the test Therefore7 your score is out of 102 0 Class average 896 102 0 Distribution 0 Solutions are posted on line Reminder If you did not receive credit for Checkpoint 3 in last week s lab Lab 47 you may show it to a TA at the start of lab this week in order to earn credit This applies to last week s lab only Review from Lecture 7 Koenig amp Moo Chapter 51 55 c We wrote several versions of a program to maintain a class enrollment list and an associated waiting list 0 The rst version used vectors to store the information Unfortunately7 erasing items from vectors is inefficient o In the second version7 we explored iterators and iterator operations as a different means of manipulating the contents of the vector 0 This allows us to replace the vector with a list in the third version We will review this solution today 7 Of particular note7 there is an erase function for both vectors and lists The vector erase function does pretty much what we did in our enrollment example program The list erase function is mush more efficient c As we will see7 for the enrollment problem the list is a better sequential container class than the vector Todayls Class 7 Review Iterators Lists Programming EX amples o Returning references to member variables from member functions 0 Lists 0 Review of iterators and iterator operations 0 Differences between indices and iterators 0 Differences between lists and vectors 0 Prime number programming example References and Return Values There is one part of the Student class that we did not discuss thoroughly in Lecture 6 and I d like to discuss it now The topic is returning references to objects Here is an excerpt from the code from Lecture 6 class Student public bool read istreamamp instr unsigned int numhomeworks unsigned int numtests void computeaverages double hwweight const stringamp firstname const return firstname const stringamp lastname const return lastname etc private string firstname string lastname string idnumber etc o A reference is an alias for another variable For example string one quotTommyquot stringamp two one two is a reference to one two1 i cout ltlt one ltlt endl This outputs the string Timmy The reference variable two refers to the same string as variable one Therefore7 when we change two7 we are changing one 7 Exactly the same thing occurs with reference parameters to func tions 0 Returning to the student grades program7 in the main function we had a vector of students vectorltStudentgt students Based on our discussion of references and looking at the class declara tion above the question arises what if we wrote string amp fname fname1 i students i firstname Would the code then be changing the internal contents of the i th Student object The answer is NO The reason is that the Student class function firstname returns a const reference The compiler will complain that the above code is attempting to assign a const reference to a non const reference variable If you instead wrote const string amp fname fname1 i students i firstname The compiler would complain that you are trying to change a const object Hence in both cases you d be safe HOWEVER you d get yourself in trouble if the member function re turn type was only a reference and not a const reference Then you could access and change the internal contents of an object This is a bad idea in most cases Lists Our second standard library container class Lists are formed as a sequentially linked structure instead of the array like7 random access indexing structure of vectors 7 Pictures showing the differences will be drawn in class Lists have pushiront and popiront functions in addition to the pushback and popback functions of vector Erasing is very efficient for a list 7 this is independent of the size of the list We can t use the standard sort function we must use a special sort function de ned by the list type Lists have no subscripting operation Iterators and Iterator Operations 7 General An iterator type is de ned by each container class For example vectorltdoublegtz iterator p listltstringgtz iterator q There are also string iterators which work just like vector iterators An iterator is assigned to a speci c location in a container For exam ple ith location in the vector first entry in the list p vbegin i q rbegin when v is a vector of doubles and r is a list of strings The contents of the speci c entry referred to by an iterator are accessed using the operator For example p 314 cout ltlt q ltlt endl q quotHelloquot In the rst and third lines p and q are l values In the second q is an r value Stepping through a container either forward and backward is done using increment and decrement Hence p C1quot moves p to the next location in the vector and moves q to the previous location in the list Neither operation changes any of the contents of vector v or list r Finally we can change the container that an iterator p attached to as long as the types are correct Thus if v and w are both vectorltdoublegt then p vbegin p 314 changes lst entry in v p wbegin 2 p 278 changes 3rd entry in w are all valid because p is a vectorltdoublegtz iterator but if a is a vectorltstringgt then p abegin is a syntax error because of a type clashl Iterators and Iterator Operations 7 Vector Iterators Vector and string iterators have special capabilities that other container iterators list for now7 but others later do not have 0 Initialization at a random spot in the vector p vbegin i o Jumping around inside the vector through addition and subtraction of location counts p p 5 moves p 5 locations further in the vector 0 Neither of these is allowed for list iterators and most other iterators7 for that matter because of the way containers are built Iterators vs Indices for Vectors and Strings Students are often confused by the difference between iterators and indices for vectors 0 Consider the following declarations vectorltdoublegt a10 25 b20 11 vectorltstringgt CS stringquothiquot vectorltdoublegtz iterator p abegin 5 unsigned int i5 o Iterator p refers to location 5 in vector 21 The value stored there is directly accessed through the operator p 6 O This has changed the contents of vector 21 c We can also access the contents of vector a using subscripting c We will treat i as the subscript It is not attached to vector a in any way We write cout ltlt ai ltlt endl to access and output the value stored at location 5 of 21 Lists vs Vectors 0 Lists are a chain of separate memory blocks one block for each entry 0 Vectors are formed as a contiguous and bigger block of memory 0 Lists therefore allow easyfast insert and remove in the middle but not indexing o Vectors therefore allow indexing which depends on jumping around inside the block of memory but slow insert and remove in the middle Erase 0 Lists and vectors each have a special member function called erase o In particular given list of ints 5 consider the example listltintgtz iterator p sbegin p listltintgtz iterator q serasep 0 After the function erase 7 The integer stored in the second entry of the list has been re moved 7 The size of the list has shrunk by one 7 The iterator p does not refer to a valid entry 7 The iterator q refers to the item that was the third entry and is now the second 0 You will often see code like the above written listltintgtz iterator p sbegin p p s erasep This makes iterator p refer to a valid entry 0 Our eraseiromwector function from the Lecture 7 enrollment ex ample becomes simply p verasep 0 Even though this has the same syntax for vectors and for list7 the vector version is 0717 whereas the list version is 01 Prime Numbers Sieve 0f Eratosthenes c We will explore the problem of nding all primes less than a given integer7 n and introduce the Sieve of Eratosthenes algorithm The algorithm is a casting out77 algorithm each new prime is used to cast out all of its multiples from a list of potential primes We will implement this during lecture using the skeleton program provided on the next page of this handout Central to the code will be the use of iterators


Buy Material

Are you sure you want to buy this material for

25 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

Allison Fischer University of Alabama

"I signed up to be an Elite Notetaker with 2 of my sorority sisters this semester. We just posted our notes weekly and were each making over $600 per month. I LOVE StudySoup!"

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Parker Thompson 500 Startups

"It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.