如何在嵌套的 for 循环中使用类似 continue 语句的东西?
How to use something like a continue statement in nested for loops?
我有 class 个对象,需要将每个对象的一个 属性 与所有其他对象的相同 属性 进行比较。如果它们匹配,则代码需要做一些事情。这导致两次 'for loops' 循环遍历对象以获得 属性,在第二个 'for loop' 中,第三个 'for loop' 遍历 [=32] 的元素=](这是一个向量)来比较它们。如果它们匹配,我需要最外面的 'for loop' 来中止当前迭代并继续进行下一次迭代(我只想考虑与另一个对象的第一次匹配)。
我研究了 'goto' 语句并创建了一个 do{}while() 结构,但无法以某种方式实现它们以获得所需的结果。我需要的是最外层循环的 'continue' 语句,它基于最内层循环中条件语句中发生的情况。
实现此目的的好方法是什么,必须如何实施?
编辑:在我接受的答案旁边,我还推荐 Martin Bonner 的答案,它也工作得很好并且不依赖于 goto。
for (int i = 0; i < max; i++){
Object & object1 = system.getAgent(i);
VectorOfStrings object_property1 = object1.getProperty();
for (int j = i + 1; j < max; j++){
Object & object2 = system.getObject(j);
VectorOfStrings object_property2 = object2.getProperty();
for (unsigned int k = 0; k < object_property1.size(); k++){
if (object_property1[k] == object_property2[k]){
//do something
break; //this aborts the inner most loop
//Additionally, I need the outer most loop to move on one iteration
}
}
}
}
因此,如果 'k' 循环中的条件语句得到满足,我希望 'i' 循环中止当前迭代并继续进行下一个迭代。
另外,由于我是新手,代码可能不够优雅,但请在回答时重点关注这个特定问题!当然,除非对代码进行一般重组可能是问题的解决方案:)
这可以通过 goto
实现:
for (int i = 0; i < max; i++){
Object & object1 = system.getAgent(i);
VectorOfStrings object_property1 = object1.getProperty();
for (int j = i + 1; j < max; j++){
Object & object2 = system.getObject(j);
VectorOfStrings object_property2 = object2.getProperty();
for (unsigned int k = 0; k < object_property1.size(); k++){
if (object_property1[k] == object_property2[k]){
//do something
goto cnt; //this aborts the inner most loop
//Additionally, I need the outer most loop to move on one iteration
}
}
}
cnt:;
}
这是使用 goto
真正简化代码的罕见情况之一。
我强烈推荐@alexeykuzmin0 的方法,但如果你必须在禁止 goto
的环境中工作,那么替代方案(带有烦人的标志)是:
for (int i = 0; i < max; i++){
Object & object1 = system.getAgent(i);
VectorOfStrings object_property1 = object1.getProperty();
bool property_matched = false;
for (int j = i + 1; j < max && !property_matched; j++){
Object & object2 = system.getObject(j);
VectorOfStrings object_property2 = object2.getProperty();
for (unsigned int k = 0; k < object_property1.size(); k++){
if (object_property1[k] == object_property2[k]){
//do something
property_matched = true; // This will break the "j" loop
break; //this aborts the inner most loop
}
}
}
}
您可以使用 lambda 并使用 return 语句来更改控件。如果你 return true lambda 将结束并且 outter "for" 将 "continue".
精简版:
for(;;) {
[&]() {
for(;;) {
if(/*break condition here*/)
return;
}
}();
}
更通用的模板:
for(;;) {
auto innerLoop = [&]() {
for(;;) {
if(/*break condition here*/)
return true;
}
return false;
};
if(innerLoop())
continue;
}
对于这种情况:
for (int i = 0; i < max; i++){
Object & object1 = system.getAgent(i);
VectorOfStrings object_property1 = object1.getProperty();
auto innerLoop = [](){
for (int j = i + 1; j < max; j++){
Object & object2 = system.getObject(j);
VectorOfStrings object_property2 = object2.getProperty();
for (unsigned int k = 0; k < object_property1.size(); k++){
if (object_property1[k] == object_property2[k]){
//do something
return true;
}
}
}
return false;
};
if(innerLoop())
continue;
}
解决这个经典问题的最可读的方法几乎总是将嵌套循环放在一个函数中。我不知道具体代码应该做什么,但这里有一些伪代码,您可以根据以下伪代码来解决问题:
bool keep_going = true;
for (int i = 0; i < max && keep_going; i++)
{
Object& object1 = system.getAgent(i);
keep_going = !compare_objects(object1.getProperty(), i+1, max);
}
其中 compare_objects
是这样的:
inline bool compare_objects (const VectorOfStrings& obj_prop1, size_t begin, size_t end)
{
for(size_t i=begin; i<end; i++)
{
Object& obj2 = system.getObject(j);
VectorOfStrings obj_prop2 = obj2.getProperty();
for size_t j = 0; j < obj_prop1.size(); j++)
{
ifobj_prop1[j] == obj_prop2[j])
{
do_something();
return true;
}
}
}
return false;
}
我有 class 个对象,需要将每个对象的一个 属性 与所有其他对象的相同 属性 进行比较。如果它们匹配,则代码需要做一些事情。这导致两次 'for loops' 循环遍历对象以获得 属性,在第二个 'for loop' 中,第三个 'for loop' 遍历 [=32] 的元素=](这是一个向量)来比较它们。如果它们匹配,我需要最外面的 'for loop' 来中止当前迭代并继续进行下一次迭代(我只想考虑与另一个对象的第一次匹配)。
我研究了 'goto' 语句并创建了一个 do{}while() 结构,但无法以某种方式实现它们以获得所需的结果。我需要的是最外层循环的 'continue' 语句,它基于最内层循环中条件语句中发生的情况。
实现此目的的好方法是什么,必须如何实施?
编辑:在我接受的答案旁边,我还推荐 Martin Bonner 的答案,它也工作得很好并且不依赖于 goto。
for (int i = 0; i < max; i++){
Object & object1 = system.getAgent(i);
VectorOfStrings object_property1 = object1.getProperty();
for (int j = i + 1; j < max; j++){
Object & object2 = system.getObject(j);
VectorOfStrings object_property2 = object2.getProperty();
for (unsigned int k = 0; k < object_property1.size(); k++){
if (object_property1[k] == object_property2[k]){
//do something
break; //this aborts the inner most loop
//Additionally, I need the outer most loop to move on one iteration
}
}
}
}
因此,如果 'k' 循环中的条件语句得到满足,我希望 'i' 循环中止当前迭代并继续进行下一个迭代。
另外,由于我是新手,代码可能不够优雅,但请在回答时重点关注这个特定问题!当然,除非对代码进行一般重组可能是问题的解决方案:)
这可以通过 goto
实现:
for (int i = 0; i < max; i++){
Object & object1 = system.getAgent(i);
VectorOfStrings object_property1 = object1.getProperty();
for (int j = i + 1; j < max; j++){
Object & object2 = system.getObject(j);
VectorOfStrings object_property2 = object2.getProperty();
for (unsigned int k = 0; k < object_property1.size(); k++){
if (object_property1[k] == object_property2[k]){
//do something
goto cnt; //this aborts the inner most loop
//Additionally, I need the outer most loop to move on one iteration
}
}
}
cnt:;
}
这是使用 goto
真正简化代码的罕见情况之一。
我强烈推荐@alexeykuzmin0 的方法,但如果你必须在禁止 goto
的环境中工作,那么替代方案(带有烦人的标志)是:
for (int i = 0; i < max; i++){
Object & object1 = system.getAgent(i);
VectorOfStrings object_property1 = object1.getProperty();
bool property_matched = false;
for (int j = i + 1; j < max && !property_matched; j++){
Object & object2 = system.getObject(j);
VectorOfStrings object_property2 = object2.getProperty();
for (unsigned int k = 0; k < object_property1.size(); k++){
if (object_property1[k] == object_property2[k]){
//do something
property_matched = true; // This will break the "j" loop
break; //this aborts the inner most loop
}
}
}
}
您可以使用 lambda 并使用 return 语句来更改控件。如果你 return true lambda 将结束并且 outter "for" 将 "continue".
精简版:
for(;;) {
[&]() {
for(;;) {
if(/*break condition here*/)
return;
}
}();
}
更通用的模板:
for(;;) {
auto innerLoop = [&]() {
for(;;) {
if(/*break condition here*/)
return true;
}
return false;
};
if(innerLoop())
continue;
}
对于这种情况:
for (int i = 0; i < max; i++){
Object & object1 = system.getAgent(i);
VectorOfStrings object_property1 = object1.getProperty();
auto innerLoop = [](){
for (int j = i + 1; j < max; j++){
Object & object2 = system.getObject(j);
VectorOfStrings object_property2 = object2.getProperty();
for (unsigned int k = 0; k < object_property1.size(); k++){
if (object_property1[k] == object_property2[k]){
//do something
return true;
}
}
}
return false;
};
if(innerLoop())
continue;
}
解决这个经典问题的最可读的方法几乎总是将嵌套循环放在一个函数中。我不知道具体代码应该做什么,但这里有一些伪代码,您可以根据以下伪代码来解决问题:
bool keep_going = true;
for (int i = 0; i < max && keep_going; i++)
{
Object& object1 = system.getAgent(i);
keep_going = !compare_objects(object1.getProperty(), i+1, max);
}
其中 compare_objects
是这样的:
inline bool compare_objects (const VectorOfStrings& obj_prop1, size_t begin, size_t end)
{
for(size_t i=begin; i<end; i++)
{
Object& obj2 = system.getObject(j);
VectorOfStrings obj_prop2 = obj2.getProperty();
for size_t j = 0; j < obj_prop1.size(); j++)
{
ifobj_prop1[j] == obj_prop2[j])
{
do_something();
return true;
}
}
}
return false;
}