Bat*_*n05 5 c++ java set treeset data-structures
我需要在C++中使用树集数据结构(在Java中可用),并使用像TreeSet.lower(i)和TreeSet.higher(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
}
Run Code Online (Sandbox Code Playgroud)
gsa*_*ras 10
使用std::set,通常作为二叉搜索树实现.
它insert(),erase()和find()方法的大小对数,但如果一个提示,给出可以做的更好.对数复杂性被引用到Java TreeSet.
我认为你应该感兴趣std::lower_bound,它将一个迭代器返回到下界,而in std::upper_bound,它返回一个迭代器到上界.