我有一个队列的循环(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()
访问给定队列的大小。