1

I'm attempting to find a value through a binary search, but i keep getting an error of error: no match for 'operator==' in '(& itemNumb)->std::vector<_Tp, _Alloc>::operator[], std::allocator > >(((std::vector >::size_type)middle)) == value'| I already have the vector sorted an am completely unsure of what caused the error.

    void Search(vector<string>& itemNumb, vector<string>& itemName, vector<double>& itemCost, vector<int>& itemQuant)
    {
        int left, right, value, middle;
        left = 0;
        right = itemNumb.size();
        cout << "Please enter desired item number." << endl;
        cin >> value;
        while (left <= right)
        {
            middle = ((left + right) / 2);
            if (itemNumb[middle] == value)
            {
                cout << "Item is " << itemName[middle] << endl;
                cout << "Price is " << itemCost[middle] << endl;
                cout << "Amount in stock is " << itemQuant[middle] << endl;
            }
            else if (itemNumb[middle] > value)
            {
                right = (middle - 1);
            }
            else
            {
                left = (middle + 1);
            }
        }
        if (intNumb[middle] != value)
        {
            cout << "Item number not found." << endl;
        }
    }

Each if statement is giving the same error, just with "operator==" or "operator>" in order. Any help at all would be wonderful, i'm at my wit's end trying to figure this out, pretty new to c++.

1 Answers1

2

The vector itemNumb is a vector of strings, while value is an integer. You cannot compare directly a string to an integer. You first have to convert the integer into a string.

In C++11, you could use std::to_string() and do:

if (itemNumb[middle] == std::to_string(value))
//                      ^^^^^^^^^^^^^^^^^^^^^

A possible alternative is to use std::ostringstream, as shown in this answer.

Community
  • 1
  • 1
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451