0
I can read a file line my line and I know the fundamentals of Binary Search. how can i make my strings get into a vector or an array where i can call my Binsearch function and return true or false?

I have most of it.

The files are test for now, but basically I read a file line by line, print line by line for proof... place strings into vector call my BS function to search for a string with the other text file i have...

the contents of my files are not important, other than the strings are all in 1 column..

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

using namespace std;

int binarySearch(vector<string> list, string str)
{
    int first, mid, last;
    bool found;
    first = 0;
    last = list.size()-1;
    found = false;

    while(!found&&first<=last)
    {
        mid=(first+last)/2;
        if(list[mid]==str)
            found = true;

        else if(list[mid]>str)
            last=mid-1;
        else
            first=mid+1;
    }
    if(found)
        return mid;
    else
        return -1;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main() {

    vector<string> list;
    string str;

    ifstream infile;
    infile.open("testdata.txt");
    if (!infile)
        cout<<"Input file cannot be openned!"<<endl;
    cout<<"---------------------------------"<<endl;
    cout<<"List of words inserted into Trie:"<<endl;
    cout<<"---------------------------------"<<endl;
    cout<<endl;




    string wordLine;
    while (!infile.eof()){
        infile >> wordLine;

        list.push_back(wordLine); //this is my attempt to put strings into vector

        cout<<wordLine<<endl;
    }



    ifstream searchFile;
    searchFile.open("searchdata.txt");
    if(!searchFile)
        cout<<"Search file cannot be openned!"<<endl;



    string searchLine;
    int loc =binarySearch(list, str);

    while (!searchFile.eof()){
        searchFile >> searchLine;
        if (loc == -1)
            cout << searchLine <<" => FOUND!" << endl;
        else 
            cout << searchLine <<" => NOT FOUND." <<endl;


        cout << searchLine << endl;


    return 0;
}
}
user3038378
  • 3
  • 1
  • 2
  • 2
    This: `while (!infile.eof())` would be wrong. [You can start there.](http://stackoverflow.com/questions/5837639/eof-bad-practice/5837668#5837668) Fix that and your surprisingly close. – WhozCraig Nov 27 '13 at 00:18
  • try while(std::getline(infile, wordLine)) – Iuri Covalisin Nov 27 '13 at 00:20
  • You'll want to move the binarySearch call inside the loop reading into searchLine too. Separately, calling your vector "list" is unfortunate, because the name list is used for the double-linked list data structure in the Standard library. It would be less confusing to call it "lines" or similar. – Tony Delroy Nov 27 '13 at 00:24
  • I don't see a sort in your code. Are the words presorted? If not you'll want to sort them before using a binary search. – Retired Ninja Nov 27 '13 at 01:28

1 Answers1

3

You may not want to do all these, the cpp standard library is there for you:

std::sort(list.begin(),list.end());
binary_search (list.begin(), list.end(), str);

These two line is enough to do a binary search on vector.

deeiip
  • 3,319
  • 2
  • 22
  • 33