插入。这会将元素插入集合中
删除。这将从集合中删除该元素(如果存在)
搜索。这将在集合中搜索元素,如果存在则显示“是”,否则显示“否”。
因此,如果输入类似于 n = 7,则查询 = [[1,5 ],[1,8],[1,3],[2,8],[1,9],[3,8],[3,3]],则输出将为 [no, yes],因为集合中不存在 8,而存在 3。
为了解决这个问题,我们将按照以下步骤操作 -
定义一个集合 s定义一组迭代器“it”来迭代 sq := 查询数当 q 不为零时,在每次迭代后减少 q,执行以下操作: 取查询类型qt为qt如果qt为1,则插入x s从区块中出来如果qt为2,则从s中删除x从区块中出来如果 qt 为 3,在 it 内调用 find(x)如果它与s 的最后一个元素,则:打印 no否则打印 yes从街区中出来示例让我们看看以下实现,以便更好地理解 -
#include <iostream>#include <set>using namespace std;int main(){ set<int> s; set<int>::iterator it; int q,x; int qt; cin >> q; while(q--){ cin>>qt>>x; switch(qt){ case 1:s.insert(x); break; case 2:s.erase(x); break; case 3:it=s.find(x); if(it==s.end()) cout<<"no"<<endl; else cout<<"yes"<<endl; break; } } return 0;}
输入71 51 81 32 81 93 83 3
输出noyes
以上就是c++程序用于在set stl中插入、删除和查找的详细内容。
