Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing...

57
Collections, Part Two

Transcript of Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing...

Page 1: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Collections, Part Two

Page 2: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Outline for Today

● The Vector type● Storing sequences.

● Reference Parameters● A key part of C++ programming.

● Recursion on Vectors● A problem with cell towers.

Page 3: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Vector

Page 4: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Vector

● The Vector is a collection class representing a list of things.● Similar to Java's ArrayList type.

● Syntax is pretty friendly:● Read/write an element: vec[index]● Append elements: vec += a, b, c, d;● Remove elements: vec.remove(index);● Check the size: vec.size()● Loop over elements: for (T elem: vec) { … }

Page 5: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Reference Parameters in C++

Page 6: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Pass-by-Value

● In C++, objects are passed into functions by value. The function gets its own local copy of the argument to work with.● There’s a cool nuance where this isn’t 100%

true; come talk to me after class if you’re curious!

● You can see this by running some code samples with our new Vector type.

Page 7: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

Page 8: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

Page 9: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

void growUp(Vector<string> characters) { characters += "Paula"; characters[0] = "Chiron";}

void growUp(Vector<string> characters) { characters += "Paula"; characters[0] = "Chiron";}

"Little" "Teresa" "Kevin"characters

Page 10: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

void growUp(Vector<string> characters) { characters += "Paula"; characters[0] = "Chiron";}

void growUp(Vector<string> characters) { characters += "Paula"; characters[0] = "Chiron";}

"Little" "Teresa" "Kevin"characters

Page 11: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

void growUp(Vector<string> characters) { characters += "Paula"; characters[0] = "Chiron";}

void growUp(Vector<string> characters) { characters += "Paula"; characters[0] = "Chiron";}

"Little" "Teresa" "Kevin"characters "Paula"

Page 12: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

void growUp(Vector<string> characters) { characters += "Paula"; characters[0] = "Chiron";}

void growUp(Vector<string> characters) { characters += "Paula"; characters[0] = "Chiron";}

"Little" "Teresa" "Kevin"characters "Paula"

Page 13: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

void growUp(Vector<string> characters) { characters += "Paula"; characters[0] = "Chiron";}

void growUp(Vector<string> characters) { characters += "Paula"; characters[0] = "Chiron";}

"Chiron" "Teresa" "Kevin"characters "Paula"

Page 14: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

void growUp(Vector<string> characters) { characters += "Paula"; characters[0] = "Chiron";}

void growUp(Vector<string> characters) { characters += "Paula"; characters[0] = "Chiron";}

"Chiron" "Teresa" "Kevin"characters "Paula"

Page 15: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

Page 16: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

Page 17: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Pass-by-Reference

● In C++, there’s the option to pass parameters into function by reference.

● This doesn’t send a copy of the argument – it really sends the actual, honest-to-goodness argument into the function.

● To declare a function that takes an argument by reference, put an ampersand after the name of the type of the argument.

● Calling a function that takes a reference looks identical to one that takes a value. You just have to know what the function expects.

Page 18: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

Page 19: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

Page 20: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

void growUp(Vector<string>& characters) { characters += "Paula"; characters[0] = "Chiron";}

void growUp(Vector<string>& characters) { characters += "Paula"; characters[0] = "Chiron";}

Page 21: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

void growUp(Vector<string>& characters) { characters += "Paula"; characters[0] = "Chiron";}

void growUp(Vector<string>& characters) { characters += "Paula"; characters[0] = "Chiron";}

Page 22: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

void growUp(Vector<string>& characters) { characters += "Paula"; characters[0] = "Chiron";}

void growUp(Vector<string>& characters) { characters += "Paula"; characters[0] = "Chiron";}

"Paula"

Page 23: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Little" "Teresa" "Kevin"moonlight

void growUp(Vector<string>& characters) { characters += "Paula"; characters[0] = "Chiron";}

void growUp(Vector<string>& characters) { characters += "Paula"; characters[0] = "Chiron";}

"Paula"

Page 24: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Chiron" "Teresa" "Kevin"moonlight

void growUp(Vector<string>& characters) { characters += "Paula"; characters[0] = "Chiron";}

void growUp(Vector<string>& characters) { characters += "Paula"; characters[0] = "Chiron";}

"Paula"

Page 25: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

int main() { Vector<string> moonlight; moonlight += "Little", "Teresa", "Kevin";

growUp(moonlight);

/* … */}

"Chiron" "Teresa" "Kevin"moonlight "Paula"

Page 26: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Pass-by-const-Reference

● Passing a large object (Vector, Stack, Queue, string, etc.) into a function by value can take a lot of time.

● Taking parameters by reference avoids making a copy, but risks that the object gets tampered with in the process.

● As a result, it’s common to have functions that take objects as parameters take their argument by const reference:● The “by reference” part avoids a copy.● The “const” (constant) part means that the function can’t

change that argument.

Page 27: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

What Should You Use?

What kind ofargument?

Actually wanta copy?

Need to changethe argument?

Pass by value!Pass byreference!

Pass by constreference!

Yes!

Nope! Object!

Primitive type!

Totes!

No way!

Start!

As always, please feel free to ask questions on Piazza, in office hours, or

at the LaIR!

As always, please feel free to ask questions on Piazza, in office hours, or

at the LaIR!

Page 28: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Recursion on Vectors

Page 29: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Example: Cell Tower Purchasing

Page 30: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Buying Cell Towers

137 42 95 272 52

Page 31: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Buying Cell Towers

137 42 95 272 52

Page 32: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Buying Cell Towers

14 22 13 25 30 11 9

Page 33: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Buying Cell Towers

14 22 13 25 30 11 9

Page 34: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Buying Cell Towers

99 100 99

Page 35: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Buying Cell Towers

99 100 99

Page 36: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

How can we solve this problem?

Page 37: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13 25 30 11 9

Page 38: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13 25 30 11 9

Page 39: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13 25 30 11 9

Page 40: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13 25 30 11 9

Maximize what's left in here.

Page 41: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13 25 30 11 9

14 22 13 25 30 11 9

Maximize what's left in here.

Page 42: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13 25 30 11 9

14 22 13 25 30 11 9

Maximize what's left in here.

Page 43: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13 25 30 11 9

14 22 13 25 30 11 9

Maximize what's left in here.

Maximize what's left in here.

Page 44: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

The Insight

● If there’s no cities, the best you can do is cover zero people.

● If there’s one city, the best you can do is build a tower there and cover everyone in it.

● Otherwise, you either● do build in the first city, skip the second city, then

do the best you can with the remaining cities; or● don’t build in the first city, and then do the best you

can with the remaining cities.

So we just try out both options and see which one is better!

Page 45: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13

How the Recursion Works

Page 46: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13

22 13

How the Recursion Works

Page 47: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13

22 13

13

How the Recursion Works

Page 48: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13

22 13

13

How the Recursion Works

Best is13

Page 49: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13

22 13

13

How the Recursion Works

+22

Best is13

Page 50: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13

22 13

13

How the Recursion Works

+22

Best is13

Best is0

Page 51: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13

22 13

13

How the Recursion Works

+22

Best is13

Best is0

Best is22

Page 52: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13

22 13

13

13

How the Recursion Works

+14

+22

Best is13

Best is0

Best is22

Page 53: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13

22 13

13

13

How the Recursion Works

+14

+22

Best is13

Best is0

Best is22

Best is13

Page 54: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

14 22 13

22 13

13

13

How the Recursion Works

+14

+22

Best is13

Best is0

Best is22

Best is13

Best is27

Page 55: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

How the Recursion Works

20 25 23 17

25 23 17

23 17

17

17

23 17

17

Best:17

Best:0

Best:0

Best:17

Best:17

+25 +23

+20

Best:23

Best:32

Best:23

Best:43

+23

Page 56: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Your Action Items

● Keep reading Chapter 5 on the different container types.

● Finish up Assignment 1! It’s due on Monday and you’ll probably not want to use your late days here.

Page 57: Collections, Part Two€¦ · Collections, Part Two. Outline for Today The Vector type Storing sequences. Reference Parameters A key part of C++ programming.

Next Time

● Map● A collection for storing associations between

elements.● Set

● A collection for storing an unordered group of elements.

● Lexicon● A special kind of Set.