为什么这行关于使用 erase() 到 vector 的代码会出错?[复制]

Why does this line of code that about the usage of erase() to vector give an error? [duplicate]

提问人:gz_zg 提问时间:11/6/2023 更新时间:11/6/2023 访问量:75

问:

#include<iostream>
#include<vector>
using namespace std;

int main()
{
    vector<int> a = { 1,2,3,4,5 };
    a.erase(a.begin());  //This line of code is correct
    a.erase(a.rbegin());  //This line of code is wrong(I want to remove the last element in the vector)

    return 0;
}

我尝试将其换成以下代码行:

a.erase(a.end()-1);  //This line of code is also correct.

为什么?

它们两个(a.rbegin()和a.end()-1)不是同一个迭代器吗?

C++ STL

评论

5赞 molbdnilo 11/6/2023
它们甚至没有相同的类型。删除最后一个元素的拼写为 。pop_back
2赞 Useless 11/6/2023
您可以阅读此处的文档,并看到 和 是不同的类型,并且没有采用反向迭代器的重载。iteratorreverse_iteratorerase

答: 暂无答案