【发布时间】:2012-07-06 13:32:43
【问题描述】:
变体 a:
const auto end = whatever.end();
for (auto it = whatever.begin(); it != end; ++it)
{
// ...
}
变体 b:
const auto end = whatever.cend(); // note the call to cend insteand of end here
for (auto it = whatever.begin(); it != end; ++it)
{
// ...
}
是否有任何理由相信变体 b 的效率会低于变体 a,因为循环条件会比较两种不同类型的迭代器?这会导致it 上的隐式转换吗?
(end 在 for 循环中被多次使用,因此我想把它吊出来。)
【问题讨论】:
-
@David 在我的具体情况下,它是
std::string,但总的来说我很好奇。
标签: c++ performance c++11 iterator constants