⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 priorityqueue3.cpp

📁 希望我提供的代码对大家有帮助
💻 CPP
字号:
//: C20:PriorityQueue3.cpp

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

// A more complex use of priority_queue

#include <iostream>

#include <queue>

#include <string>

using namespace std;



class ToDoItem {

  char primary;

  int secondary;

  string item;

public:

  ToDoItem(string td, char pri ='A', int sec =1)

    : item(td), primary(pri), secondary(sec) {}

  friend bool operator<(

    const ToDoItem& x, const ToDoItem& y) {

    if(x.primary > y.primary) 

      return true;

    if(x.primary == y.primary)

      if(x.secondary > y.secondary) 

        return true;

    return false;

  }

  friend ostream& 

  operator<<(ostream& os, const ToDoItem& td) {

    return os << td.primary << td.secondary 

      << ": " << td.item;

  }

};



int main() {

  priority_queue<ToDoItem> toDoList;

  toDoList.push(ToDoItem("Empty trash", 'C', 4));

  toDoList.push(ToDoItem("Feed dog", 'A', 2));

  toDoList.push(ToDoItem("Feed bird", 'B', 7));

  toDoList.push(ToDoItem("Mow lawn", 'C', 3));

  toDoList.push(ToDoItem("Water lawn", 'A', 1));

  toDoList.push(ToDoItem("Feed cat", 'B', 1));

  while(!toDoList.empty()) {

    cout << toDoList.top() << endl;

    toDoList.pop();

  }

} ///:~

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -