Most of us are quite familiar with libraries in software. For example, in COMP1511, we've used <stdio.h> and <stdlib.h>.
Being an effective programmer often consists of the effective use of libraries. In some ways, this becomes more important than being a genius at writing code from scratch (Don't reinvent the wheel!).
While there are many libraries that can be used with C++, the Standard Template Library is the one we will focus on.
Container
Container
Algorithm
Container
Iterator
Iterator
Container
#include <array>
#include <iostream>
int main() {
// C-style. Don't do this
// int ages[3] = { 18, 19, 20 };
// for (int i = 0; i < 3; ++i) {
// std::cout << ages[i] << "\n";
// }
// C++ style. This can be used like any other C++ container.
// It has iterators, safe accesses, and it doesn't act like a pointer.
std::array<int, 3> ages{ 18, 19, 20 };
for (unsigned int i = 0; i < ages.size(); ++i) {
std::cout << ages[i] << "\n";
}
for (auto it = ages.begin(); it != ages.end(); ++it) {
std::cout << *it << "\n";
}
for (const auto& age : ages) {
std::cout << age << "\n";
}
}
demo201-vec-iter.cpp
Organises a finite set of objects into a strict linear arrangement.
Dynamically-sized array.
Fixed-sized array.
Double-ended queue.
Singly-linked list.
Doubly-linked list.
We will explore these in greater detail in Week 10.
It won't be necessary to use anything other than std::vector in COMP6771.
#include <iostream>
#include <vector>
// Begin with numbers 1, 2, 3 in the list already
int main() {
// In C++17 we can omit the int if the compiler can determine the type.
std::vector<int> numbers {1, 2, 3};
int input;
while (std::cin >> input) {
numbers.push_back(input);
}
std::cout << "1st element: " << numbers.at(0) << "\n"; // slower, safer
std::cout << "2nd element: " << numbers[1] << "\n"; // faster, less safe
std::cout << "Max size before realloc: " << numbers.capacity() << "\n";
for (int n : numbers) {
std::cout << n << "\n";
}
}
demo202-vec-obj.cpp
Provide fast retrieval of data based on keys. The keys are sorted. A value is accessed via a key.
A collection of unique keys.
A collection of keys.
Associative array that map a unique keys to values.
Associative array where one key may map to many values.
They are mostly interface-compatible with the unordered associative containers.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, double> m;
// The insert function takes in a key-value pair.
std::pair<std::string, double> p1{"bat", 14.75};
m.insert(p1);
// The compiler will automatically construct values as
// required when it knows the required type.
m.insert({"cat", 10.157});
// This is the preferred way of using a map
m.emplace("cat", 10.157);
// This is very dangerous, and one of the most common causes of mistakes in C++.
std::cout << m["bat"] << '\n';
auto it = m.find("bat"); // Iterator to bat if present, otherwise m.end()
// This is a great example of when to use auto, but we want to show you what type it is.
for (const std::pair<const std::string, double>& kv : m) {
std::cout << kv.first << ' ' << kv.second << '\n';
}
}
demo203-map.cpp
Provide fast retrieval of data based on keys. The keys are hashed.
A collection of unique keys.
Associative array that map unique keys to a values.
O(1)+ means amortised constant time