sort()
场景:
1.设计好的结构体链表,并不能直接使用c++的sort函数,所以得先把这个链表的元素放到vector里排序,之后再链接成新链表。
2.需要对链表里的元素根据元素的特定属性比较,进而排序。
3.自己重新写一个快速排序肯定是耗费时间的,所以利用C++的sort模版函数肯定省不少时间,这时候看出C++模版的优势了吧。
代码:
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef struct _Info
{
struct _Info *prev;
struct _Info *next;
char* dir;
char* name;
}Info;
bool InfoCmp(Info* a ,Info* b)
{
if(a->dir)
{
if (b->dir)
{
return strcmp(a->dir,b->dir) < 0;
}else
{
return true;
}
}else
{
return false;
}
}
int main(int argc, char cons