C++无效的结构比较向量排序向量
我试图对包含自定义结构的std::vector进行排序,每个结构都有自己的int值向量。关键是我想根据内部int向量的顺序进行排序。。。i、 e.{1,1,2,3,4}小于{1,2,2,3,4},因为向量中有第二个值 在我看来,这将提供严格的弱排序,但在运行它时,我总是收到一个无效的Comparator异常。我还尝试实现一个单独的键函数作为std::sort的第三个参数,但同样的情况也发生了 我做错了什么C++无效的结构比较向量排序向量,c++,sorting,c++11,vector,struct,C++,Sorting,C++11,Vector,Struct,我试图对包含自定义结构的std::vector进行排序,每个结构都有自己的int值向量。关键是我想根据内部int向量的顺序进行排序。。。i、 e.{1,1,2,3,4}小于{1,2,2,3,4},因为向量中有第二个值 在我看来,这将提供严格的弱排序,但在运行它时,我总是收到一个无效的Comparator异常。我还尝试实现一个单独的键函数作为std::sort的第三个参数,但同样的情况也发生了 我做错了什么 #include <iostream> #include <algori
#include <iostream>
#include <algorithm>
#include <vector>
typedef struct AA {
std::vector<int> AA_v;
bool operator<(const AA& a1)const {
for (int i = 0; i < this->AA_v.size(); i++) {
if (this->AA_v[i] < a1.AA_v[i]) {
return true;
}
}
return false;
}
}AA;
void main(void) {
AA a1, a2, a3, a4;
a1.AA_v = { 1, 1, 3, 5, 4 };
a2.AA_v = { 1, 1, 2, 4, 5 };
a3.AA_v = { 0, 1, 3, 5, 4 };
a4.AA_v = { 1, 1, 3, 4, 5 };
std::vector<AA> AA_vector;
AA_vector.push_back(a1);
AA_vector.push_back(a2);
AA_vector.push_back(a3);
AA_vector.push_back(a4);
std::sort(AA_vector.begin(), AA_vector.end());
}
试一试
bool operator<(const AA& a1)const {
for (int i = 0; i < this->AA_v.size(); i++) {
if (this->AA_v[i] < a1.AA_v[i]) {
return true;
}
else if (this->AA_v[i] > a1.AA_v[i]) {
return false;
}
}
return false;
}
必须是int main,而不是void main。我不知道你需要强制另一个为false,我很惊讶我没有遇到操作符
bool operator< (const AA& a1) const
{ return this->AA_v < a1.AA_v; }