Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用反向迭代器从“std::set”中删除元素?_C++_Stl_C++14 - Fatal编程技术网

C++ 如何使用反向迭代器从“std::set”中删除元素?

C++ 如何使用反向迭代器从“std::set”中删除元素?,c++,stl,c++14,C++,Stl,C++14,面临删除集合中最后一个元素的问题: #include <bits/stdc++.h> using namespace std; int main() { set < pair <int,int > > a; a.insert(make_pair(2,3)); auto it = a.rbegin(); a.erase(it.base()); // for deleting last element in set cout << a.size()

面临删除集合中最后一个元素的问题:

#include <bits/stdc++.h>
using namespace std;
int main()
{
set < pair <int,int > > a;
a.insert(make_pair(2,3)); 
auto it = a.rbegin();
a.erase(it.base()); // for deleting last element in set
cout << a.size() << endl;
return 0; 
}
#包括
使用名称空间std;
int main()
{
设置a;
a、 插入(制作成对(2,3));
自动it=a.rbegin();
a、 erase(it.base());//用于删除集合中的最后一个元素
库特
有没有其他方法可以从集合中删除元素


你能告诉我,我的代码有什么问题吗

itr.base()
其中
itr==a.begin()
相当于
a.end()
。请参阅:

删除结束迭代器之后的内容是未定义的行为

有没有其他方法可以从集合中删除元素


你能告诉我,我的代码有什么问题吗

itr.base()
其中
itr==a.begin()
相当于
a.end()
。请参阅:


删除过期迭代器是未定义的行为

谢谢,但是你能告诉我,我的代码有什么问题吗?我告诉过你-你的
.base()
调用基本上返回
a.end()
。然后你删除过期迭代器,这是未定义的行为。好的,我知道了,但如果我直接通过,“a.erase(it)”然后也不起作用了?即使我正在传递一个需要删除的元素的引用。谢谢,但是你能告诉我,我的代码有什么问题吗?我告诉过你-你的
.base()
调用基本上返回
a.end()
。然后,您将删除一个超过结束迭代器的元素,这是未定义的行为。好的,我知道了,但如果我直接传递“a.erase(it)”,那么也不起作用?即使我传递了需要删除的元素的引用。可能重复的可能重复的
a.erase(std::prev(std::end(a)));