boost and c++11stanford.edu/.../lectures/lecture08/08-boostc++11.pdf · boost cs106b assignment...

51
Boost and C++11 Cristian Cibils ([email protected])

Upload: others

Post on 27-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost and C++11

Cristian Cibils ([email protected])

Page 2: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Administrivia

Assignment two is out!● We're going to write a hangman program● Due: Tuesday May 12th, 11:59 PM● I recommend doing it

○ You must do it if you did not do GraphViz

● There's no starter code whatsoever, so all you have to download is the dictionary file

Page 3: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Administrivia

Apply to section lead!

Page 4: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Assignment One Feedback

Overall, submissions were very good.

Let's go over some of the common problems though

Page 5: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Assignment One Feedback

There were four main lessons in this assignment● File I/O● Basic usage of data structures to transform

values● Reading basic user input● Looping for a specified amount of time

Page 6: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Assignment One Feedback

Biggest issue: File I/OMany used getline and a stringstream to read

integers from the file

Page 7: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Assignment One Feedback

Let's look at some correct but overly verbose file reading code.

Page 8: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Assignment One Feedback

Biggest issue: File I/OMany used getline and a stringstream to read integers from the file

Input file stream

fstream

Temporary value for getline

string

Converter

stringstream

Temporary value from converter

int

Place temporary into edge

Edge

Page 9: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Assignment One Feedback

Biggest issue: File I/OThe following code is the easiest way to read in a graph:

size_t numNodes;

input >> numNodes;

Edge e;

while (input >> e.start >> e.end)

graph.edges.push_back(e);

Page 10: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Assignment One Feedback

● Mixing getline and >> will result in bad things!○ Extraneous data can be left sitting on the stream○ In general, you should only use one or the other○ See slides on streams for more details

Page 11: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Assignment One Feedback

● Updating nodes as you iterate through them○ If you apply attractive or repulsive forces before

calculating all forces, then nodes will have different positions

○ This will affect the computation of later forces!

Page 12: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Changing C++

Now let’s take a quick look at the growth of C++ as a language over the years

Page 13: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Changing C++

C

Page 14: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Changing C++

C

More features!

1979: 1 user

Page 15: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Changing C++

C++

More features!

1979: 1 user

Page 16: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Changing C++

C++

1979: 1 user

Page 17: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Changing C++

C++

More features!

1985: 500 users

Page 18: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Changing C++

C++

More features!

1985: 500 users

Page 19: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Changing C++

C++

1985: 500 users

Page 20: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Changing C++

C++

More features!

2015: 3.3M users

Page 21: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Changing C++

C++

More features!

2015: 3.3M users

Page 22: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Changing C++

2015: 3.3M users

Page 23: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Solution: Extensible Language

ExtensibilityLet C++ users add features to the language

without changing the definition of the language

Page 24: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

● Huge (19 million lines of code) collection of third party C++ code

● High quality● Many libraries incorporated into C++11

Page 25: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

How much C++ you need to know to understand...

Pretty much none ALL OF IT

Page 26: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

How much C++ you need to know to fully understand...

Hello World

Pretty much none ALL OF IT

Page 27: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

How much C++ you need to know to fully understand...

Hello World

Pretty much none ALL OF IT

CS106B assignment

Page 28: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

CS106B assignment

CS106L assignment

How much C++ you need to know to fully understand...

Hello World

Pretty much none ALL OF IT

Page 29: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

CS106B assignment

How much C++ you need to know to fully understand...

Hello World

Pretty much none ALL OF IT

CS106L reference solutions

CS106L assignment

Page 30: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

CS106B assignment

CS106L assignment

How much C++ you need to know to fully understand...

Hello World

Pretty much none ALL OF IT

CS106B Libraries

CS106L reference solutions

Page 31: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

CS106B assignment

CS106L assignment

How much C++ you need to know to fully understand...

Hello World

Pretty much none ALL OF IT

CS106B Libraries

CS106L reference solutions

Huge C++ Projects

Page 32: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

CS106B assignment

CS106L assignment

How much C++ you need to know to fully understand...

Hello World

Pretty much none ALL OF IT

CS106B Libraries

CS106L reference solutions

A C++ compilerHuge C++

Projects

Page 33: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

CS106B assignment

CS106L assignment

How much C++ you need to know to fully understand...

Hello World

Pretty much none ALL OF IT

CS106B Libraries

CS106L reference solutions

A C++ compiler

Boost

Huge C++ Projects

Page 34: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

Boost uses the extensibility of C++ to add features to the language without requiring a

new version of the language itself

Page 35: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

● We often talk about converting between integers and strings

● What if there was an easier way?● Like, much easier

Page 36: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost

See code in LexicalCast.pro

Page 37: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Boost and C++11

● Boost uses the extensibility of C++ to add features to the language without requiring a new version of the language itself

● Sometimes, Boost’s changes will be incorporated into C++ itself

● Let’s take a look at some examples.

Page 38: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

C++11

● Let's take a look at how foreach came to be included in the language

● BOOST_FOREACH first introduced in 2004● Stanford’s foreach loop does similar stuff● This was standardized to the “range-based

for” loop we know and love in C++11set<int> s;

for (int x : s)

cout << x << endl;

Page 39: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

C++11

See code in ForEach.pro

Page 40: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Auto

● Now let’s look at a C++11 feature, auto● The new keyword auto allows a programmer

to simplify the declaration of a variable● Variables declared auto have their type

inferred from the right of the = sign

auto x = 0;

auto y = 0.0;

Page 41: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Auto

See code in Auto.pro

Page 42: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Lambdas

● Let’s look at another cool feature: lambdas● Lambdas allow us to define a function just

like we define a variable● It’s tricky to explain why this is useful, but let’

s take a look at a few examples to understand why

Page 43: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Lambdas

Let’s take a look at a simple use of lambdas(BasicLambda.pro)

Page 44: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Lambdas

Lambda Syntax:

[captured variables](paramaters) {

//function stuff

};

Page 45: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Lambdas

Lambda Syntax:

int x;

[x](paramaters) {

//function stuff

};

Captures x by value

Page 46: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Lambdas

Lambda Syntax:

int x;

[&x](paramaters) {

//function stuff

};

Captures x by reference

Page 47: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Lambdas

Lambda Syntax:

[&=](paramaters) {

//function stuff

};

Captures all variables by reference

Page 48: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Comparator Function

● Many algorithms and containers have a concept of order

● Ascending order → n1 < n2 < …< n100● A Comparator Function in C++ is a function

that determines whether one element is less than another

● Equivalent to implementing the ‘<’ operator

Page 49: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Comparator Function

bool compareInt(int x, int y) {

return x < y;

}

bool reverseCompareInt(int x, int y) {

return y < x;

}

Page 50: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

Lambdas

Let’s take a look at some more advanced uses of lambdas

(BasicLambda.pro)

Page 51: Boost and C++11stanford.edu/.../lectures/lecture08/08-BoostC++11.pdf · Boost CS106B assignment CS106L assignment How much C++ you need to know to fully understand... Hello World

C++11

● C++11 added much more than just range based for, type inference, and lambdas○ Whole new set of algorithms including all_of,

is_sorted, and many more○ Better random number library○ Better functions to measure time○ Multithreading○ much, much more