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

cs 162 intro to programming ii
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS ¡162 ¡ Intro ¡to ¡Programming ¡II ¡

Vectors ¡

1 ¡

slide-2
SLIDE 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>

slide-3
SLIDE 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] ¡
slide-4
SLIDE 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
  • f vector finals

vector<int> scores(finals);

slide-5
SLIDE 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();

slide-6
SLIDE 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()) ...

slide-7
SLIDE 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 ¡

slide-8
SLIDE 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 ¡ 5 ¡ 7 ¡ 9 ¡ Insert ¡2 ¡at ¡index ¡1 ¡ 8 ¡ 2 ¡ 5 ¡ 7 ¡ 9 ¡

slide-9
SLIDE 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 ¡ 5 ¡ 7 ¡ 9 ¡ Remove ¡index ¡1 ¡ 8 ¡ 2 ¡ 5 ¡ 7 ¡ 9 ¡

slide-10
SLIDE 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. ¡ ¡ ¡