如何检查队列是否使用 STL 排序
how to check if queue is sorted using stl
我想请您帮助解决我遇到的这个问题,我需要编写一个C++函数,该函数将队列作为参数输入并检查队列中的内容是否按排序顺序排列(使得前面元素最小(。应相应地返回BOOLEAN
值。假设队列中没有重复的元素。
我正在尝试了解排序的概念,因此任何帮助将不胜感激,这是我到目前为止尝试过的:
#include "stdafx.h"
#include <iostream>
#include <queue>
using namespace std;
bool is_Sorted(queue<int> q) {
int my_front = q.front();
int my_back = q.back();
if (my_front==my_back) {
return true;
}
if (my_front+1>my_front) {
return true;
}
}
int main()
{
queue <int> q;
q.push(3);
q.push(4);
q.push(5);
q.push(6);
q.push(7);
is_Sorted(q);
return 0;
}
因为queue
不提供不能使用的迭代器:is_sorted
因此,比较需要复制queue
或顺序pop
和比较queue
的元素,然后push
将它们放回queue
。我选择简单地复制此示例的queue
:
template <typename T>
bool is_sorted(queue<T> q) {
if(!empty(q)) {
for(T i = q.front(); size(q) > 1U; i = q.front()) {
q.pop();
if(i > q.front()) {
return false;
}
}
}
return true;
}
这个例子显然会产生复制queue
的成本,这显然是不可取的。产生此成本是因为queue
是作业的错误工具。考虑priority_queue
或只是一个vector
。
相关文章:
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查函数返回类型是否与STL容器类型值相同
- glibcxx STL 在实现 std::valarray::sum() 时是否不正确?
- 如何检查C ++ STL列表是否为回文?
- 是否有具有外部元素分配的序列容器(在 STL 中)?
- STL 是否为其标头指定标头保护?
- 擦除是否删除 stl 无序列图元素使用的堆内存
- 是否有一种 STL 算法可以最后找到,但它也适用于指针?
- C++/STL 我应该使用哪种算法来检查容器是否有重复项?
- 是否有一个类似STL的函数来用索引的某个函数填充数组
- 如何检查队列是否使用 STL 排序
- STL 库在不同平台上是否不同?
- 是否有任何常规方法可以通知 STL 移动和复制构造函数?
- STL功能用于确定距离是否在n`中
- 是否可以在C STL中获得集合的单个元素
- 什么 STL 算法可以确定容器中的一个项目是否满足谓词?
- 将对象(如 STL 对象)传入和传出静态库是否安全
- 如果其他人在等待,是否有标准的STL或QT方法可以产生互惠码,否则请保留它
- 使用 stl 迭代器封装向量是否很好?如果是?怎么可能呢?
- STL 是否有办法在调用小于之前应用函数