1 / 15

Understanding Vectors in C++ Programming

Learn about vectors in C++ programming, including syntax, access methods, benefits over arrays, and multidimensional applications. Explore practice exercises and challenges to deepen your understanding.

tkathleen
Download Presentation

Understanding Vectors in C++ Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 202 Lesson 3 C++ Primer: Vectors

  2. There are several compiler errors and logic errors in the following code. Identify them! #include <iostream>; using namespace std; int main() { cout >> "How many items?" >> endl; int size; cin << size; int array[size]; for (int i = 0; i <= size; ++i) { cout >> "Next Item?" >> endl; cin << array[i]; } return 0; } Warmup

  3. Announcements • Labs start Today! • ENGR 104-A • Project 0 is due on Friday (9/15) • Ensure that the submit system is setup for you so that you can flawlessly submit Hw1 • Homework 1 is due on Sunday (9/17) • Testing – test the programs for bugs • Note: errors that all of them share are not considered bugs • Submit – makefile, testing files and README

  4. Arrays, Good? Arrays, Bad? Arrays are Evil, Why?

  5. Vectors, Gooooooood! • Similar to Arrays • Direct index-based access • Compact • Different than Arrays • Completely dynamic • Occasional memory re-allocation • Bounds-safe • Methods • #include <vector>

  6. Vectors • Syntax vector<type> vecName; vector<type> vecName ( size ); vector<type> vecName ( size , initVal ); • Example vector<int> integers; vector<Car> cars; vector<string> messages; vector<Student> students(100); // 100 Students vector<double> grades(100, 0.0); // 100 doubles, all 0.0

  7. Vector Access • Two methods • [index] – just like arrays • Syntax vecName[index] = newValue; temp = vecName[index]; • .at(index) – different from arrays • Syntax vecName.at(index) = newValue; temp = vecName.at(index); • NEW: dot operator • Calls a method associated with that object • vecName is the object, at() is the method

  8. Vector Access • Examples vector<double> grades(100, 0.0); grades[0] = 97.6; grades.at(1) = 86.4; grades[2] = grades.at(1) / 2.0; • Why use .at(i)? • Boundary checking! • Throws an exception if out-of-bounds If there are 10 items in a vector, what is the index of the last item?

  9. Vectors – Bigger is Better! • .push_back(item) • Add an item to back of vector vector<int> numbers(9); for (int i = 0; i < 9; ++i) numbers.at(i) = i + 1; numbers.push_back(10); What’s going on here? .push_back(10) 1 2 3 4 5 6 7 8 9 10

  10. Vectors – Other Methods • .size() • Number of elements • .capacity() • Number of elements it “can” hold (reserved…) • .erase( iterator ) • Erase the item at the iterator position • .reserve( size ) • Reserves space for size number of elements • .resize( size ) • Shrinks/grows the vector to size • .empty() • Returns true if vector is empty, false otherwise • .clear() • Erases all items in the vector

  11. Vectors - Iterators • Erase? Iterators? How does that work? • Iterator • Special “pointer” that points into the vector • NOT the index of the item! • Using an index WITH an Iterator • vecName.begin() is an iterator • Add the index to the iterator • Example numbers.erase( numbers.begin() + 3 ); • This will erase the 4th item (with index 3)

  12. Vectors - Multidimensional • Multidimensional arrays • 10 x 20 matrix, 10 rows, 20 columns int scores[10][20] • 3 x 3 x 3 matrix int cube[3][3][3] • Multidimensional vectors • A little different! • For each dimension (except last) • Push_back a collection of vectors!

  13. Vectors - Multidimensional Extra space – VERY IMPORTANT, why? • Example vector<vector<int> > integers; for (int i = 0; i < 10; ++i) { integers.push_back( vector<int>() ); for (int j = 0; j < 20; ++j) { integers[i].push_back(i * j); } } Push_back() whole vector Push_back() integer

  14. Practice • Create a vector of strings • Hint: vector<…> … • Add your first, middle and last name to the vector • Hint: …push_back(…)

  15. Challenge • Use vectors and strings • Ask the user to enter in text • Store each word in the vector • When they enter a period (as a word by itself), terminate the input • Print the text, one word on a line

More Related