binary_search for strings won't work

Anyone have any idea why this won't work? It won't seem to find my string. When I check if they're inside the vector they definitely are there. I've tried with different data types, too. It always comes up as "Not found" even when I know I typed in a number inside the vector.

Thank you!


#include <iostream>
#include <vector>
#include <string>
#include <climits>
#include <algorithm>
using namespace std;

int main(void)
{
string number;

const string VAL[] = { "5658845", "4520125", "7895122", "8777541",
"8451277", "1302850", "8080152", "4562555", "5552012",
"5050552", "7825877", "1250255", "1005231", "6545231",
"3852085", "7576651", "7881200", "4581002" };

const vector<string> VALUES ( VAL, VAL + sizeof(VAL) / sizeof(string) );

cout << "Please enter a number to compare with the vector of the winning ";
cout << "numbers of happiness." << endl;

cin >> number;

if (binary_search(VALUES.begin(), VALUES.end(), number))
cout << "Found" << endl;
else
cout << "Not found" << endl;

return EXIT_SUCCESS;
}
Last edited on
Test if value exists in sorted sequence

You search from unsorted ...
Topic archived. No new replies allowed.