在最小大小队列中入队 [英] Enqueue in a minimum size queue

查看:151
本文介绍了在最小大小队列中入队的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个范例,每个线程有一个队列。我希望找到最小尺寸队列,在队列的数量,我用下面的代码。

I have a paradigm which has one queue per thread. I wish to find the minimum size queue, among the number of queues, which i did with the following code.

std::vector<std::queue<task> > q;

int min_value = INT_MAX;
std::size_t size = q.size();
for( i=1; i<size; i++){ //accessing loop of queues
  if(min_value > q[i].size())
    min_value = q[i].size();
}

现在我想在这里做一个额外的操作,每次只有最小大小队列(从上面的代码中找到)应该排队任务。

now i wish to do a additional operation in this, every time only the minimum size queue (found from the above code) should enqueue the task.

q.get (min_value)
q.push(task) // will this one, does the required operation?


推荐答案

我认为你要找的是最小队列在向量 q 中。如果是,你可以这样做:

I think what you're looking for is the smallest queue in the vector q. If so, you can do this:

auto cmp = [](std::queue<task> const & a, std::queue<task> const & b) {  
               return a.size() < b.size(); 
           };

//note q is a std::vector of std::queue
auto min_queue = std::min_element(q.begin(), q.end(), cmp);

min_queue->push(item);//min_queue is the iterator to the smallest queue


b $ b

另外,我相信你的代码, i = 1 是一个错误,我认为应该 i = 0 如果你想搜索整个向量,即从开始到结束。如果你真的是指 i = 1 ,那么你必须这样做:

Also, I believe in your code, i=1 is a mistake, which I think should be i=0 if you want to search the entire vector, i.e from begin to end. If you really mean i=1, then you have to do this:

auto min_queue = std::min_element(q.begin() + 1, q.end(), cmp);

希望有帮助。

这篇关于在最小大小队列中入队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆