12 - Vectors and Vectors of Vectors, oh my!

12 - Vectors and Vectors of Vectors, oh my! - Engineering...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Quote of the Day The High Life From building cars, planes, and trains To building drugs, plugs, and cranes, We exist on the fundamentals through struggle and pain, Doing math and science to help the world have less strain. With our video games and quirky jokes, All of us unite in someway until we croak. Our actions, though they cause some to jeer, Allow us to proudly say, "I am an engineer!" Vectors and Vectors of Vectors, oh my! my! - Tejash Patel, ENGR 101 (W'09) Making Lists of Things Often times we want to make lists of things that are not characters For these we can use another class called the vector class #include <vector> Vectors Vectors can be used to make lists of anything: int, double, char, string, .... To declare a vector you have to specify the type you are making a list of. vector <type> identifier; Vectors For example: Vectors Typically we also want to tell how long this list will be: vector <int> intlist(5); vector <double> doublelist(10); vector <string> stringlist(25); vector <int> intlist; vector <double> doublelist; vector <string> stringlist; 1 Vectors We can also specify what to initialize the values in the list to: vector <int> intlist(5, -1); vector <double> doublelist(10, 0.0); vector <string> stringlist(25, "none"); Vectors Individual parts of the list can be accessed using array semantics vector <int> intlist(5, -1); intlist[ 1 ] = 10; Vectors Individual parts of the list can be accessed using array semantics vector <int> intlist(5, -1); intlist[ 1 ] = 10; for(i=2; i < 5; i = i + 1) intlist[ i ] = intlist[ i-1 ] + 1; i- Vectors Alternately we can use the safer at( ) method to index the list vector <int> intlist(5, -1); intlist.at(1) = 10; for(i=2; i < 5; i = i + 1) intlist.at( i ) = intlist. at( i-1 ) + 1; i- Exercise What will be output? 1- 2 4 8 16 32 2- 32 16 8 4 2 3- 2 2 2 2 2 4- 2 2 2 4 2 What will be output? 1- 1 2 4 8 16 2- 16 8 4 2 1 3- 2 4 8 16 32 4- 32 16 8 4 2 Exercise ; ; 2 Vectors Other methods also exist to manipulate vectors: v.back( ) v.front( ) v.empty( ) v.clear( ) refers to the last element refers to the first element true if the vector is empty empties the vector Vectors Other methods also exist to manipulate vectors: v.pop_back( ) v.push_back(e) v.resize(newsize) v.resize(newsize,e) removes the last element appends another element changes size of the vector and puts e into any new elements What is the purpose of this code? 1- Print out each word in a line separately 3- Remove all the spaces in a line 2- Read successive lines into a vector of strings 24- Read each word in a line into a vector of strings Which of the functions will successfully return the string containing the letters in the incoming string, but in reverse order? reverse("hello")=="olleh" 1. 2. 3. Vectors Vectors can also be used to store lists of lists vector< vector <int> > table; Vectors of Vectors If you think of vector <int> as a row of integers then you can think of vector <vector <int> > as a series of rows vector <vector <int> > table; table[0] = 1 2 3 5 7 Note this is table[1] = 4 2 7 3 0 just a schematic table[2] = 5 5 -1 2 6 you can't actually assign arrays this way Don't forget the space here! 3 Vectors of Vectors To access an individual number you need to designate which item in the row vector <vector <int> > table; table[0] = 1 table[1] = 4 2 2 3 7 5 3 7 0 Vectors of Vectors Uses of Vectors of Vectors: Tables of data double Ex, Ey, Ez; double Mx, My, Mz; double Cx, Cy, Cz; //earth data //moon data //comet data table[2] = 5 5 -1 table[0][2] == 3 2 6 table[2][3] == 2 Vectors of Vectors Using of Vectors of Vectors: Tables of data vector <double> Earth(3, 0.0); vector <double> Moon(3, 0.0); vector <double> Comet(3, 0.0); //earth data //moon data //comet data Vectors of Vectors Using of Vectors of Vectors: Tables of data vector <vector <double> > PlanetData; Vectors of Vectors Creating of Vectors of Vectors: Assigning data to the elements vector <vector <double> > PlanetData(3); vector <double> row(3, 0.0); for(int i=0; i < 3; i=i+1){ PlanetData[i] = row; } Vectors of Vectors Creating of Vectors of Vectors: An easier way of assigning data to the elements vector <vector <double> > PlanetData(3); for(int i=0; i < 3; i=i+1){ PlanetData[i] .assign(3, 0.0); } 4 Vectors of Vectors Creating of Vectors of Vectors: The easiest way of assigning data to the elements vector <double> row(3, 0.0); vector <vector <double> > PlanetData(3, row); Vectors of Vectors Accessing of Vectors of Vectors: Printing the data vector <double> row(3, 0.0); vector <vector <double> > PlanetData(3, row); int i, j; for(i=0; i < PlanetData.size(); i=i+1){ for(j=0; j < PlanetData[ i ].size(); j=j+1) cout << PlanetData[ i ][ j ] << " "; cout << endl; } What will the contents of table be after the for loops execute? Vectors of Vectors Accessing of Vectors of Vectors: Printing the data vector <double> row(3, 0.0); vector <vector <double> > PlanetData(3, row); int i, j; for(i=0; i < PlanetData.size(); i=i+1){ for(j=0; j < PlanetData.at( i ).size(); j=j+1) PlanetData.at( ).size(); cout << PlanetData.at( i ).at( j ) << " "; PlanetData.at( cout << endl; } 1 0 0 0 0 1 2 0 2 4 2 0 1 2 0 2 4 0 3 6 3 1 2 3 2 4 6 3 6 9 4 1 1 2 1 2 4 1 3 6 Next Lecture Sorting and Recursion 5 ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern