// // multiset で sorted collection を作る // #include <list.h> #include <assert.h> #include <iostream.h> #include <multiset.h> // for gcc-2.8.1 and libg++ // #include <set> for Visual C++ Ver.6.0 class Data { public: Data( int t, int v ) { m_time = t; m_value1 = v; } int m_time; int m_value1; } ; typedef Data * DataPtr; class DataLess { public: bool operator() ( const DataPtr &p, const DataPtr &q ) const { if ( p->m_time != q->m_time ) return p->m_time < q->m_time ; else return p->m_value1 < q->m_value1 ; } } ; int main() { list<DataPtr> list1; list1.push_back( new Data( 1, 100 ) ); list1.push_back( new Data( 3, 300 ) ); list1.push_back( new Data( 3, 370 ) ); list1.push_back( new Data( 2, 220 ) ); list1.push_back( new Data( 3, 310 ) ); list1.push_back( new Data( 3, 330 ) ); list1.push_back( new Data( 3, 350 ) ); list1.push_back( new Data( 3, 390 ) ); list1.push_back( new Data( 5, 520 ) ); list1.push_back( new Data( 4, 460 ) ); multiset<DataPtr, DataLess> multiset1; list<DataPtr>::iterator i; for (i = list1.begin(); i != list1.end(); ++i) multiset1.insert(*i); multiset<DataPtr, DataLess>::iterator k; for (k = multiset1.begin(); k != multiset1.end(); ++k) { cout << "time=" << (*k)->m_time << " val=" << (*k)->m_value1 << endl; } }
time=1 val=100 time=2 val=220 time=3 val=300 time=3 val=310 time=3 val=330 time=3 val=350 time=3 val=370 time=3 val=390 time=4 val=460 time=5 val=520