Feeds:
Posts
Comments

Archive for March, 2009

STL tips

int value[100];
struct myless: public less<int>
{
bool operator()(int a, int  b) const{return (value[a] <value[b]||(value[a]==value[b]&&a<b));}
};
int main()
{
set<int,myless> stree;
map<int,int,myless> mtree;
priority_queue<int,vector<int>,myless> pq;
const int n=10;
for(int i=0;i<n;i++)value[i]=-i/2;
for(int i=0;i<n;i++)stree.insert(i),mtree[i]=-i,pq.push(i);
cout<<stree.size()<<" "<<mtree.size()<<" "<<pq.size()<<endl;
cout<<"-----------------------------"<<endl;
for(set<int,myless>::iterator it=stree.begin();it!=stree.end();it++)
cout<<(*it)<<" "<<value[*it]<<endl;
cout<<"-----------------------------"<<endl;
for(map<int,int,myless>::iterator it=mtree.begin();it!=mtree.end();it++)
cout<<"("<<it->first<<","<<it->second<<") "<<value[it->first]<<endl;
cout<<"-----------------------------"<<endl;
while(pq.size())cout<<pq.top()<<" "<<value[pq.top()]<<endl,pq.pop();
return 0;
}
Advertisements

Read Full Post »