无法从libstdc++;基于策略的数据结构 C++中有一个关联容器,它实际上是一个集合(多集合),它可以给出元素的顺序。
以下是我如何使用容器:无法从libstdc++;基于策略的数据结构 C++中有一个关联容器,它实际上是一个集合(多集合),它可以给出元素的顺序。,c++,stl,containers,libstdc++,C++,Stl,Containers,Libstdc++,以下是我如何使用容器: #包括 #包括 使用名称空间std; 使用名称空间_gnu_pbd; 模板 使用有序_multiset=树; 问题是,我无法从中删除元素: 有序多集; s、 插入(0); s、 擦除(0); 库特 是什么导致了问题 您尚未提供到树的路径。你的程序格式不正确 我怎样才能解决这个问题 使用std::multiset和 template<typename Set, typename Key> size_t order_of_key(const Set &
#包括
#包括
使用名称空间std;
使用名称空间_gnu_pbd;
模板
使用有序_multiset=树;
问题是,我无法从中删除元素:
有序多集;
s、 插入(0);
s、 擦除(0);
库特
是什么导致了问题
您尚未提供到树的路径。你的程序格式不正确
我怎样才能解决这个问题
使用std::multiset
和
template<typename Set, typename Key>
size_t order_of_key(const Set & set, const Key & key)
{
return std::distance(set.begin(), set.lower_bound(key));
}
模板
密钥的大小和顺序(常数集和集合、常数键和键)
{
return std::distance(set.begin(),set.lower_bound(key));
}
使用std::less_equal
,无法知道两个元素是否相等std::set
使用表达式!公司(a、b)和&!comp(b,a)
确定a
和b
是否等效。如果使用类似的std::less
,则此功能有效,但在使用std::less_equal
时失败<代码>5和5是等效的,但是!(5我知道这篇文章已经有一年多的历史了,但我一直在努力解决同样的问题,我认为我找到了一个不那么优雅但很有用的解决方案
您可以使自己的擦除功能如下:
void myerase(ordered_set &t, int v){
int rank = t.order_of_key(v);//Number of elements that are less or equal to v in t
ordered_set::iterator it = t.find_by_order(rank); //Iterator that points to the (rank)th element in t
t.erase(it);
}
由于按键的顺序、按顺序查找和标准的擦除都是O(log(N))
,myerase也应该是O(log(N))
。如果我错了,有人纠正我
下面是我用来测试它的代码片段:
#include<iostream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
void myerase(ordered_set &t, int v){
int rank = t.order_of_key(v);//Number of elements that are less or equal to v in t
ordered_set::iterator it = t.find_by_order(rank); //Iterator that points to the (rank)th element in t
t.erase(it);
}
void printOrderedSet(ordered_set s){
//Function to show the contents of the set
for(auto it=s.begin(); it!=s.end(); it++){
cout<<*it<<" ";
}cout<<endl;
}
int main(){
//Create an ordered_set t with the numbers 0,0,1,1,2,2,3,3,4,4
ordered_set t;
for(int i=0; i<10;i++){
t.insert(i/2);
}
printOrderedSet(t); //output: 0 0 1 1 2 2 3 3 4 4
myerase(t, 3);
printOrderedSet(t); //output: 0 0 1 1 2 2 3 4 4
myerase(t, 3);
printOrderedSet(t); //output: 0 0 1 1 2 2 4 4
myerase(t, 0);
printOrderedSet(t); //output: 0 1 1 2 2 4 4
myerase(t, 1);
printOrderedSet(t); //output: 0 1 2 2 4 4
}
#包括
#包括
#包括
使用名称空间_gnu_pbd;
使用名称空间std;
typedef树有序集合;
无效髓鞘酶(有序集&t,int v){
int rank=t.order_of_key(v);//t中小于或等于v的元素数
有序_集::迭代器it=t.find_by_order(rank);//指向t中第(rank)个元素的迭代器
t、 抹去(它);
}
无效printOrderedSet(有序集合){
//函数来显示集合的内容
对于(自动it=s.begin();it!=s.end();it++){
为什么不使用<代码> STD::多集?而不是使用命名空间STD来依赖<代码>;使用命名空间αGNUUPBDS;,您所谈论的命名空间是明确的。<代码> OrdEdEuleSuth不是C++的一部分,但是对于一些非通用扩展I,无法找到代码< O(1)的声明的位置。
来自键的顺序,但我非常怀疑它,因为查找是O(log(n))
找不到文档是不使用此库的另一个原因,相反,使用标准librarylinear仍然比未实现好,这比未实现好如果您的意思是std::multiset
,那么我不能使用multiset,因为我需要键的order\u函数n多集容器。std::distance
在O(n)中,n=两个元素的距离。要使第一个元素大于搜索的元素,它实际上必须是上限吗?这方面的问题有点不清楚。
#include<iostream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
void myerase(ordered_set &t, int v){
int rank = t.order_of_key(v);//Number of elements that are less or equal to v in t
ordered_set::iterator it = t.find_by_order(rank); //Iterator that points to the (rank)th element in t
t.erase(it);
}
void printOrderedSet(ordered_set s){
//Function to show the contents of the set
for(auto it=s.begin(); it!=s.end(); it++){
cout<<*it<<" ";
}cout<<endl;
}
int main(){
//Create an ordered_set t with the numbers 0,0,1,1,2,2,3,3,4,4
ordered_set t;
for(int i=0; i<10;i++){
t.insert(i/2);
}
printOrderedSet(t); //output: 0 0 1 1 2 2 3 3 4 4
myerase(t, 3);
printOrderedSet(t); //output: 0 0 1 1 2 2 3 4 4
myerase(t, 3);
printOrderedSet(t); //output: 0 0 1 1 2 2 4 4
myerase(t, 0);
printOrderedSet(t); //output: 0 1 1 2 2 4 4
myerase(t, 1);
printOrderedSet(t); //output: 0 1 2 2 4 4
}