文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>stl pri

stl pri

时间:2010-08-01  来源:chhaya


/* 基本数据类型 */

#include <iostream>
#include <queue>
using namespace std;

int main()
{
  int i;
  int a[10];
  priority_queue<int, vector<int>, greater<int> >big;
  priority_queue<int, vector<int>, less<int> > small;

  for(i=0; i<10; i++) {
    a[i] = rand() % 10;
    big.push(a[i]);
    small.push(a[i]);
  }

  for(i=0; i<10; i++) {
    cout << a[i] << ' ';
  }
  cout << endl;

  while(!big.empty()) {
    cout << big.top() << " ";
    big.pop();
  }
  cout << endl;

  while(!small.empty()) {
    cout << small.top() << " ";
    small.pop();
  }
  return 0;
}


/× 自定义结构 ×/

#include <iostream>
#include <queue>
using namespace std;
struct stata
{
  int a, b;
  stata(int aa=0, int bb=0):a(aa),b(bb)
  {}
};

bool operator<(const stata x, const stata y)
{
  return x.a < y.a;
}

struct cmp
{
  bool operator()(stata x, stata y)
  {
    return x.a > y.a;
  }
};


int main()
{
  priority_queue <stata> big; //最大优先
  priority_queue <stata, vector<stata>, cmp> small; //最小优先

  int aa[10], bb[10];
  for(int i =0; i < 10; i++)
  {
    aa[i] = rand() % 20;
    bb[i] = rand() % 20;
    big.push(stata(aa[i], bb[i]));
    small.push(stata(aa[i], bb[i]));
  }
  cout << "from big to small:" << endl;
  while(!big.empty())
  {
    cout << big.top().a << ' ' << big.top().b << endl;
    big.pop();
  }
  cout << endl;
  cout << "from small to big:" << endl;
  while(!small.empty())
  {
    cout << small.top().a << ' ' << small.top().b << endl;
    small.pop();
  }
  cout << endl;
  return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载