C++:二进制搜索字符串的一部分

标签 c++ string algorithm binary-search

我现在正在做家庭作业的第 4 部分,我正在尝试找出如何对字符串数组中的前 14 个字符进行二进制搜索。我们得到了两个 .csv 文件:

01110011100110 是模拟条码。

Inventory.csv:以逗号分隔的库存项目 - 条形码、产品、价格 前五行:

01001010100011,Air freshener,12.43
01101000111101,Alfredo sauce,10.71
10000010100101,All spice,4.14
00011011001111,Allergy medication,20.06
00101010111011,Aluminum foil,8.92

Carts.csv:包含购买条形码的“购物车” 前五行:

01000011010100,00010011010100
00110111100000
00100110100000,01011000110111,01011101000110,01011101000000,01001110000010
01110101100101,00100111110101,00101101110110,00100110000000,00100000001100,00101101011100
00101100111110,01000110110000,01010110111110,00100111111001,01011101101010,01011011010011,00010011010100,01010111101001

我正在做作业的第 4 部分: 第 4 部分:编写函数以读取 Carts.csv 文件。一次读取一行并处理它。工艺步骤为: A。根据逗号分隔符分隔每个购物车中的条形码。
b.对于每个条形码,在产品列表中查找条形码。 C。从产品字符串数据行中提取价格并将其添加到购物车的运行总计中。 d.处理该行后,输出购物车的总价。 e.重复步骤 (a) 直到文件结束。

当我尝试查找它返回 -1 时,它比较的是整行而不是其中的一部分。二进制搜索如何只是字符串的一部分。这是我的代码的摘录:

int binarySearch(string* list, int size, string value){
    int first = 0,
        last = size - 1, middle,
        position = -1;
        bool found = false;
    while (!found && first <= last){
        middle = (first + last) / 2;

        if (list[middle].compare(value) == 0){
            found = true;
            position = middle;
        }
        else if (list[middle].compare(value) > 0)
            last = middle - 1;
        else
            first = middle + 1;     
    }
    return position;
}

void processFile(string filename, string* list, int size){
    ifstream file(filename);
    string line;
    int count = 1;
    if (file.is_open()){
        while (!file.eof()){
            int ctr = 0;
            int start = 0;
            getline(file, line);
            string substring = "";
            int find = line.find(COMMA);
            cout << "Cart " << count << ": ";
            while (find != string::npos){
                substring = line.substr(start, find - start);
                int position = binarySearch(list, size, substring);
                cout << position << " ";
                start = find + 1;
                ctr++;
                find = line.find(COMMA, start);
            }
            if (ctr > 0 || find == -1){
                substring = line.substr(start, line.length() - start);
                int position = binarySearch(list, size, substring);
                cout << position << endl;
                count++;
            }
        }
    }
    file.close();
}

最佳答案

试试下面的功能实现

int binarySearch( const std::string* list, int size, std::string value )
{
    int position = -1;
    int first = 0, last = size - 1;

    while ( first <= last )
    {
        int middle = ( first + last ) / 2;

        int result = list[middle].substr( 0, value.size() ).compare( value );
        if ( result == 0 )
        {
            position = middle;
            break;
        }
        else if ( result > 0 )
        {
            last = middle - 1;
        }
        else
        {
            first = middle + 1;
        }      
    }

    return position;
}

当然数组列表必须按照条码升序排列。

关于C++:二进制搜索字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23202694/

相关文章:

c++ - Matlab矩阵逆函数在C++中的Eigen实现

c++ - 返回非静态本地对象时,选择复制构造函数而不是 move 构造函数

string - 从 IPv4 地址的字符串表示中获取特定的八位字节

javascript - 如何删除尾随空格而不是字符串中的空格(也不是开头的空格)?

algorithm - 所有连续素数长度子数组的最大和

c++ - "Everything Search"究竟如何能在不到 10 秒的时间内为我提供 4TB 硬盘上 20 亿个文件的可立即搜索列表?

c++ - 错误 : no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(int&)’

python - 将随机字符序列转换为 JSON

c# - C# 错误中的 Dijkstra 算法

algorithm - 如何在无向图中找到桥?