O'Reilly logo

C++17 STL Cookbook by Jacek Galowicz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required