vector sort结构体排序要点

方法1:重载运算符

struct pcap_pkt{
DWORD seq_number;   
DWORD ack_number; 
bool operator< (const pcap_pkt& pkt1) const
{
if (seq_number == pkt1.seq_number)
return ack_number < pkt1.ack_number;
else
return seq_number < pkt1.seq_number;
//return false;
}

} pcap_pkt;

注意vector存储的是pcap_pkt结构体而非结构体指针

 

方法2:自定义sort的等三个参数

struct pcap_pkt{
DWORD seq_number;   
DWORD ack_number; 

} pcap_pkt;

bool comp(const pcap_pkt* pfirst,const pcap_pkt* psecond) 

{                                                  
 if (pfirst->seq_number == psecond->seq_number)
return pfirst->ack_number < psecond->ack_number;
else
return pfirst->seq_number < psecond->seq_number;
}

sort(pcap_pkt.begin(),pcap_pkt.end(),comp);

注意vector存储的是pcap_pkt结构体指针而非结构体


你可能感兴趣的:(C++,结构,vector)