Best way to get the index of an iterator
http://stackoverflow.com/questions/2152986/best-way-to-get-the-index-of-an-iterator
it - vec.begin()
std::distance(vec.begin(), it)
it - vec.begin()
takes constant time, but the operator -
is only defined on random access iterators, so the code won't compile at all with list iterators, for example.
std::distance(vec.begin(), it)
works for all iterator types, but will only be a constant-time operation if used on random access iterators.
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步