08 lecture

08 lecture - for (int i=0; i < z.size( ); i++) {...

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

View Full Document Right Arrow Icon
5/12/08 Homework 6: house class: -fields: int address point location -member functions: draw_house accessors street class: -fields: house first house last int number_on_street -member functions: draw_street midterm: 5,6, 7 and 9.1-9.3 vectors: #include <vector> declare: vector <int> z(10); - you tell it what you want it to be in the brackets (< >) and then you declare what you want it to be (in this case z) and then how many you want in the following parentheses -int/doubles will be set to zero, strings will be left empty, objects will have default construction vector elements are indexed just like strings: 0,1,2. ... -access is: z[index] if you wanted to change something or call some part of the lsit you would use this -you put the posion of the variable you want to work with inside the brackets member functions: -z.size() -z.push_back(a): increases the vectors size by one by adding whatever a is to the end of the vector -z.pop_back(): removes the last element of the vector common loop for visiting all elements:
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: for (int i=0; i &lt; z.size( ); i++) { z[i]; cout &lt;&lt; z[ i ] &lt;&lt; &quot;\n&quot;; } adding and removing elements to the middle of a vector: int main() { vector &lt;double&gt; z(10); int i; for (i=0; i&lt;10; i++) { z[ i ] = i; { //this will remove an element from the middle of the vector. int pos = 5; for (i=pos; i &lt; z.size()-1; i++) { z[ i ] = z[ i+1]; } //this takes the given position and in a sense removes it by moving all the integers around it up. after this you would want to delete the last integer of the vector and you will have successfully removed a variable //this will add an element to the middle of the vector int pos = 6; int last = z.size()-1; z.push_back (z[last]); for (i=last; i&gt;pos; i--); { z [ i ] = z [ i-1 ]; } z [ pos ] = 92; // this will add an element by starting to reassign the values of the elements starting at the given value of 'pos' for (int i=0; i &lt; z.size( ); i++) { z[i]; cout &lt;&lt; z[ i ] &lt;&lt; &quot;\n&quot;; }...
View Full Document

This note was uploaded on 05/19/2008 for the course PIC 10A taught by Professor Wittman during the Spring '08 term at UCLA.

Page1 / 3

08 lecture - for (int i=0; i &amp;amp;lt; z.size( ); i++) {...

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

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