![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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/13.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/15.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/16.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/17.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/18.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/19.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/20.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/21.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/22.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/23.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/24.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/25.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/26.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/27.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/28.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/29.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/30.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/31.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/32.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/33.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/34.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/35.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/36.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/37.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/38.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/39.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/40.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/41.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/42.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/43.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/44.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/45.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/46.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/47.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/48.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/49.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/50.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070906/5f7867119b1d48383008653d/html5/thumbnails/51.jpg)
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