NOTE: this website is out of date. This is the course web site from a past quarter. If you are a current student taking the course, you should visit the current class web site instead. If the current website is not yet visible by going to cs111.stanford.edu, it may be accessible by visiting this link until the new page is mounted at this address. Please be advised that courses' policies change with each new quarter and instructor, and any information on this out-of-date page may not apply to you.
Based on a Handout by John Ousterhout, with modifications by Nick Troccoli
This page lists several classes from the C++ library that you may find useful at some point during CS 111, as well as information about C++ classes. There are two popular Web sites for learning more about C++ classes: cppreference.com and cplusplus.com. The cppreference.com site is more precise and formal; it tends to be preferred by purists and experts. However, many people find cplusplus.com to be simpler and easier to understand, especially for people still learning C++. We provide links to both sites below; take your pick.
We also have review videos on Canvas that review pointers/memory and C++ classes; check them out if you want a review under the "Panopto Course Videos" tab. The slides from those videos are available here.
C++ Classes
In addition to the review video, the following resources provide more information about defining C++ classes:
- Sean Szumlanski’s awesome CS106B lecture notes Cynthia Bailey + Julie Zelenski’s awesome CS106B slides
- cplusplus reference on classes
- cppreference information on classes
Pointers and Memory
In addition to the review video, the following resources provide more information about pointers and memory in C and C++:
- Sean Szumlanski's awesome CS106B lectures notes for C++ (pointers/arrays (which work much the same in C and C++) and heap)
- CS107 prior quarter course website for slides for C (strings, pointers, heap)
C++ Strings and Common C++ Data Structures
std::string
Provides a variety of mechanisms for manipulating strings of text.
std::vector
Expandable arrays of objects of any type (all the objects in a given instance must have the same type). Provides fast access to any element using its index.
Here is a short example program that appends entries to a vector and then prints them out:
vector<int> nums;
// Add elements 0-9, each time appending to the back.
for (int i = 0; i < 10; i++) {
nums.push_back(i);
}
// Regular for loop - prints 0-9
for (size_t i = 0; i < nums.size(); i++) {
cout << nums[i] << endl;
}
/* Same, but "range-based for loop" (another option for
* iterating over elements). We access each element as a
* const reference if we are not changing any elements,
* to avoid making copies.
*/
for (const int& num : nums) {
cout << num << endl;
}
Check out the reference for more information about vector.
std::map
A container that contains key-value pairs where there is an ordering among the keys. Supports both lookup by individual key and also range queries (find a group of objects in order of their keys). This class isn't quite as fast as std::unordered_map (more information further down on this page), but it supports ordering, which is not supported in std::unordered_map.
Check out the reference for more information about map:
Here is a short example program that adds entries to a map, checks if an entry is in the map, and then prints all entries out.
// first type is key, second type is value
map<int, int> nums_map;
// Add 0->1, 1->2, 2->3, 3->4, etc. (value = key + 1).
for (int i = 0; i < 10; i++) {
nums_map[i] = i + 1;
}
// This should print "12 is not in the map"
if (nums_map.find(12) != nums_map.end()) {
cout << "12 is in the map" << endl;
} else {
cout << "12 is not in the map" << endl;
}
/* Range-based for loop with quirky syntax - "Auto" means
* "auto-detect" the type; we generally discourage using auto
* because it obscures the type information, which is useful.
* However, in this case it's OK because the type is somewhat
* complex and not very helpful (we know that key will have
* the type of a key and value will have the type
* of a value). We access each pair with
* const if we are not changing any elements,
* to avoid making copies.
*/
for (const auto& [key, value] : nums_map) {
cout << key << ": " << value << endl;
}
However, a word of caution: if a key is not in the map, using [] will insert it! This is called auto-insertion, and is sometimes useful, but can also cause problems. It's good practice to check if something is in a map (e.g., using the find method) before you try to access it. Here is an example where auto-insertion causes a problem:
map<int, string> nums_to_str_map;
/* 2 is not in the map, but trying to access it with []
* will auto-add an entry for it with an empty string value,
* and then return that!
*/
string nonexistent_value = nums_to_str_map[2];
cout << nonexistent_value << endl; // Will be empty string
cout << nums_to_str_map.size() << endl; // Will print 1!
std::unordered_map
A container that contains key-value pairs with unique keys. Both keys and values can have arbitrary type. Provides fast lookup based on keys. The container is unsorted (there is no ordering among the elements). Works similarly to an std::map.
std::queue
FIFO container for objects of any type. You add elements to one end and remove from the other. Most people find std::deque easier to use and more powerful.
std::deque
A double-ended queue of objects of arbitrary type. Can insert and remove from both ends.
Here is a short example program that appends entries to a vector and then prints them out:
deque<int> nums;
// Add elements 0-4 to the back
for (int i = 0; i < 5; i++) {
nums.push_back(i);
}
// Add elements 5-9 to the front
for (int i = 5; i < 10; i++) {
nums.push_front(i);
}
/* Get and remove the front - note that
* `pop_front` just removes the element,
* it does not return it To get the
* the element itself, we must call
* `front` separately.
*/
int front = nums.front();
nums.pop_front();
// Prints 9
cout << "front elem = " << front << endl;
Check out the reference for more information about deque.
C++ Multithreading Types
std::thread
Create system threads.
std::mutex
Standard locks; normally used in conjunction with std::unique_lock and std::condition_variable_any.
std::unique_lock
Acquires an std::mutex when constructed, automatically unlocks the mutex when the std::unique_lock is destroyed. This class is very useful to ensure that mutexes are always unlocked (e.g. when a method returns).
std::condition_variable_any
Used in conjunction with std::mutex or std::unique_lock to put threads to sleep and wake them up again.
Other C++ Types
std::function
A variable type that represents a function; you can use it to store functions in variables, pass functions as parameters, etc. and call the function later.