【问题标题】:Simple binary search doesn't work for me. I am trying to search for a word from text file简单的二进制搜索对我不起作用。我正在尝试从文本文件中搜索单词
【发布时间】:2020-02-16 18:40:57
【问题描述】:

我用 C++ 构建了一个程序,它从 txt 文件中获取单词并输入到程序中。然后程序将这些单词存储到数组中。现在,我想使用二进制搜索在数组中搜索特定的单词。

我的txt文件有以下几个字:

hello
world
hi
how
are
you
i
am
fine
thank
welcome
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;

int binarySearch(string words[], const string& x,int n)
{
    int l = 0 ;
    int r = n - 1;
    while (l <= r)
    {
        int m = l + (r - l) / 2;

        int res;
        if (x == (words[m]))
            res = 0;

        // Check if x is present at mid
        if (res == 0)
            return m;

        // If x greater, ignore left half
        if (x > (words[m]))
            l = m + 1;

            // If x is smaller, ignore right half
        else
            r = m - 1;
    }

    return -1;
}

int main () {
    ifstream inFile;
    inFile.open("test.txt");

    if(inFile.fail()){
        cerr << "Error opening file"<< endl ;

        exit(1);
    }

    string x1;
    string words[100];
    int count=0,i=0;
    string str;

    while( !inFile.eof()) {
        inFile >> x1;
        words[i]=x1;
        count++;
        i++;
    }

    for (i=0;i<100;i++){
        cout<< words[i]<<endl;
    }

    string x;
    x = "how";
    int n = 14;
    int result = binarySearch(words , x,n);
    if (result == -1)
        cout << ("\nElement not present");
    else
        cout << ("Element found at index ") << result;

    return 0;
}

我找不到除了第一个单词 Hello 之外的单词。所以请帮助我。

【问题讨论】:

  • 当你使用你的调试器运行你的程序时,一次一行,观察你的二分搜索函数是如何执行的,一行一行地检查所有变量的值,当它们改变时,和跟踪你的程序逻辑的执行流程,你做了什么观察?你知道如何使用调试器吗?如果没有,这是您学习如何使用它的绝佳机会,因此您可以找到并修复自己的错误,而无需寻求其他人的帮助。能够使用调试器是每个 C++ 开发人员必备的技能。没有例外。这正是调试器的用途。
  • 你有int res; 然后if (x == (words[m])) res = 0; 然后if (res == 0) return m; 你的res 变量被使用但当x 不等于words[m] 时没有初始化。所以你的程序有未定义的行为。
  • 二分搜索算法期望其输入被排序,但words 不是。
  • @drescherjm 是的,伙计!你是对的 。因此,它的行为异常。你让我开心!!!
  • 那么,我应该先对单词进行排序,然后将它们保存在另一个数组中,然后再搜索吗?

标签: c++ search binary-search clion linear-search


【解决方案1】:

希望这项工作

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;

int binarySearch(string words[], const string& x, int n)
{
    int l = 0;
    int r = n - 1;
    while (l <= r)
{
    int m = l + (r - l) / 2;

    int res = 0;
    if (x == (words[m]))
        res = 0;

    // Check if x is present at mid
    if (res == 0)
        return m;

    // If x greater, ignore left half
    if (x > (words[m]))
        l = m + 1;

    // If x is smaller, ignore right half
    else
        r = m - 1;
}

return -1;
}

int main() {
ifstream inFile;
inFile.open("test.txt");

if (inFile.fail()) {
    cerr << "Error opening file" << endl;

    exit(1);
}

string x1;
string words[100];
int count = 0, i = 0;
string str;

while (!inFile.eof()) {
    inFile >> x1;
    words[i] = x1;
    count++;
    i++;
}

for (i = 0; i < 100; i++) {
    cout << words[i] << endl;
}

string x;
x = "fine";
int n = 11;
int result = binarySearch(words, x, n);
if (result == -1)
    cout << ("\nElement not present");
else
    cout << ("Element found at index ") << result;

return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-22
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    相关资源
    最近更新 更多