far*_*zki 13 c++ conditional loops continue nested-loops
我有一类对象,需要将每个对象的一个属性与所有其他对象的相同属性进行比较.如果匹配,代码需要做一些事情.这导致两个'for循环'循环遍历对象以获取该属性,而在第二个'for循环'中,有第三个'for循环'遍历属性的元素(这是一个向量)来比较它们.如果它们匹配,我需要最外面的'for循环'来中止当前迭代并继续下一个(我只希望第一个匹配另一个对象被考虑).
我已经研究了'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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,如果满足'k'循环中的条件语句,我希望'i'循环中止当前迭代并继续下一个循环.
此外,由于我是新手,代码可能不够优雅,但请关注您答案中的这个特定问题!除非代码的一般重组当然可能是问题的解决方案:)
ale*_*in0 23
这可以用以下方式实现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:;
}
Run Code Online (Sandbox Code Playgroud)
这是使用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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 lambda 并使用 return 语句来更改控件。如果您返回 true,则 lambda 将结束,而外部“for”将“继续”。
精简版:
for(;;) {
[&]() {
for(;;) {
if(/*break condition here*/)
return;
}
}();
}
Run Code Online (Sandbox Code Playgroud)
一个更通用的模板:
for(;;) {
auto innerLoop = [&]() {
for(;;) {
if(/*break condition here*/)
return true;
}
return false;
};
if(innerLoop())
continue;
}
Run Code Online (Sandbox Code Playgroud)
对于这种情况:
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;
}
Run Code Online (Sandbox Code Playgroud)
解决这个经典问题最可读的方法几乎总是将嵌套循环放在一个函数中.我不知道具体代码是做什么的,但是这里有一些伪代码可以基于以下解决方案:
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);
}
Run Code Online (Sandbox Code Playgroud)
这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;
}
Run Code Online (Sandbox Code Playgroud)