C++中的新手。我想創建一個動態對象數組,並使用std :: sort()對它們進行排序。但是,出現了一些錯誤,我無法弄清楚原因。謝謝你的幫助。 中出現錯誤是這樣的:iterator_category':不是任何基類的成員'std :: iterator_traits'<_InIt>'
> community\vc\tools\msvc\14.10.25017\include\xutility(988): > error C2794: 'iterator_category': is not a member of any direct or > indirect base class of 'std::iterator_traits<_InIt>' > with > [ > _InIt=Problem > ] \include\algorithm(2915): > note: see reference to function template instantiation 'void > std::_Debug_range<_RanIt>(_InIt,_InIt,std::_Dbfile_t,std::_Dbline_t)' > being compiled > with > [ > _RanIt=Problem, > _InIt=Problem
#include "stdafx.h"
#include "stdlib.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class Problem{
public:
string name;
int t;
int d;
Problem() {}
Problem(string name,int t,int d):name(name),t(t),d(d) {}
~Problem() {}
bool operator<(const Problem &right) const {
if (t == right.t) return d < right.d;
else return t < right.t;
}
};
void FindOrder(int H, int N, int t0, Problem ProblemSet[]);
bool compare(const Problem &left,const Problem &right) {
if (left.t == right.t) return left.d < right.d;
else return left.t < right.t;
}
int main()
{
int H, N, t0;
cin >> H;
while (H >= 0) {
cin >> N >> t0;
//Problem ProblemSet = (Problem)malloc(N * sizeof(struct ProblemNode));
Problem* ProblemSet = new Problem[N];
for (int i = 0;i<N;i++)
cin >> ProblemSet[i].name >> ProblemSet[i].t >> ProblemSet[i].d;
FindOrder(H, N, t0, ProblemSet);
delete[] ProblemSet;
cin >> H;
}
return 0;
}
void FindOrder(int H, int N, int t0, Problem ProblemSet[]) {
int total = t0;
sort(ProblemSet[0], ProblemSet[N-1]);
for (int i = 0;i < N;i++) {
cout << ProblemSet[i].name << ProblemSet[i].t << ProblemSet[i].d << endl;
}
}
您的錯誤消息,在那麼結束,應該告訴你什麼行代碼產生了這個混亂。那是什麼? – NathanOliver
'sort(ProblemSet [0],ProblemSet [N-1]);'是錯誤的。索引到一個未知大小的數組是不會產生迭代器的。你可能意思是'排序(ProblemSet,ProblemSet + N);' – WhiZTiM
錯誤信息基本上說「Product'不是迭代器」。你把'產品'傳給'排序'。 – molbdnilo