C++ 如何将字符串从文本文件读入要搜索的向量?

C++ 如何将字符串从文本文件读入要搜索的向量?,c++,vector,iostream,binary-search,C++,Vector,Iostream,Binary Search,这些文件目前正在测试中, 但基本上我一行一行地读文件,一行一行地打印出来作为证明。。。 将字符串放入向量 调用我的BS函数以搜索包含其他文本文件的字符串 “我的文件”的内容并不重要,只是字符串都在1列中 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 cal

这些文件目前正在测试中, 但基本上我一行一行地读文件,一行一行地打印出来作为证明。。。 将字符串放入向量 调用我的BS函数以搜索包含其他文本文件的字符串

“我的文件”的内容并不重要,只是字符串都在1列中

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.
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int二进制搜索(向量列表、字符串str)
{
int first、mid、last;
布尔发现;
第一个=0;
last=list.size()-1;
发现=错误;
而(!found&&firststr)
last=mid-1;
其他的
第一个=中间+1;
}
如果(找到)
中途返回;
其他的
返回-1;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#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;
}
intmain(){
向量表;
字符串str;
河流充填;
open(“testdata.txt”);
如果(!infle)

cout您可能不想做所有这些,cpp标准库为您准备:

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;
}
}

这两行就足以对向量进行二进制搜索。

您可能不想做所有这些,cpp标准库为您准备:

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;
}
}

这两行就足以对向量进行二进制搜索。

您可能不想做所有这些,cpp标准库为您准备:

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;
}
}

这两行就足以对向量进行二进制搜索。

您可能不想做所有这些,cpp标准库为您准备:

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;
}
}

这两行足够在vector上进行二进制搜索。

这:
while(!infle.eof())
可能是错误的。修复这一问题,然后进行令人惊讶的关闭。try while(std::getline(infle,wordLine))您将希望将循环内的binarySearch调用也移动到searchLine中。另外,调用您的vector“list”很不幸,因为名称列表用于标准库中的双链接列表数据结构。将其称为“行”或类似名称会更容易混淆。我在您的代码中没有看到排序。单词是否已预排序?如果没有,您将希望在使用二进制搜索之前对其进行排序。此:
while(!infle.eof())
将是错误的。请修复此问题,并修复令人惊讶的关闭。请尝试(std::getline(infle,wordLine))同时将循环读取中的binarySearch调用移动到searchLine中。另外,调用向量“list”很不幸,因为名称列表用于标准库中的双链接列表数据结构。将其称为“行”或类似名称会更容易混淆。我在您的代码中没有看到排序。单词是否已预排序?如果没有,您将希望在使用二进制搜索之前对其进行排序。此:
while(!infle.eof())
将是错误的。请修复此问题,并修复令人惊讶的关闭。请尝试(std::getline(infle,wordLine))同时将循环读取中的binarySearch调用移动到searchLine中。另外,调用向量“list”很不幸,因为名称列表用于标准库中的双链接列表数据结构。将其称为“行”或类似名称会更容易混淆。我在您的代码中没有看到排序。单词是否已预排序?如果没有,您将希望在使用二进制搜索之前对其进行排序。此:
while(!infle.eof())
将是错误的。请修复此问题,并修复令人惊讶的关闭。请尝试(std::getline(infle,wordLine))同时将循环读取中的binarySearch调用移动到searchLine中。另外,调用向量“list”很不幸,因为名称列表用于标准库中的双链接列表数据结构。将其称为“行”或类似名称会更容易混淆。我在您的代码中没有看到排序。单词是否已预排序?如果没有,则在使用二进制搜索之前要对其进行排序。