c++ - 通过 'next_permutation'算法发送一个struct对象的vector构建失败

标签 c++ algorithm struct

我在使用蛮力算法时遇到了问题。正如您从下面的代码中看到的那样,我正在尝试评估一个结构 vector 并找到对一系列定时事件进行排序的最有效方法。这是我将其对象放置在“part1Vec” vector 中的简单结构布局:

struct person
{
    float swim;
    float bike;
    float run;

    float bikeRun;

    person();

    person(float swim, float bike, float run)
    {
        this->swim = swim;
        this->bike = bike;
        this->run = run;

        this->bikeRun = bike + run;
    };
};

但是,当我编译时,我在算法类中遇到了一个错误,据推测我可以追溯到这一行:

while (next_permutation(part1Vec.begin(), part1Vec.end()))

我得到的错误信息是这样的:

//Invalid operands to binary expression ('const person' and 'const person')

我相信我已经正确实现了 next_permutation 函数,但我不明白为什么它会给我这个错误。任何帮助将不胜感激,谢谢。这是我的完整代码:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct person
{
    float swim;
    float bike;
    float run;

    float bikeRun;

    person();

    person(float swim, float bike, float run)
    {
        this->swim = swim;
        this->bike = bike;
        this->run = run;

        this->bikeRun = bike + run;
    };
};

//function declarations and global variables
vector<person> part1Vec;
vector<person> highestVec;

float highestRating;
float tempRating;

float rating(vector<person> vector);

int main()
{
    //PART 1

    //make objects
    person one(20, 25, 20), two(35, 20, 15), three(40, 20, 30);

    //insert into vector
    part1Vec.push_back(one);
    part1Vec.push_back(two);
    part1Vec.push_back(three);

    cout << "_________swim__bike__run__" << endl;

    for (int i=0; i<part1Vec.size(); i++)
    {
        cout << "Vector #" << i+1 << ": "
        << part1Vec[i].swim << "  "
        << part1Vec[i].bike << "  "
        << part1Vec[i].run;
    }

    cout << endl << "Calculating..." << endl;

    //Next permutation function
    while (next_permutation(part1Vec.begin(), part1Vec.end())) //Invalid operands to binary expression ('const person' and 'const person')
    {
        //initialize highestVec
        if (highestVec.size() == 0)
        {
            highestRating = rating(part1Vec);
            highestVec = part1Vec;
        }
        //if Highest Rating is less than current permutation, update.
        else if (highestRating < (tempRating = rating(part1Vec)) )
        {
            highestVec = part1Vec;
            highestRating = tempRating;
        }
    }

    cout << "Best Solution:" << endl;
    for (int i=0; i<part1Vec.size(); i++)
    {
        cout << "Vector #" << i+1 << ": "
        << highestVec[i].swim << "  "
        << highestVec[i].bike << "  "
        << highestVec[i].run;
    }

    cout << endl << "Rating: " << highestRating << endl;

    return 0;
}

float rating(vector<person> thisVector)
{
    float rating = 0;
    float swimSum = 0;

    for (int i=0; i<thisVector.size()-1; i++)
    {
        swimSum += thisVector[i].swim;

        if (rating < swimSum + thisVector[i].bikeRun)
            rating = swimSum + thisVector[i].bikeRun;
    }

    return rating;
}

最佳答案

Kyle Y.,你明白克里斯的意思了吗?

我希望 Chris 不会介意,但为了以防万一,我会冒昧地详细说明。

首先,说明您使用的是什么编译器。原因之一是它们给出了不同的错误信息。在这种情况下,Invalid operands to binary expression ('const person' and 'const person')有点用处,但没有发挥应有的作用。如果我通过 gcc 运行它,例如,它可能会告诉我更多类似的信息 std::next_permuation正在寻找未定义的运算符。

std::next_permuation使用井序生成排列。因此,它需要具有自身定义顺序的类型参数,这样算法将始终终止,一致顺序(如果 a<(b) 和 b<(a) 是,则顺序不一致可能,无论如何这通常都是不可取的)。

这就是 chris 所指的,在 C++ 中使用像您这样的结构类型(其顺序尚未由基类定义)的方式是覆盖 bool operator<(...在结构中。

因为您只需要排列生成的顺序,所以任何旧的顺序都可以,只要:一切都有序并且顺序一致,如上所述。

参见 here使用该覆盖和一些 unsigned int 再次为您的代码在他们应该在的地方,注意:

    bool operator<(const person& rhs) const {
    return (this->swim + this->bike + this->run) < (rhs.swim + rhs.bike + rhs.run);
}

最好的。

关于c++ - 通过 'next_permutation'算法发送一个struct对象的vector构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19341253/

相关文章:

algorithm - 从一组偏序中重建一个序列

javascript - 从 Node HTTP 请求中运行的算法需要更长的时间来运行

c++ - 如果 include_directories 中的路径之一不存在,为什么 CMakeLists 不抛出警告/错误(在编译期间)?

C++ 新手 : using stream for writing data to file. .. 为什么我的代码引入回车?

algorithm - 哪种搜索算法失败最快

c++ - 结构 C++ 的数组

C题, '->'左边必须指向class/struct/union/generic类型?

c - 为什么这段使用 qsort 函数的代码在 C 中不起作用?

c# - 在 C# 中读写 C++ 动态数组 (InteropServices)

c++ - 如何在字符串中搜索 C++