【问题标题】:How to go to next within for each loop如何在每个循环中转到下一个
【发布时间】:2018-04-04 19:31:39
【问题描述】:

我最近需要修改某人的代码,该代码在 for each 中使用了多个 continue 案例。添加的是for each 内部的新控制循环,它立即破坏了continue 逻辑。有没有一种好方法可以在这样的循环中获取下一个列表项,而无需重写所有的 continue 案例?

// Additional control loops within the member function which cannot be 
// turned into functions due to native C++ data types.
{
    for each(KeyValuePair<String^,String^> kvp in ListOfItems) {
        do { // new condition testing code
           // a bunch of code that includes several chances to continue
        } while (!reachedCondition)
    }
}

【问题讨论】:

标签: foreach c++-cli next continue


【解决方案1】:

continuebreak 转到最里面的控制循环。我在for 循环中使用了迭代器。因此,根据您的 ListOfItems 是什么(即 SortedListDictionary ...),您可能能够迭代而不是 continue

int i=0;
Dictionary^ d = gcnew Dictionary<string, string>();

for (IEnumerator<KeyValuePair<string, string>>^ e =  d->GetEnumerator();i< d->Count;i++) {
    // do stuff
    e->MoveNext();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-27
    • 2012-06-02
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多