在C 中,是否有树木数据结构具有相似的功能
Is there a TreeSet data structure equivalent in C++ with similar functions
我需要在C 中使用树集数据结构(可在Java中使用(,并使用treeset.lower..lower(i(和treetet.higher.higher(i( ->的功能将元素返回较低,并且比给定的树集中的I高。有stl吗?
编辑:以下是我需要的功能,我想知道如何使用upper_bound和lower_bound函数来完成:
for (int i = 1; i<10; i++) myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90
int k = 50; // I need 40 and 60
set<int>::iterator itr = myset.find(k);
if (itr != myset.end()) {
// Found the element
itr--; // Previous element;
cout << *(itr); //prints 40
itr++; // the element found
itr++; // The next element
cout << *(itr); // prints 60
}
使用std::set
,通常用作二进制搜索树。
其insert()
,erase()
和find()
方法的大小对数,但是如果给出提示,可以做得更好。对数复杂性被转向爪哇树。
我认为您应该对std::lower_bound
感兴趣,它将迭代器返回到下限和std::upper_bound
,它将迭代器返回到上限。
您可以使用std::set
查看std::set::lower_bound
和std::set::upper_bound
您可以在此处使用std ::设置。对于您的功能,您可以使用函数upper_bound(i(和lower_bound(i(,但是在这里抓住它们不类似于treetet.treet.lower(i(和treeset.higher.higher(i(。
lower_bound(const i( - 将迭代器返回到容器中的第一个元素,该元素在i(即它是等效或之后(或设置为::结束如果所有元素都被考虑在i。
之前upper_bound(const i( - 将迭代器返回到容器中的第一个元素中,该元素被认为是在i之后进行的,或者set :: end :: end如果不考虑元素在i。<<<<<<<</p>
for (int i = 1; i<10; i++) myset.insert(i * 10); // 10 20 30 40 50 60 70 80 90
int k = 50;
set<int>::iterator itlow,itup;
itlow=myset.lower_bound (k);
itup=myset.upper_bound (k);
if(itlow!=myset.begin()){
itlow--;
cout << *itlow; // 40 will print
}
cout << *itup; // 60 will print
相关文章:
- 链表,反向函数,数据结构
- 如何使用set实现无序数据结构?
- 我们可以将数据永久保存为数据结构吗?
- C++中的可变长度数组/数据结构
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 通过 NIF 从C++返回自定义数据结构
- 编译器上的策略数据结构不起作用
- 尝试构建"lock-free"数据结构C++
- 设计将引用元素移动到开头的数据结构.C++
- 在学习数据结构之前对STL有一个了解是好的吗?
- 如何解析表示树状数据结构的字符串
- 我对数据结构、双向链表有一些问题
- googletest:测试太大的数据结构
- C++中deque数据结构的大O是什么?
- 我可以使用哪种数据结构来处理这种方式
- 将文本文件解析为树状数据结构
- C++ 中具有 O(1) 搜索时间复杂度的数据结构
- 哪些存储了不完整类型的 STL 数据结构可以用作类成员?
- C++,您能否设计一种数据结构,将指针保存在连续内存中并且不会使它们失效?
- 在C 中,是否有树木数据结构具有相似的功能