【问题标题】:Using struct with an insertion sort使用带有插入排序的结构
【发布时间】:2015-03-26 06:22:36
【问题描述】:

我在插入排序时遇到了一些问题,从结构中传递数据。 它返回错误:|98|无法在赋值中将 'store' 转换为 'int'。

struct store{
    char tag[5];
    int cost;
    long int volume;
};


void costSort(store storedEntries[], int count);
void volumeSort(store storedEntries[], int count);

int main(){

    store record[100];
    ifstream fin;
    char choice;
    int count = 0;

    fin.open("stockdata.txt");

    //file read in
    if(fin.good())
    {
        while(!fin.eof())
        {
            fin >> record[count].tag;
            fin >> record[count].cost;
            fin >> record[count].volume;
            count++;
        }
    count--;
    }


    cout << "Main Menu:" << endl;
    cout << "c: sort data by Cost\nv: sort data by trade Volume\nq: Quit\nEnter Choice: ";

    cin >> choice;

    switch(choice)
    {
        case 'C':
        case 'c': //costSort(record, count);
            break;
        case 'V':
        case 'v': volumeSort(record, count);
            break;
        case 'q':
        case 'Q': return 0;
            break;
    }
   return 0;
}

void volumeSort(store record[], int count)
{
    int p = 0, item = 0;

    for(int i=1; i<count; i++){
    cout << "test";
        item = record[i];
        p = (i - 1);
        while(p>=0 && item < record[p]){
            record[p+1] = record[p];
            p--;
        }
        record[p+1] = item;
        cout << record[i].tag << " " << record[i].volume << endl;
    }

}

插入排序在函数 void volumeSort() 中。 任何建议将不胜感激,到目前为止我还没有遇到任何问题:S

【问题讨论】:

  • 欢迎来到这里,请将检测到错误的代码贴出来。
  • 感谢您的提醒,对插入排序有什么建议吗? @philant,检测到错误的代码在函数volumeSort中。行“项目=记录[i];”
  • item = record[i] 肯定是错的; itemintrecord[i]storeitem &lt; record[p] 也是如此。而record[p+1] = item,同样错误。

标签: c++ struct insertion-sort


【解决方案1】:

您正在比较不相似的类型,但没有提供支持比较的运算符(如果正确完成,则没有需要)。目前,您正在将 intstore 进行比较。您应该比较的是两个商店对象的两个 volume 成员。

一个可能更接近您想要的简单循环是这样的:

// note: count is size_t, an unsigned magnitude. only used signed
//  integer types where it makes sense a negative integer will be
//  plausible input.
void volumeSort(store record[], size_t count)
{
    for(size_t i=1; i<count; ++i)
    {
        // compare current element to one below us, swapping if needed
        // and stopping as soon as we reach an equal or lesser record
        size_t j=i;
        while(j>0 && record[j].volume < record[j-1].volume)
        {
            std::swap(record[j-1], record[j]);
            --j;
        }
    }
}

或类似的东西。注意比较:

record[j].volume < record[j-1].volume

在while条件下。苹果对苹果...


对于一个有趣的insertion_sort,它利用了标准库的两个出色功能std::upper_boundstd::rotate,可以创建一个相当密集的函数版本,如下所示:

void insertion_sort(store record[], size_t len)
{
    for (auto it = record; it != record+len; ++it)
    {
        std::rotate(std::upper_bound(record, it, *it,
            [](const store& lhs, const store& rhs) { return lhs.volume < rhs.volume; }),
            it, std::next(it));
    }
}

这比最初看起来要高效得多,因为使用std::upper_bound 在 O(logN) 中完成对潜在客户元素的正确放置的搜索。然后std::rotate 打开元素所在的孔并将其交换到位。

只是一些思考的食物。再加上将通过甚至补救优化内联的比较器,它的冲击力比您最初想象的要大得多。仍然没有 std::sort 那样出色,通常高度利用多种算法进行了优化,但仍然是很好的大脑食物。

祝你好运。

【讨论】:

    【解决方案2】:

    您正在尝试将intstore 进行比较。

    除非您重载 &lt; 运算符来比较 intstore,否则这将不起作用。

    store record[];
    int p = 0, item = 0;
    //[...]
    while (p >= 0 && item < record[p])
    //Neither can you assign that
    record[p + 1] = item;
    

    运算符示例:

    bool operator<(const int &left, const store &s)
    {
        //You could also do some calculation in here,
        //if you want to compare a value inside the struct
        //like this:
        return left < s.cost;
        //Please... do it in place.
        //item < record[p].cost;
    }
    

    【讨论】:

    • “自己的运营商”是什么意思?我尝试将 int 值更改为存储,反之亦然,但没有成功
    • 您可以编写一个operator&lt; 函数,该函数接受一个int 和一个store,并根据store 中的一个值或您希望比较它们的值来比较两者。但是你不能比较两种完全不同的类型。这根本行不通。
    • @Cirrus 用operator&lt; 示例更新了我的答案。
    • Fwiw 我认为在编写运算符 可以;你这样做是因为你应该
    • @WhozCraig 当然在这里没有任何意义。但我不知道他实际上想要达到什么目的。最有可能的测试代码......所以至少他现在知道,他如何可以重载运算符。 - 更新答案...
    【解决方案3】:

    如果你想按体积排序,你应该采取 record[i].volume 即使在比较时...比较值时类型应该相同..

    其他情况也一样..

    【讨论】:

      猜你喜欢
      • 2022-06-10
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      相关资源
      最近更新 更多