在队列循环中找到最小值
find least among the loop of queues
我有一个队列的循环(n
队列的数量),我想搜索所有队列的大小并找到最小大小的队列。
我刚想到一个逻辑
std::queue<int> q;
/* fill queue ... */
int min_value = INT_MAX;
std::size_t size = q.size();
for( q=2; q=n; q++){ // from the second queue to the end queue
if
min_value=min.size() > q.size()?
q.size()=min_value
这个逻辑是正确的吗,我不确定,有人能帮我吗?
编辑: i tried to figure out
std::queue<int> q;
/* fill queue ... */
int min_value = INT_MAX;
std::size_t size = q.size();
for( q=0; q<n; q++){ // given loop of queues
if
(q.size()<min_value) // q.size() is compared with the min_value (limits MAX)
min_value=q.size(); // any value of my q.size() which is less than INT_MAX will initially be declared the minimum value. On subsequent iterations this value is refined -- if a smaller value is found that's used for future iterations. at the end of loop, i will get the least value.
这个逻辑正确吗?
您有几个错误:
-
数组在包括
c++
在内的大多数语言中都是零索引的,所以循环应该是:for( q=0; q<n; q++){
注意:你的条件
q=n
是完全没有意义的,将导致无限循环。 -
在循环中使用
min_value
而不是min.size
,这没有意义。 -
在周期内和之前通过索引访问队列。我建议您将队列保持在矢量中,因此这将是:
std::vector<std::queue<int> > q;
使用
q[i].size()
访问给定队列的大小。相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用递归的数组的最小值.这是怎么回事
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 带自定义比较器的最小优先级队列
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- txt 文件中浮点数的最大和最小值
- 余数运算符的等效操作,用于处理低于允许的最小值
- 需要使用模板查找数组的第二个最小和最小值
- CUDA - 将 float3 数组的 (x,y,z) 分量的最小值/最大值分开?
- 查找包含 N 个元素的数组的最小值和最大值
- 错误C++在Visual Studio 2019中使用numeric_limits的长双精度最小值/最大值
- 是否存在用于 C 或 C++ 中常见数学运算(例如最小值、最大值和平均值)的可导入库?
- 如何在 c++ 中创建对的优先级队列.这会弹出具有最小值的元素.默认的弹出最大值
- 如何获取队列的最小值和最大值
- 找到连续的最小值并在队列的向量中进行比较
- 在队列循环中找到最小值