有没有其他选择可以打破?
Is there another alternative to break?
我对中断指令有问题;
事实上,就我而言,我在下面的计算代码示例中重现了我使用两个嵌套的 for 循环和 if 循环。
我希望当open_bound变量= 0时,完全退出循环,从而显示时间t的值。执行后,我看到显示时间 t = 0 而不是 3,我很难理解为什么。你能开导我吗?
有没有其他选择可以打破?(我不能使用 goto,而且我在真实代码中并行化这部分(
提前谢谢你
#include <iostream>
#include <vector>
using namespace std;
int main () {
int numtracers = 1000;
int save_t;
double t;
int open_bound = 0;
int tau = 5;
double int_step = 0.25;
for (int i = 0; i < numtracers; i++) {
// Variable to overwrite the successive positions of each particle
vector <double> coord(2);
coord[0] = 0.1;
coord[1] = 0.2;
int result_checkin;
for(t=0; t<tau-1; t+=int_step) {
save_t = t;
// Function to check if coordinates are inside the domain well defined
// result_checkin = check_out(coord);
if (t == tau-2) result_checkin = 1;
if (result_checkin == 1) { // Particle goes outside domain
if (open_bound == 0) {
break;
}
else {
coord[0]+=0.1;
coord[1]+=0.1;
}
}
else {
coord[0]+=0.1;
coord[1]+=0.1;
}
}
}
cout << save_t << endl;
return 0;
}
好的,让我们首先回顾一下break
语句的作用(不包括它在switch
块中的使用(:它"打破"了最里面的封闭for
、while
或do ... while
循环。因此,这里不考虑if
语句 - 它们也不是真正的循环,是吗。
因此,在您的主代码中,您实际上只有两个循环。你自己的break
将退出最里面,立即跳到我在下面的代码中突出显示的点。添加额外的if ... break;
代码,就像我所做的那样,将退出外部循环:
for (int i = 0; i < numtracers; i++) {
int open_bound = 0; // MUST HAVE HERE to parallelize this loop!
// Variable to overwrite the successive positions of each particle
vector <double> coord(2);
coord[0] = 0.1;
coord[1] = 0.2;
int result_checkin;
for(t=0; t<tau-1; t+=int_step) {
save_t = t;
// Function to check if coordinates are inside the domain well defined
// result_checkin = check_out(coord);
if (t == tau-2) result_checkin = 1;
if (result_checkin == 1) { // Particle goes outside domain
if (open_bound == 0) {
break; // Exits the inner for loop and goes to the "-->HERE" line!
}
else {
coord[0]+=0.1;
coord[1]+=0.1;
}
}
else {
coord[0]+=0.1;
coord[1]+=0.1;
}
}
// Your "break" exits the for loop and execution continues -->HERE
if (open_bound == 0) break; // This will (always) exit the outer loop!
}
这有帮助吗?请随时要求进一步解释!
编辑 - 关于循环并行化的说明:如果要并行化 外循环,那么只有当你在该外循环内移动
open_bound
的声明/定义时,你才能这样做(正如我 在上面的代码中完成了(!如果您正在尝试,则无法并行化 修改和测试在循环之外声明的标量变量 范围。
退出所有想要的循环的替代方法是使用 bool 标志来决定何时强制循环终止。当您点击open_bound=0
时,您可以先将标志设置为 false,然后中断。
检查以下内容以了解我的意思:
bool go = true;
for (int i = 0; go && CONDITION1; i++)
for (int j = 0; go && CONDITION2; j++)
for (int k = 0; go && CONDITION3; k++)
....
if(open_bound==0){
go = false;
break;
}
您的代码的工作版本在这里
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 如何使用默认参数等选择模板专业化
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 为什么这个选择排序算法仍然切换一个元素,当它已经是其他元素中最小的元素时?
- cv::cuda::setTo()是否有其他选择
- 有没有其他选择可以打破?
- 除了使用 #define 进行条件编译之外,还有其他选择吗?
- 谷神星求解器:残差函子使用的可变对象是否良好实践?还有什么其他选择
- 在C 中,Haskell下划线有其他选择吗?
- 有没有机会触发一个不知道拥有队列的kevent?还有其他选择吗?
- 除了使用OpenCV的imdecode之外,还有其他选择吗?太慢了
- C++ 如何通过可选的typedef,使用SFINAE或其他方式选择类
- 从一个组合框中选择项目,然后从其他组合框中删除该项目
- 如何存储需要随机选择和删除的数据,矢量,哈希表或其他
- 除了制作类的const版本,还有其他选择吗
- 为什么glReadPixels这么慢,有没有其他选择?
- 除了这一长串"if argA == argB do ..."之外,还有其他选择吗?
- 从下拉框中选择其他项目时更改选项卡小部件的 currentIndex()
- Objective-c选择器,其他语言如c++, python, ruby, java, javascript有类似的东
- 是否有可能在C中使用系统api启动一个kill命令?如果没有其他选择的话