【发布时间】:2018-04-28 15:41:42
【问题描述】:
我需要在 C++ 中使用 Tree Set 数据结构(在 java 中可用),并使用 TreeSet.lower(i) 和 TreeSet.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
}
【问题讨论】:
标签: java c++ data-structures set treeset