在C 中使用静态功能,我的教授希望我告诉Main哪个对象具有最高优先级,而无需传递任何内容
Using a static function in c++ my prof wants me to tell main which object has the highest priority without passing anything
i获得了以下主函数,我必须编码对象以通过。
这是主要的:
#include <iostream>
#include <string>
#include <ctime>
#include <stdexcept>
#include "ToDo.h"
using namespace std;
int getRand(int min, int range) {
return (rand() % range) + min;
}
const unsigned int NUM_VERB = 4;
const unsigned int NUM_ACTIVITY = 7;
const string VERB[] = { "Play", "Work on", "Practice","Eat", };
const string TARGET[] = { "homework", "dishes", "games", "guitar","vacuuming","aardvarking","coding" };
int main()
{
srand(static_cast<unsigned int>(time(NULL))); // seed random number generator
int numTask = getRand(3, 3); // number of tasks is 3->3+3
ToDo** tasks = new ToDo*[numTask]; // create array of ToDo pointers, sized with numTask
// creates new ToDo objects and keeps the pointers in tasks array
for (int i = 0; i < numTask; i++) {
tasks[i] = new ToDo(getRand(1, 9), VERB[rand() % NUM_VERB] + " " + TARGET[rand() % 7]);
}
cout << "The tasks are:n" << "PrioritytTaskn";
// lists the ToDo objects using the output() member
for (int i = 0; i < numTask; i++) {
cout << tasks[i]->output("tt") << endl;
}
cout << "nYou should work on:n";
cout << ":==> " << ToDo::highestPriority()->getTask() << endl << endl;
unsigned int increaseBy = rand() % 7 + 1;
cout << "But if i increase the priority of: " << tasks[numTask -1]->getTask() << " by " << increaseBy << endl;
tasks[numTask - 1]->increasePriority(increaseBy);
cout << "nYou should work on:n";
cout << ":==> " << ToDo::highestPriority()->output(": ") << endl;
// make sure all priorities are greator than 0
for (int i = 0; i < numTask; i++) {
if (tasks[i]->getPriority() < 1) {
throw invalid_argument("Invalid Priority Found!");
}
}
// de-allocate memory, pointer null-ing not important as end of progran
for (int i = 0; i < numTask; i++) {
delete tasks[i];
}
delete tasks;
getchar();
return 0;
}
我对ToDo::highestPriority()->getTask()
和ToDo::highestPriority()->output(": ")
感到困惑,我不知道我将如何使用这些位置来告诉阵列中的哪个位置的优先级最高。
我的运行理论是使用3个静态int
s,如下:
- 跟踪对象的数量(计数器)
- 跟踪哪个对象具有最高优先级(通过使其在最高优先级相等),
- 跟踪最高优先级的数字。
我仍然无法弄清楚如何判断数组中的哪个位置的优先级最高。
我不能编辑主函数,只能创建一个对象,任何人都可以帮助我解决这个问题吗?
您需要定义 class Todo
,并且存在现有用法。
让我们从最小的实现开始:
class Todo {
public:
static ToDo * highestPriority() { return nullptr; }
Todo (int, std::string) {}
int getPriority() { return {}; }
std::string getTask() { return {}; }
std::string output(std::string) { return {}; }
void increasePriority(int) {}
}
现在我们需要对每个
做明智的事情class ToDo;
bool todo_less(ToDo const * lhs, ToDo const * rhs) { return lhs->getPriority() < rhs->getPriority(); }
class ToDo {
int priority;
std::string task;
static std::vector<ToDo*> instances;
public:
static ToDo * highestPriority() { return *std::max_element(instances.begin(), instances.end(), todo_less); }
ToDo (int _priority, std::string _task) : priority(_priority), task(_task) { instances.push_back(this); }
~ToDo() { instances.erase(std::find(instances.begin(), instances.end(), this)); }
int getPriority() const { return priority; }
std::string getTask() const { return task; }
std::string output(std::string joiner) const { return to_string(priority) + joiner + task; }
void increasePriority(int inc) { priority += inc; }
}
相关文章:
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 按对象的特定方法按升序排列的C++优先级队列
- 为什么STL优先级队列错误地分配了我的类对象
- 具有自定义对象的C 优先级队列
- 在优先级队列被推送到队列后,如何编辑对象成员
- 进程中线程的最高优先级是多少?
- 是否可以更改在优先级队列中添加具有相同优先级的"side"对象?
- C++使用对象的优先级队列
- 在C 中使用静态功能,我的教授希望我告诉Main哪个对象具有最高优先级,而无需传递任何内容
- 为什么为我的对象类编译标准优先级队列失败
- 模板化优先级队列导致对象成为指针.C++
- 具有对象的最低优先级队列
- 在继承中使用delete的意外行为,基指针对象指向派生程度最高的类
- 为什么C++操作优先级在处理对象或基元类型++a*b/b--a时不同
- C++对象可以存储在最高内存位置吗
- 内置类型的Do对象具有特殊的静态初始化顺序优先级
- 在c++中使用用户定义对象的优先级时遇到问题
- 将堆栈对象存储在优先级队列中
- 优先级队列排序指针指向改变值的对象.c++
- 使对象的优先级队列