在c ++中使用erase-remove惯用语时,我应该通过引用传递吗?
例如:
void Country::clean()
{
cities.erase( std::remove_if(
cities.begin(),
cities.end(),
[](City city) -> bool { return city.getNumberOfBuildings() == 0; }
),
cities.end()
);
}
Run Code Online (Sandbox Code Playgroud)
也许最好将lambda函数行更改为:
[](City &city) -> bool { return city.getNumberOfBuildings() == 0; }
Run Code Online (Sandbox Code Playgroud)
并通过参考传递城市?
谢谢
制作副本没有任何好处,因此您应该传递参考.但你应该做的是使用一个const参考:
[](const City &city) -> bool { return city.getNumberOfBuildings() == 0; }
Run Code Online (Sandbox Code Playgroud)
请注意,在这种情况下,您不必指定返回类型.
| 归档时间: |
|
| 查看次数: |
90 次 |
| 最近记录: |