C++ 按姓氏和姓氏对结构排序
我有按姓氏排序的算法,但我很难弄清楚如何按姓氏排序,如果两个人的姓氏相同,就按他们的名字排序C++ 按姓氏和姓氏对结构排序,c++,sorting,struct,C++,Sorting,Struct,我有按姓氏排序的算法,但我很难弄清楚如何按姓氏排序,如果两个人的姓氏相同,就按他们的名字排序 void sortLastName(FRIEND friends[ARRAY_MAX], int& count) { FRIEND temp; for(int i = 0; i < count - 1; i++) { for (int j = i + 1; j < count; j++) { if (stricmp(frie
void sortLastName(FRIEND friends[ARRAY_MAX], int& count) {
FRIEND temp;
for(int i = 0; i < count - 1; i++) {
for (int j = i + 1; j < count; j++) {
if (stricmp(friends[i].lastName, friends[j].lastName) > 0) {
temp = friends[i]; //swapping entire struct
friends[i] = friends[j];
friends[j] = temp;
}
}
}
}
void sortLastName(友元友元[ARRAY_MAX],int&count){
朋友临时工;
对于(int i=0;i0){
temp=friends[i];//交换整个结构
朋友[i]=朋友[j];
朋友[j]=临时工;
}
}
}
}
==编辑====================
我不想使用
STD sort()
首先,比较姓氏。如果它们相等,则比较名字:
int compareResult = stricmp(friends[i].lastName, friends[j].lastName);
if(compareResult == 0)
compareResult = stricmp(friends[i].firstName, friends[j].firstName);
if(compareResult < 0)
// swap friends[i] and friends[j]
int compareResult=stricmp(朋友[i].lastName,朋友[j].lastName);
if(compareResult==0)
compareResult=stricmp(朋友[i].firstName,朋友[j].firstName);
如果(比较结果<0)
//交换朋友[i]和朋友[j]
首先,比较姓氏。如果它们相等,则比较名字:
int compareResult = stricmp(friends[i].lastName, friends[j].lastName);
if(compareResult == 0)
compareResult = stricmp(friends[i].firstName, friends[j].firstName);
if(compareResult < 0)
// swap friends[i] and friends[j]
int compareResult=stricmp(朋友[i].lastName,朋友[j].lastName);
if(compareResult==0)
compareResult=stricmp(朋友[i].firstName,朋友[j].firstName);
如果(比较结果<0)
//交换朋友[i]和朋友[j]
> p>首先,使用<代码> qStase或使用C++比较的适当的C++等价物,该函数采用两个对象的比较。
因此,比较应该是微不足道的:
int compare_by_name(const FRIEND& f1, const FRIEND& f2)
{
int last_name_matches = strcmpi(f1.lastName, f2.lastName);
return (last_name_matches != 0) ? last_name_matches :
strcmpi(f1.firstName, f2.firstName) ;
}
NB:一个真正的C++实现可能使用模板来为比较器函数使用。
首先,使用<代码> qStase或使用一个比较两个对象的函数的适当C++等价。
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
typedef boost::tuple<std::string, std::string> Friend;
Friend f1, f2;
bool compareFriends = f1 < f2;
因此,比较应该是微不足道的:
int compare_by_name(const FRIEND& f1, const FRIEND& f2)
{
int last_name_matches = strcmpi(f1.lastName, f2.lastName);
return (last_name_matches != 0) ? last_name_matches :
strcmpi(f1.firstName, f2.firstName) ;
}
NB:一个真正的C++实现可能使用模板作为比较器函数。
添加以下内容:
else if (stricmp(friends[i].lastName, friends[j].lastName) == 0 &&
stricmp(friends[i].firstName, friends[j].firstName) > 0) {
temp = friends[i]; //swapping entire struct
friends[i] = friends[j];
friends[j] = temp;
}
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
typedef boost::tuple<std::string, std::string> Friend;
Friend f1, f2;
bool compareFriends = f1 < f2;
添加以下内容:
else if (stricmp(friends[i].lastName, friends[j].lastName) == 0 &&
stricmp(friends[i].firstName, friends[j].firstName) > 0) {
temp = friends[i]; //swapping entire struct
friends[i] = friends[j];
friends[j] = temp;
}
你必须改变你的比较。基本算法是,如果friends[i]>friends[j],则交换它们。因此,请更改“>”的定义,以包含名比较
应该采取以下措施:
if (stricmp(friends[i].lastName, friends[j].lastName) > 0 ||
(stricmp(friends[i].lastName, friends[j].lastName) == 0 &&
stricmp(friends[i].firstName, friends[j].firstName) > 0))
不过,您可能只想进行一次姓氏比较(将其存储在临时变量中,而不是两次比较),但想法是一样的
请注意,“最好”的方法可能是在FRIEND类中提供比较函数。然后您可以使用if(friends[i].CompareTo(friends[j])>0
。您必须更改比较。基本算法是,如果friends[i]>friends[j],则交换它们。因此,请更改“>”的定义,以包含名比较
应该采取以下措施:
if (stricmp(friends[i].lastName, friends[j].lastName) > 0 ||
(stricmp(friends[i].lastName, friends[j].lastName) == 0 &&
stricmp(friends[i].firstName, friends[j].firstName) > 0))
不过,您可能只想进行一次姓氏比较(将其存储在临时变量中,而不是两次比较),但想法是一样的
请注意,“最好”的方法可能是在FRIEND类中提供比较函数。然后你可以使用如果(朋友[i].CompareTo(朋友[j])>0)
。请不要自己实现排序-std::sort(在
中)做得更好,效率更高。(除非你只是想看看你的算法是如何用于实验目的的)
您必须指定一个比较函数或更好的函子
struct FriendComparer {
bool operator () (const FRIEND& a, const FRIEND& b) {
// Comparison code here (see previous posts)
}
};
您可以这样调用它:
std::sort(friendArray, friendArray + count, FriendComparer());
请不要自己实现排序-std::sort(在
中)可以更好、更高效地完成这项工作。(除非你只是想看看你的算法是如何用于实验目的的)
您必须指定一个比较函数或更好的函子
struct FriendComparer {
bool operator () (const FRIEND& a, const FRIEND& b) {
// Comparison code here (see previous posts)
}
};
您可以这样调用它:
std::sort(friendArray, friendArray + count, FriendComparer());
为什么不使用std::sort
?这就是它的用途:
struct NameComparer {
bool operator()(const FRIEND& lhs, const FRIEND& rhs){
int compareResult = stricmp(lhs.lastName, rhs.lastName);
if(compareResult == 0){
compareResult = stricmp(lhs.firstName, rhs.firstName);
}
return compareResult < 0;
}
};
std::sort(friends, friends + ARRAY_MAX, NameComparer());
结构名称比较器{
布尔运算符(){
int compareResult=stricmp(lhs.lastName,rhs.lastName);
if(compareResult==0){
compareResult=stricmp(lhs.firstName,rhs.firstName);
}
返回比较结果<0;
}
};
排序(friends,friends+ARRAY_MAX,NameComparer());
当然,你也应该使用C++ <代码> STD::String 类。这就是它的目的。然后,您就不必使用容易出错的C字符串操作函数,比如stricmp
为什么不使用std::sort
?这就是它的用途:
struct NameComparer {
bool operator()(const FRIEND& lhs, const FRIEND& rhs){
int compareResult = stricmp(lhs.lastName, rhs.lastName);
if(compareResult == 0){
compareResult = stricmp(lhs.firstName, rhs.firstName);
}
return compareResult < 0;
}
};
std::sort(friends, friends + ARRAY_MAX, NameComparer());
结构名称比较器{
布尔运算符(){
int compareResult=stricmp(lhs.lastName,rhs.lastName);
if(compareResult==0){
compareResult=stricmp(lhs.firstName,rhs.firstName);
}
返回比较结果<0;
}
};
排序(friends,friends+ARRAY_MAX,NameComparer());
当然,你也应该使用C++ <代码> STD::String 类。这就是它的目的。然后,您不必使用容易出错的C字符串操作函数,如stricmp
除了可以选择构建一个使用两个名称的比较函数之外,您还可以先对姓氏排序,然后再对姓氏排序,但在第二遍时必须注意使用a。还是第一次用吧
好消息:std::stable_sort
是可用的,并且保证是稳定的(感谢Adam和libt的更正)。普通的std::sort
和c标准库qsort
不能保证是稳定的,尽管有些实现可能是稳定的。正如马克在评论中指出的那样,你展示的冒泡分类已经很稳定了
这比单排序和自定义比较函数选项的效率要低,但它可以让用户在运行时轻松选择多个排序(因为您不必定义所有可能的比较函数或小型语言)。除了选择构建使用两个名称的比较函数之外,