How to do it...

In this section, we are going to use linear and binary search algorithms on a small example data set:

  1. We first include all the necessary headers and declare that we use the std namespace:
      #include <iostream>      #include <vector>      #include <list>      #include <algorithm>      #include <string>      using namespace std;
  1. Our data set will consist of city structs, which just save a city's name, and its population count:
      struct city {          string name;          unsigned population;      };
  1. Search algorithms need to be able to compare one item to the other, so we overload the == operator for the city struct instances:
      bool operator==(const city &a, const city &b) {          return a.name == b.name && a.population == b.population;      }
  1. We also want to print the

Get C++17 STL Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.