Is there a TreeSet data structure equivalent in C++ with similar functions

Batwoman05 :

I need to use a Tree Set data structure(available in java) in C++, and make use of functions like TreeSet.lower(i) and TreeSet.higher(i) - > which returns the element just lower, and just higher than i in the given tree set. Is there an STL?

Edit: Following is the functionality I need, I was wondering how to use the upper_bound and lower_bound functions to do it:

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
}
gsamaras :

Use std::set, which is typically implemented as a binary search tree.

Its insert(), erase() and find() methods are logarithmic in size, but can do better if a hint is given. The logarithmic complexity is reffered to the the Java TreeSet.

I think you should be interested in std::lower_bound, which returns an iterator to the lower bound, and in std::upper_bound, which returns an iterator to the upper bound.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=471386&siteId=1