Tie koodariksi

C++ Programming

Chapter 13: The Standard Library

Overview

The C++ standard library contains classes and functions that can be used to assist in programming. The standard library consists of parts, each of which has a specific header file.

The header file is included in the code via the #include directive. We do this at the beginning of the code, with the following line, which includes the <iostream> header file:

#include <iostream>
For example, in this file is defined streams cin and cout, which we have been using to read and print information.

The C++ standard library has both header files inherited from the C language, which begin with c (such as <cmath> and <ctime>), and C++'s own header files (such as <random> and <vector>). This and the next two chapters cover some parts of the standard library.

A good way to get acquainted with the contents of the standard library is to take advantage of online references. Useful sources include: cplusplus.com and cppreference.com.

Namespace std

All parts of the C++ standard library are in the namespace std, which is why we used the following command at the beginning of the code, which allows them to be used directly:
using namespace std;
Then, for example, we can print text like this:
cout << "Hello!\n";
If there is no using line, we need to type std:: at the beginning of each name:
std::cout << "Hello!\n";
This makes it (slightly) more effort to use the standard library, but, on the other hand, this way the standard library names don't interfere with any of the names we define.

Mathematics

#include <cmath>
Examples of mathematical functions include: The parameters and return values for these functions are floating points. The following code describes how to use the functions:
cout << pow(3,4) << "\n"; // 81
cout << sqrt(5) << "\n"; // 2.23607
cout << sin(1) << "\n"; // 0.841471
cout << cos(1) << "\n"; // 0.540302
cout << tan(1) << "\n"; // 1.55741
cout << exp(3) << "\n"; // 20.0855
cout << log(7) << "\n"; // 1.94591
cout << floor(3.1) << "\n"; // 3
cout << ceil(3.1) << "\n"; // 4

Random numbers

#include <random>
The following code creates a random number generator g and the d distribution, which gives random integers between 1 and 6. The generator is the Mersenne Twister (mt19937), which is a method to generate pseudo-random numbers. The code then simulates 10 dice throws.
mt19937 g(random_device{}());
uniform_int_distribution<int> d(1,6);
for (int i = 1; i <= 10; i++) {
    cout << d(g) << " ";
}
cout << "\n";
The code prints the following:
3 1 3 2 6 4 6 3 5 4

The seed value of a random number generator sets its initial state. In the code above, the seed number is a value from the system's random source (random_device), so that the code always produces different random numbers. However, if an experiment needs to be repeatable, the seed number can be given a constant, such as 1337:

mt19937 g(1337);
In this case, the generator will always generate the same sequence of numbers:
2 4 1 2 2 4 3 1 2 2

Time measurement

#include <chrono>
The following program calculates the number of prime numbers in the range 2...106 and displays the elapsed time for the calculation.
auto start = chrono::system_clock::now();
int result = 0;
for (int i = 2; i <= 1000000; i++) {
    bool ok = true;
    for (int j = 2; j*j <= i; j++) {
        if (i%j == 0) {ok = false; break;}
    }
    if (ok) result++;
}
auto end = chrono::system_clock::now();
chrono::duration<double> elapsed = end-start;
cout << "number of prime numbers: " << result << "\n";
cout << "elapsed time: " << elapsed.count() << " s\n";
Program output should look something like this:
number of prime numbers: 78498
elapsed time: 0.358783 s

Regular expressions

#include <regex>
A regular expression is a string that describes the format of a desired string. For example, the expression [a-z]{5,8} matches strings of 5-8 characters in length and made up of characters from a to z.

The regex_match function tests if a regular expression matches a specified string. The following code shows how to use the function:

regex r("[a-z]{5,8}");
cout << regex_match("monkey",r) << "\n"; // 1
cout << regex_match("banana",r) << "\n"; // 1
cout << regex_match("harpischord",r) << "\n"; // 0
cout << regex_match("lol",r) << "\n"; // 0
cout << regex_match("12345",r) << "\n"; // 0

Datatähti 2027 – peruskoulun ja lukion ohjelmointikilpailu käynnissä 26.10.–8.11.2026