2013-02-20 119 views
1

我有一个范例,在向量的队列循环中,如果条件对于第i个队列为真,则将该第i个队列的队列大小增加5。在这个操作之后,我需要搜索所有队列的队列大小,并在最短队列中排队。 我想要做的事,如下面的代码增加队列大小并找到最短队列

#include <vector> 
#include <queue> 
int min_index = 0; 
std::vector<std::queue<int> > q 
std::size_t size = q.size(); 
for(i=0; i<size; i++){ 
    if(..) {// A condition is true 
//increase the size of the ith queue by 5 more times 
} 

if(q[min_index].size() > q[i].size()) 
     min_index = i; // Now q[min_index] is the shortest queue 
} 
q[min_index].push(int) 
} 

给出如何人为地增加了队列的大小,如果条件是真的吗?然后搜索队列并找到最短队列。

修订

#include <vector> 
#include <deque> 
int min_index = 0; 
std::vector<std::deque<int> > q 
std::size_t size = q.size(); 
for(i=0; i<size; i++){ 
    if(...) {// A condition is true 
    q[i].resize(q[i].size() + 5) 
} 
if(q[min_index].size() > q[i].size()) 
     min_index = i; // Now q[min_index] is the shortest queue 
} 
q[min_index].push(int) 
} 
+0

“如何人为增加队列大小”?我希望你的意思是支持存储队列可以保存数据,但目前没有,因为除此之外,你将获得的唯一“大小”增加是项目占用,即,即。将垃圾推入队列。 – WhozCraig 2013-02-20 16:07:06

+0

@WhozCraigSo循环'if(..){//条件为真(int j = 0; j <5; j ++)q [i] .push(0);'会做什么? – billa 2013-02-20 16:15:09

回答

3

“增加队列大小”并不清楚你的意思。

如果你的意思是'增加队列容量',你不需要。队列的默认底层容器是deque,它不是内存中的连续块,因此在扩展时不会遇到任何问题,因此无需事先删除reserve()。有关详细信息,请参阅here

所以,一个队列的大小就是它中的项目数量。如果要增加,deque有一个resize()函数,它将所有新项目的指定值作为参数,否则只是对它们进行值初始化。

+0

@ J20正如你所说,德克将是一个不错的选择,所以上面我更新了问题的答案,请检查是否可以这样做。我使用'deques'而不是'queues'的向量,并使用'resize()'来增加第i个deque的大小。然后我搜索一个最短的deque,并在它入队 – billa 2013-02-21 13:41:30

+0

挂起,我认为你误解了我写的东西。 'queue'不是一个容器 - 它是一个容器适配器(一种容器周围的包装),可以使用几种不同的容器类型来实现其内部。除非您在创建时指定了不同的基础容器类型,否则您的队列*是底层的* deque。所以你不需要改变任何东西。 – jam 2013-02-21 13:46:09

+0

@ J20其实我需要在找到最短队列之前增加第i个队列的大小(不是容量)(因为这个范例与线程的多重本地队列中的任务调度有关)。所以我需要如果一个条件是真的增加ith队列的大小和搜索最短的队列。 – billa 2013-02-21 13:57:04

1

在这里是这样做的一个可能的方式(假定C++ 11是一个选项,否则例如容易改写而不lambdas和auto):

#include <vector> 
#include <queue> 
#include <algorithm> 

// Don't use this in your real code: I use it here just for convenience. 
// It is a bad programming practice to import a whole namespace (especially 
// if it is a Standard Library namespace). 
using namespace std; 

// The function that defines your condition on each queue 
bool cond_on_q(queue<int> const& q) 
{ 
    bool satisfied = false; 
    // Determine whether the condition is satisfied... 
    return satisfied; 
} 

int main() 
{ 
    vector<queue<int>> v = ...; // Initialize your vector of queues somehow 

    // Iterate over the vector of queues 
    for (auto& q : v) 
    { 
     if (cond_on_q(q)) // Is the condition satisfied? 
     { 
      // Insert 5 elements with any valid value (for instance, 0) 
      for (int i = 0; i < 5; i++) (q.push(0)); 
     } 
    } 

    // Determine the queue with the minimum size. 
    auto i = min_element(begin(v), end(v), 
     [] (queue<int> const& q1, queue<int> const& q2) { 
      return q1.size() < q2.size(); 
      } 
     ); 

    int newValue = ...; // Initialize the value to be enqueued somehow.   

    // Add the value to the queue with minimum size. 
    i->push(newValue); 
}