C++ - 如何从 queue 中删除最大元素
C++ - How to delete a max element from queue
正如您从标题中看到的,我想从使用数组实现的 queue 中删除一个元素。
例如,如果我有:
array[]={1,5,4,3,2}
我希望它看起来像这样
array[]={1,4,3,2}
我需要将其从 queue 中删除,但以上只是我正在尝试执行的操作的简单版本。这是可能对您有帮助的其余代码,对我有帮助。
class QueueArray: Queue <InfoTip> {
private:
int capacity; // capacity
int beginning; // index of the first element
int end; // end of the array
InfoTip *elements; // elements of queue
public:
QueueArray(int capacity = 10) : capacity(capacity), // Konstruktor
beginning(-1),end(-1), elements (new InfoTip[capacity]) {}
~QueueArray() { delete [] elements; }
template <typename InfoTip> // putting elements in a queue
void QueueArray<InfoTip>::putInArray(const InfoTip &x) {
end = (end + 1) % capacity;
elements[end] = x;
if (beginning == -1)
beginning= 0;
}
这是我为从 queue 数组
中删除最大元素而编写的函数
template <typename InfoTip>
void QueueArray<InfoTip>::deleteMax() {
InfoTip max = 0;
for(int i = 0; i < capacity; i++) {
if(max < elements[i]) {
max = elements[i];
}
}
帮自己一个大忙,像这样使用 STL 容器和算法:
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v { 1,2,3,4,5,4,3,2,1 };
v.erase(std::max_element(v.begin(), v.end()));
for (auto x : v)
std::cout << x << " ";
std::cout << "\n";
}
打印:1 2 3 4 4 3 2 1
如果要使用队列
首先你必须创建一个 temp
变量来存储你所有的 element
数据。
for(int i = 0; i < capacity; i++) {
if(max < elements[i]) {
max = elements[i];
}
temp[i] = elements[i];
}
然后你必须创建一个 bool isTrue
变量,因为在找到最大元素后你必须跳过一个元素
bool isTrue = false;
for(int i = 0; i < capacity-1; ++i) {
if(temp[i] == elements[i]) {
isTrue = true;
}
if(isTrue==true) {
elements[i] = temp[i+1];
}
else {
elements[i] = temp[i];
}
}
正如您从标题中看到的,我想从使用数组实现的 queue 中删除一个元素。
例如,如果我有:
array[]={1,5,4,3,2}
我希望它看起来像这样
array[]={1,4,3,2}
我需要将其从 queue 中删除,但以上只是我正在尝试执行的操作的简单版本。这是可能对您有帮助的其余代码,对我有帮助。
class QueueArray: Queue <InfoTip> {
private:
int capacity; // capacity
int beginning; // index of the first element
int end; // end of the array
InfoTip *elements; // elements of queue
public:
QueueArray(int capacity = 10) : capacity(capacity), // Konstruktor
beginning(-1),end(-1), elements (new InfoTip[capacity]) {}
~QueueArray() { delete [] elements; }
template <typename InfoTip> // putting elements in a queue
void QueueArray<InfoTip>::putInArray(const InfoTip &x) {
end = (end + 1) % capacity;
elements[end] = x;
if (beginning == -1)
beginning= 0;
}
这是我为从 queue 数组
中删除最大元素而编写的函数template <typename InfoTip>
void QueueArray<InfoTip>::deleteMax() {
InfoTip max = 0;
for(int i = 0; i < capacity; i++) {
if(max < elements[i]) {
max = elements[i];
}
}
帮自己一个大忙,像这样使用 STL 容器和算法:
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
std::vector<int> v { 1,2,3,4,5,4,3,2,1 };
v.erase(std::max_element(v.begin(), v.end()));
for (auto x : v)
std::cout << x << " ";
std::cout << "\n";
}
打印:1 2 3 4 4 3 2 1
如果要使用队列
首先你必须创建一个 temp
变量来存储你所有的 element
数据。
for(int i = 0; i < capacity; i++) {
if(max < elements[i]) {
max = elements[i];
}
temp[i] = elements[i];
}
然后你必须创建一个 bool isTrue
变量,因为在找到最大元素后你必须跳过一个元素
bool isTrue = false;
for(int i = 0; i < capacity-1; ++i) {
if(temp[i] == elements[i]) {
isTrue = true;
}
if(isTrue==true) {
elements[i] = temp[i+1];
}
else {
elements[i] = temp[i];
}
}