查找特定项目最后一次出现的位置的最佳方法是什么?

What is the best way to find the position of the last occurrence for a specific item?

类似这样但相反:

int pos = find(v.begin(), v.end(), item) - v.begin();

您可以使用 std::find 但请使用容器提供的反向迭代器:

auto it = std::find(v.rbegin(), v.rend(), item);
int index = v.rend() - it + 1;

您需要 +1 因为 v.rend() "points" 到元素 -1,即 "past" 第一个元素。

使用reverse iterators:

find(v.rbegin(), v.rend(), item);