每个循环如何进入下一个

时间:2018-04-04 19:31:40

标签: foreach c++-cli next continue

我最近需要修改在continue中使用多个for each个案的某人的代码。添加是for each内部的新控制循环,它立即打破了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)
    }
}

1 个答案:

答案 0 :(得分: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();
}