翻譯你的邏輯成代碼期正視的最文學的方式是這樣的:
std::vector<AggregatedQuoteType>::iterator OrderBook::find_price(PriceType price)
{
std::vector<AggregatedQuoteType>::iterator i = v_BuyOrders.begin();
do
{
for (; i != v_BuyOrders.end(); ++i)
if(i->get_price() >= price)
break;
return i; // i.e. end()
} while (!another_function(*i));
return i;
}
它簡化爲:
std::vector<AggregatedQuoteType>::iterator OrderBook::find_price(PriceType price)
{
std::vector<AggregatedQuoteType>::iterator i;
for (i = v_BuyOrders.begin(); i != v_BuyOrders.end(); ++i)
if(i->get_price() >= price && another_function(*i))
break;
return i;
}
或者,如果你想使用標準算法更聲明式的方法:
#include <algorithm>
...
std::vector<AggregatedQuoteType>::iterator OrderBook::find_price(PriceType price)
{
return std::find_if(v_BuyOrders.begin(), v_BuyOrders.end(),
[](const AggregatedQuoteType& x)
{ return x.get_price() >= price && another_function(x); });
}
那麼,它可能是*另一個循環沒有初始化部分,因爲你會使用我您保存的計算器。 – jsantander
爲什麼不'if(i-> get_price()> = price && some_other_condition(i))break;'? –
你能提出一個建議嗎? –