64,639
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
struct Stu
{
string No;
string Name;
};
class Table
{
public:
void build(char *flName);
void show();
void sortNo();
void sortName();
private:
/* 摆这里会报错,为什么??
bool cmpNo(const Stu& s1, const Stu& s2)
{return s1.No < s2.No;}
bool cmpName(const Stu& s1, const Stu& s2)
{return s1.Name < s2.Name;}
*/
list<Stu> T;
ifstream In;
};
bool cmpNo(const Stu& s1, const Stu& s2)
{return s1.No < s2.No;}
bool cmpName(const Stu& s1, const Stu& s2)
{return s1.Name < s2.Name;}
void Table:: build(char *flName)
{
In.close();
In.clear();
In.open(flName);
Stu tmp;
while(In >> tmp.Name && In >> tmp.No)
T.push_back(tmp);
}
void Table:: show()
{
list<Stu>::iterator it = T.begin();
cout << "\nafter sort:" << endl;
while(it != T.end())
{
cout << it->Name << "\t" << it->No << endl;
it++;
}
}
void Table:: sortNo()
{
T.sort(cmpNo);
//sort (T.begin(), T.end(), cmpNo); 不行
//stable_sort(T.begin(), T.end(), cmpNo); 不行..
}
void Table:: sortName()
{
T.sort(cmpName);
}
int main(int argc, char *argv[])
{
Table A;
A.build("a.txt"); A.show();
A.sortNo(); A.show();
A.sortName(); A.show();
system("PAUSE");
return EXIT_SUCCESS;
}
// list 不能使用STL 演算法 sort(),必須使用自己的 sort() member function,
// 因為STL演算法sort() 只接受RamdonAccessIterator.
// 本函式採用 quick sort.
template <class T, class Alloc>
void list<T, Alloc>::sort() {
// 以下判斷,如果是空白串列,或僅有一個元素,就不做任何動作。
// 使用 size() == 0 || size() == 1 來判斷,雖然也可以,但是比較慢。
if (node->next == node || link_type(node->next)->next == node) return;
// 一些新的 lists,做為中介資料存放區
list<T, Alloc> carry;
list<T, Alloc> counter[64];
int fill = 0;
while (!empty()) {
carry.splice(carry.begin(), *this, begin());
int i = 0;
while(i < fill && !counter[i].empty()) {
counter[i].merge(carry);
carry.swap(counter[i++]);
}
carry.swap(counter[i]);
if (i == fill) ++fill;
}
for (int i = 1; i < fill; ++i)
counter[i].merge(counter[i-1]);
swap(counter[fill-1]);
}
//可以改成这样
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <list>
#include <algorithm>
#include <string>
using namespace std;
struct Stu
{
string No;
string Name;
};
class Table
{
public:
void build(char *flName);
void show();
void sortNo();
void sortName();
private:
struct cmpNo
{
bool operator()(const Stu& s1, const Stu& s2)
{
return s1.No < s2.No;
}
};
struct cmpName
{
bool operator()(const Stu& s1, const Stu& s2)
{
return s1.Name < s2.Name;
}
};
list<Stu> T;
ifstream In;
};
bool cmpName(const Stu& s1, const Stu& s2)
{
return s1.Name < s2.Name;
}
void Table:: build(char *flName)
{
In.close();
In.clear();
In.open(flName);
Stu tmp;
while(In >> tmp.Name && In >> tmp.No)
T.push_back(tmp);
}
void Table:: show()
{
list<Stu>::iterator it = T.begin();
cout << "\nafter sort:" << endl;
while(it != T.end())
{
cout << it->Name << "\t" << it->No << endl;
it++;
}
}
void Table:: sortNo()
{
T.sort(cmpNo());
}
void Table:: sortName()
{
T.sort(cmpName());
}
int main(int argc, char *argv[])
{
Table A;
A.build("a.txt"); A.show();
A.sortNo(); A.show();
A.sortName(); A.show();
system("PAUSE");
return EXIT_SUCCESS;
}