如何在每个循环中转到下一个
How to go to next within for each loop
我最近需要修改某人的代码,该代码在 for each
中使用了多个 continue
案例。添加的是 for each
内部的新控制循环,它立即打破了 continue
逻辑。有没有什么好方法可以在这样的循环中获取下一个列表项而不用重写所有的 continue cases?
// 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)
}
}
continue
和 break
进入最内层的控制循环。我在 for
循环中使用了迭代器。因此,根据您的 ListOfItems 是什么(即 SortedList
或 Dictionary
...),您可以迭代而不是 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();
}
我最近需要修改某人的代码,该代码在 for each
中使用了多个 continue
案例。添加的是 for each
内部的新控制循环,它立即打破了 continue
逻辑。有没有什么好方法可以在这样的循环中获取下一个列表项而不用重写所有的 continue cases?
// 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)
}
}
continue
和 break
进入最内层的控制循环。我在 for
循环中使用了迭代器。因此,根据您的 ListOfItems 是什么(即 SortedList
或 Dictionary
...),您可以迭代而不是 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();
}