2013-02-20 50 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); 
}