ex_intersect_sorted

ows how to find an intersection of two sorted vectors of dat

Categories:

  • core/algorithms/basic_algorithms.hh

Output files:

Program source:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <vector>
#include <iostream>
#include <iterator>

#include <core/algorithms/basic_algorithms.hh>

/** @brief Shows how to find an intersection of two sorted vectors of data
 *
 * CATEGORIES: core/algorithms/basic_algorithms.hh
 * KEYWORDS:   algorithms; data structures
 */
int main(const int argc, const char* argv[]) {

  std::vector<int> range1({1,2,3,5,6,7}), range2({5,6,7,8,9,10}), repeated;

  // Note that both <code>range1</code> and <code>range2</code> are already sorted!
  core::algorithms::intersect_sorted(range1.begin(), range1.end(), range2.begin(), range2.end(), repeated);

  // Print the element found as the intersection between the two ranges
  std::copy(repeated.begin(), repeated.end(), std::ostream_iterator<int>(std::cout, " "));
  std::cout << "\n";
}
../_images/file_icon.png