1 of 16

CS 368 (C++): Lecture 8

Operator Overloading, const and Templates

Nov 9th, 2016

2 of 16

Lecture 9: Topics

  1. Overloading << operator�
  2. const correctness�
  3. Storing data in 2 dimensions�
  4. Overloading [ ] operator�
  5. Templates

3 of 16

How can we store data as a grid?

a00

a01

a02

a10

a11

a12

4 of 16

A vector of vectors!

std::vector<std::vector<int>> grid;

5 of 16

A vector of vectors!

std::vector<std::vector<int>> grid;

g00

g01

g02

g10

g11

g12

6 of 16

How to initialize the vector of vectors?

std::vector<std::vector<int>> grid;

7 of 16

How to initialize the vector of vectors?

std::vector<std::vector<int>> grid;

grid.resize(2);

8 of 16

How to initialize the vector of vectors?

std::vector<std::vector<int>> grid;

grid.resize(2);

grid[0]

grid[1]

9 of 16

How to initialize the vector of vectors?

std::vector<std::vector<int>> grid;

grid[0].resize(3);

grid[0]

grid[1]

10 of 16

How to initialize the vector of vectors?

std::vector<std::vector<int>> grid;

grid[0].resize(3);

0

grid[0]

grid[1]

0

0

11 of 16

How to initialize the vector of vectors?

std::vector<std::vector<int>> grid;

grid[1].resize(3);

0

grid[0]

grid[1]

0

0

12 of 16

How to initialize the vector of vectors?

std::vector<std::vector<int>> grid;

grid[1].resize(3);

0

0

grid[0]

grid[1]

0

0

0

0

13 of 16

How to access elements in a vector of vectors?

grid[0][0] = 3;

0

0

grid[0]

grid[1]

0

0

0

0

14 of 16

How to access elements in a vector of vectors?

grid[0][0] = 3;

0

0

grid[0]

grid[1]

0

0

0

0

15 of 16

How to access elements in a vector of vectors?

grid[0][0] = 3;

0

0

grid[0]

grid[1]

0

0

0

0

16 of 16

How to access elements in a vector of vectors?

grid[0][0] = 3;

3

0

grid[0]

grid[1]

0

0

0

0