cs 162 intro to programming ii
play

CS 162 Intro to Programming II Vectors 1 Vectors A - PowerPoint PPT Presentation

CS 162 Intro to Programming II Vectors 1 Vectors A container from the Standard Template Library Holds a set of elements, like an array Flexible number of elements - can grow and shrink


  1. CS ¡162 ¡ Intro ¡to ¡Programming ¡II ¡ Vectors ¡ 1 ¡

  2. Vectors ¡ – A container from the Standard Template Library – Holds a set of elements, like an array – Flexible number of elements - can grow and shrink Similar ¡to ¡a ¡dynamic ¡array ¡ • • Must include vector header file to use vectors #include <vector>

  3. Vectors ¡ • Can hold values of any type – Type is specified when a vector is defined vector<int> scores; vector<double> volumes; • Can use [] to access elements • scores[3] • volumes[0] ¡

  4. Defining ¡Vectors ¡ • Define a vector of integers (starts with 0 elements) vector<int> scores; • Define int vector with initial size 30 elements vector<int> scores(30); • Define 20-element int vector and initialize all elements to 0 vector<int> scores(20, 0); • Define int vector initialized to size and contents of vector finals vector<int> scores(finals);

  5. Growing a Vector ’ s Size ¡ • Use push_back member function to add an element to a full array or to an array that had no defined size // Add a new element holding a 75 scores.push_back(75); • Use size member function to determine number of elements currently in a vector howbig = scores.size();

  6. Removing Vector Elements ¡ • Use pop_back member function to remove last element from vector scores.pop_back(); • To remove all contents of vector, use clear member function scores.clear(); • To determine if vector is empty, use empty member function while (!scores.empty()) ...

  7. Vector ¡Func<ons ¡ • Useful ¡vector ¡func<ons: ¡ • capacity() : ¡returns ¡the ¡capacity ¡of ¡the ¡vector ¡ • reserve (n) : ¡reallocates ¡storage ¡to ¡increase ¡ ¡ the ¡capacity ¡to ¡be ¡n. ¡If ¡n ¡is ¡< ¡than ¡the ¡current ¡ capacity, ¡it ¡does ¡nothing. ¡ • push_back(val) : ¡adds ¡val ¡to ¡the ¡end ¡of ¡the ¡ vector ¡ • pop_back() : ¡returns ¡the ¡last ¡element ¡of ¡the ¡ vector ¡ More ¡on ¡page ¡567 ¡

  8. Expensive ¡Opera<ons ¡ • Add ¡an ¡element ¡into ¡the ¡middle ¡of ¡a ¡vector ¡ (not ¡at ¡the ¡end) ¡ • eg. ¡add ¡element ¡at ¡posi<on ¡I ¡ • All ¡elements ¡at ¡index ¡i ¡and ¡higher ¡are ¡moved ¡ down ¡by ¡one ¡posi<on ¡ 8 ¡ 8 ¡ 5 ¡ 2 ¡ Insert ¡2 ¡at ¡index ¡1 ¡ 7 ¡ 5 ¡ 9 ¡ 7 ¡ 9 ¡

  9. Expensive ¡Opera<ons ¡ • Remove ¡an ¡element ¡from ¡the ¡middle ¡of ¡ ¡ vector ¡(not ¡from ¡the ¡end) ¡ • All ¡elements ¡aTer ¡the ¡removed ¡element ¡ moved ¡up ¡by ¡one ¡ 8 ¡ 8 ¡ 2 ¡ 5 ¡ Remove ¡index ¡1 ¡ 5 ¡ 7 ¡ 7 ¡ 9 ¡ 9 ¡

  10. Arrays ¡or ¡Vectors ¡ • Vectors ¡are ¡great ¡if ¡you ¡need ¡the ¡flexibility ¡to ¡ dynamically ¡resize ¡an ¡array ¡ • Arrays ¡are ¡more ¡efficient ¡than ¡vectors. ¡ • Use ¡arrays ¡if ¡you ¡really ¡care ¡about ¡speed ¡ ¡ ¡ • Do ¡not ¡use ¡vectors ¡just ¡because ¡the ¡container ¡ with ¡built ¡in ¡func<ons ¡is ¡“easier” ¡to ¡code. ¡ ¡ ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend