У меня возникли проблемы с тем, чтобы алгоритм грубой силы работал. Как вы можете видеть из моего кода ниже, я пытаюсь оценить вектор структур и найти наиболее эффективный способ упорядочить серию событий времени. Вот моя простая структура макета, объекты которого я помещаю в векторе «part1Vec»:Отправка вектора объектов stuct через алгоритм 'next_permutation' не удается построить
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;
}
Он должен знать, как заказать ваши объекты. – chris