【发布时间】:2013-03-11 12:46:26
【问题描述】:
我在向量中有一个队列循环,我需要在队列循环中搜索以找到最小索引队列和最大大小队列。
我正在使用以下代码
int min_index = 0;
int max_size = -1;
std::size_t size = q.size();
for( i=0; i<size; i++){ //accessing loop of queues
if(q[min_index].size() > q[i].size())
min_index = i; // Now q[min_index] is min_index is the minimum size queue
if(q[i].size() > max_size)
max_size = q[i].size(); // maximum size queue
}
我有点怀疑是否对每个if statement 使用{},如下面的代码
int min_index = 0;
int max_size = -1;
std::size_t size = q.size();
for( i=0; i<size; i++){ //accessing loop of queues
if(q[min_index].size() > q[i].size()){
min_index = i; // Now q[min_index] is the shortest queue
}
if(q[i].size() > max_size){
max_size = q[i].size(); // longest queue
}
}
哪个是正确的,有和没有{} 会有什么区别。对不起,如果这是一个愚蠢的问题。我是编程新手。
【问题讨论】:
-
没有“if 循环”这样的东西。如果一个块中只有一个语句,
{}是可选的,这是样式问题。 -
对不起,我把它改成了'if语句'
标签: c++ loops if-statement for-loop