前田稔(Maeda Minoru)の超初心者のプログラム入門
ファイル名 | 説明 |
---|---|
listsort.cpp | List ソート関数 |
#include <iostream> #include <list> using namespace std; typedef struct { int key; char s[8]; } LANK; LANK tbl[6]= { {5,"suzuki"}, {3,"tanaka"}, {1,"aoki"}, {4,"maeda"}, {6,"yamada"}, {2,"ozawa"} }; |
list<LANK> v; int i; for(i=0; i<6; i++) v.push_back(tbl[i]); |
Sort(v); |
list<LANK> v; list<LANK>::iterator p; for(p=v.begin(); p!=v.end(); p++) cout << p->key << " " << p->s << endl; |
void Sort(list<LANK> &pv) { list<LANK>::iterator p,q,ep; int sw; ep= pv.end(); ep--; sw= 1; while(sw) { for(sw=0,p=pv.begin(); p!=ep; p++) { q= p; q++; if (p->key > q->key) { sw =1; swap(*p,*q); } } } } |