Problem p02268 (C++) — buggy submission, verdict: Runtime Error

#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <stack>
#include <string.h>
#include <vector>

// TODO テンプレート化したい
// std::vector<int>::iterator binarySearch(std::vector<int> sorted_array, int
// value)
// {
//     int left  = 0;
//     int right = sorted_array.size();

//     while (left < right - 1) {
//         int current = (left + right) / 2;
//         if(value == sorted_array.at(current)){
//             return sorted_array.begin() + current;
//         }else if(value < sorted_array.at(current)){
//             right = current;
//         }else{
//             left = current;
//         }
//     }

//     return sorted_array.end();
// }

int binarySearch(std::vector<int> sorted_array, int value) {
  int left = 0;
  int right = sorted_array.size();

  while (left < right) {
    int current = (left + right) / 2;
    if (value == sorted_array.at(current)) {
      return current;
    } else if (value < sorted_array.at(current)) {
      right = current;
    } else {
      left = current;
      if (current + 1 == right)
        break;
    }
  }

  return sorted_array.size();
}

std::vector<int> createVectorFronCin(int cnt) {
  std::vector<int> ret;
  ret.reserve(cnt);

  int cur = 0;
  for (int i = 0; i < cnt; ++i) {
    std::cin >> cur;
    ret.push_back(cur);
  }

  return ret;
}

int main() {
  int n = 0;
  int q = 0;

  std::cin >> n;
  std::vector<int> sorted_array = createVectorFronCin(n);

  std::cin >> q;
  std::vector<int> values = createVectorFronCin(q);

  int counter = 0;
  for (std::vector<int>::iterator it = values.begin(); it < values.end();
       ++it) {
    int pos = binarySearch(sorted_array, *it);
    if (pos < sorted_array.size())
      counter++;

    // std::vector<int>::iterator pos = binarySearch(sorted_array, *it);
    // if (pos != sorted_array.end())
    //     counter++;

    // if (binarySearch(sorted_array, *it) != sorted_array.end())
    //     counter++;
  }

  std::cout << counter << std::endl;
}


Error: -6
6f4cb61d-06b0-48d4-85ba-58e1d35c3dc5.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02268/C++/s459023231.cpp:68: int main(): Assertion `false' failed.


Fixed submission:

#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <stack>
#include <string.h>
#include <vector>

// TODO テンプレート化したい
// std::vector<int>::iterator binarySearch(std::vector<int> sorted_array, int
// value)
// {
//     int left  = 0;
//     int right = sorted_array.size();

//     while (left < right - 1) {
//         int current = (left + right) / 2;
//         if(value == sorted_array.at(current)){
//             return sorted_array.begin() + current;
//         }else if(value < sorted_array.at(current)){
//             right = current;
//         }else{
//             left = current;
//         }
//     }

//     return sorted_array.end();
// }

int binarySearch(std::vector<int> sorted_array, int value) {
  int left = 0;
  int right = sorted_array.size();

  while (left < right) {
    int current = (left + right) / 2;
    if (value == sorted_array.at(current)) {
      return current;
    } else if (value < sorted_array.at(current)) {
      right = current;
    } else {
      left = current;
      if (current + 1 == right)
        break;
    }
  }

  return sorted_array.size();
}

std::vector<int> createVectorFronCin(int cnt) {
  std::vector<int> ret;
  ret.reserve(cnt);

  int cur = 0;
  for (int i = 0; i < cnt; ++i) {
    std::cin >> cur;
    ret.push_back(cur);
  }

  return ret;
}

int main() {
  std::ifstream ifs("input/alds1_04_b_01.txt");
  if (!ifs)
    assert(false);
  std::cin.rdbuf(ifs.rdbuf());

  int n = 0;
  int q = 0;

  std::cin >> n;
  std::vector<int> sorted_array = createVectorFronCin(n);

  std::cin >> q;
  std::vector<int> values = createVectorFronCin(q);

  int counter = 0;
  for (std::vector<int>::iterator it = values.begin(); it < values.end();
       ++it) {
    int pos = binarySearch(sorted_array, *it);
    if (pos < sorted_array.size())
      counter++;

    // std::vector<int>::iterator pos = binarySearch(sorted_array, *it);
    // if (pos != sorted_array.end())
    //     counter++;

    // if (binarySearch(sorted_array, *it) != sorted_array.end())
    //     counter++;
  }

  std::cout << counter << std::endl;
}