Vector Iterator Not Dereferencable

I'm getting the error as stated in the title at line 80 when I call my binarySearch function. Any ideas why/how to fix?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// BinarySearch.cpp
// Cedar Wiseman
// COSC 2030, Section 11, Spring 2014
// Project04
// March 13, 2014

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

double binarySearch( const vector<double> & v, double target )
{
   unsigned long low = 0;
   unsigned long high = v.size();

   while( low < high )
   {
      unsigned long mid = (low + high) / 2;

      if( target > *v.end() ) //used to check if lower bound is greater that last number in vector
        return -1;

      if( target < *v.begin() ) //used to check if upper bound is lower than first number in vector
        return -2;

      if( target >= v.at(mid) && target > v.at(mid-1) ) //make sure it's first instance of target
      {
        if( target <= v.at(mid+1) )
          return mid;
        else
          low += low;
      }
        
      if( target == v.at(mid) && target == v.at(mid-1) ) //find first instance of target
        high -= high;

      if( target < v.at(mid) ) //this is what makes it a binary search
        high = mid;

      else
        low = mid + 1;   
   }
}

int main()
{
	string filename, filename2;
  vector<double> vec;

	cout << "Enter filename of sorted doubles: " << endl;
	cin >> filename;
  ifstream infile ( filename );


	if (infile) // check if file is open 
  {        
    double value;

    // read the elements in the file into a vector  
    while ( infile >> value )
      vec.push_back( value );
  }
	else
    cout << filename << " failed to open" << endl;

  cout << "Enter filename of double pairs: " << endl;
  cin >> filename2;
  ifstream infile2 ( filename2 );

  if ( infile2 )
  {
    double a, b;
    while ( infile2 >> a >> b )
    {
      cout << "The range being searched is " << a << " to " << b << endl;

      if( binarySearch( vec, a ) == -1 )
        cout << "The specified file has no values in this range." << endl;
      else if( binarySearch( vec, b ) == -2 )
        cout << "The specified file has no values in this range." << endl;
      else
        cout << "There are " << binarySearch( vec, b ) - binarySearch( vec, a ) << " values in this range." << endl;
    }
  }
  else
    cout << filename2 << " failed to open" << endl;
		
return 0;
}
Okay, I realized it's because I was using *v.begin() and *v.end() instead of v.at(low) and v.at(high), but now when running the program, I get a "std::out_of_range at memory location 0x003EF580" error
Last edited on
A. In a vector, v, v.size() is never a valid index (line 17.)
B. binarySearch does not (always) return a value as it promises to do.
Last edited on
Topic archived. No new replies allowed.