基于条件的循环选择。

if(valid)
for (std::multimap<int,int>::reverse_iterator rit=id_count.rbegin(); mcount<10 && rit!=id_count.rend();++rit)
else
for (std::multimap<int,int>::iterator rit=id_match.begin(); mcount<10 && rit!=id_match.end();++rit)

{
    //this is common for both for loop
}

如何用C++实现呢?

最佳答案

您别无选择,只能将通用部分放入函数中,大致如下所示:

void somefunction(...)
{
    //this is common for both for loops
}

if (valid)
{
  for (std::multimap<int,int>::reverse_iterator rit=id_count.rbegin(); mcount<10 && rit!=id_count.rend();++rit)
    somefunctiuon(...);
}
else
{
  for (std::multimap<int,int>::iterator rit=id_match.begin(); mcount<10 && rit!=id_match.end();++rit)
    somefunctiuon(...);
}

09-11 19:19