如何用goto跳到for循环的末尾,但不离开它

How to jump to the end of a for loop, but not leaving it, with goto

本文关键字:不离 离开 goto 何用 跳到 for 循环      更新时间:2023-10-16

我有一个函数,可以查找向量中的所有多个元素。如果我发送{1,2,3,4,5,1,2,3,3,7},它将返回{1,2,3}。我的输入向量大约有100到10000个元素,但我希望只有很少不同的(!)重复;约1-5%。

因此,如果我已经识别出一个元素发生了多次,我会对照我的重复向量进行检查。如果是,则函数应转到下一个元素(如果有)。为此,我使用goto

但我需要在goto label之后有一个命令。否则编译器会抱怨。有什么方法可以避免这种情况并保持goto?我知道我可以使用其他方法,例如相应地设置bool和使用if()。然而,我认为goto方法是直接的。

vector<int> findDublicates(vector<int> const& v) {
    // e.g. {1,2,3,4,5,1,2,3,7} -> {1,2,3}
    vector<int> dublicates;
    for (auto it(v.begin()); it != v.end() - 1;
         ++it) { // go through each element except the last
        for (auto const& i :
             dublicates) { // check if this is already a known dublicate
            if (i == *it)
                goto nextElement; // if so, goto the next element in v
        }
        for (auto it2(it + 1); it2 != v.end();
             ++it2) { // else compare it with the "not checked" elements in v
            if (*it == *it2) { // if a dublicate is found, keep it
                dublicates.emplace_back(*it);
                break; // check the next element in v; could also use goto
                       // nextElement
            }
        }
    nextElement:
        cout << " "; // if I remove cout it won't compile: "expected
                     // primary-expression before '}' token"
    }
    return dublicates;
}

您应该能够使用分号作为no-op。

    nextElement:
    ;
}

然而,我不确定你的方法是否找到重复是有效的。您最好先对数组进行排序,然后迭代一次。对矢量进行排序将把所有重复项组合在一起。然后,您只需检查当前元素是否与前一个元素相同。

如果删除goto不会杀死您,请尝试使用布尔辅助变量:

for(auto it(v.begin()); it!=v.end()-1; ++it) { //go through each element except the last
    bool found = false;
    for(auto const &i : dublicates) { //check if this is already a known dublicate
        if(i==*it)
        {
           found = true;
           break;
         }
    }
    if (!found)
    {
        for(auto it2(it+1); it2!=v.end(); ++it2) { //else compare it with the "not checked" elements in v
            if(*it==*it2) { //if a dublicate is found, keep it
                dublicates.emplace_back(*it);
                break; //check the next element in v; could also use goto nextElement
            }
        }
     }
    cout<<" "; //if I remove cout it won't compile: "expected primary-expression before '}' token"
}

在编程中使用goto被认为是不好的形式。在网上搜索"意大利面条代码goto"。

我建议重新构造代码,使其不使用goto。Gotos可能很有用,但它们不受欢迎,通常可以用更可读的结构来代替。

考虑:

bool isDublicate( int candidate, vector<int> const & dublicates )
{
    for ( auto const &i: dublicates )
        if ( i == candidate ) return true;
    return false;
}
vector<int> findDublicates(vector<int> const &v) {
    //e.g. {1,2,3,4,5,1,2,3,7} -> {1,2,3}
    vector<int> dublicates;
    for(auto it(v.begin()); it!=v.end()-1; ++it) { //go through each element except the last
        if ( isDublicate( *it, dublicates ) )
            continue;
        for(auto it2(it+1); it2!=v.end(); ++it2) { //else compare it with the "not checked" elements in v
            if(*it==*it2) { //if a dublicate is found, keep it
                dublicates.emplace_back(*it);
                break; //check the next element in v; could also use goto nextElement
            }
        }
    }
    return dublicates;
}