数据类型:
class Student
{
public:
Student();
QString name;
int age;
int num;
void insertData(); // 初始化数据
QList<Student> m_stu; // Qlist列表数据
static bool compareBarData(const Student &stu1, const Student &stu2);// 用于对比的 函数
int m_value = 1;
};
添加 初始化数据 ,可以用按钮调用一直添加.
void Dialog::insertData()
{
Student a;
m_value ++;
a.age =10 + m_value;
a.num = 20 + m_value*2;
a.name = "asdf";
m_stu.append(a);
}
对比函数声明
bool Dialog::compareBarData(const Student &stu1, const Student &stu2)
{
return stu1.age > stu2.age;
}
重点: 在你需要使用排序的地方调用qSort就可以了.
qSort(m_stu.begin(),m_stu.end(),compareBarData);
本文介绍了一段C++代码,展示了如何创建一个Student类,使用QList来存储对象,并实现数据初始化和排序功能。通过`insertData`函数添加Student对象,`compareBarData`函数进行年龄升序排序,最后使用`qSort`进行列表排序。这为理解和应用C++容器及排序提供了实例。
5061

被折叠的 条评论
为什么被折叠?



