在队列循环中找到最小值

find least among the loop of queues

本文关键字:最小值 队列 循环      更新时间:2023-10-16

我有一个队列的循环(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.

这个逻辑正确吗?

您有几个错误:

  1. 数组在包括c++在内的大多数语言中都是零索引的,所以循环应该是:

    for( q=0; q<n; q++){
    

    注意:你的条件q=n是完全没有意义的,将导致无限循环。

  2. 在循环中使用min_value而不是min.size,这没有意义。

  3. 在周期内和之前通过索引访问队列。我建议您将队列保持在矢量中,因此这将是:

    std::vector<std::queue<int> > q;
    
  4. 使用q[i].size()访问给定队列的大小。