Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lower_bound for vector<MyClass*>

I have this simple class:

class MyClass {
public:
    int id;
    string name;

};

I want to have a vector with pointers to objects of this class that is sorted by the referenced MyClass id. I thought that using lower_bound would be easy, I did it before with vectors of objects (not pointers). With objects, I overloaded operator< like that:

bool operator<(MyClass left, int right) {
    return (left.id < right);
}

Then I used lower_bound to insert new MyClass object to sorted vector.

vector<MyClass>::iterator low;
low = lower_bound(vectorname.begin(),vectorname.end(),id);
prP = idContainer.begin();
prP = idContainer.insert(low, newobject); 

I am lost how to do the same with the vector of MyClass pointers. Can anyone help me achieve that?

like image 685
703 Avatar asked Apr 10 '16 16:04

703


People also ask

Can we use lower_bound on vector?

You can use lower_bound on vector of pairs with custom compare operator .

What does lower_bound function do?

The lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than val. This means that the function returns an iterator pointing to the next smallest number just greater than or equal to that number.

What is lower_bound in STL?

Set lower_bound() function in C++ STL returns an iterator pointing to the element in the container which is equivalent to k passed in the parameter. If k is not present in the set container, then the function returns an iterator pointing to the immediate next element which is just greater than k.

What will the lower_bound () function return if the element you passed as an argument isn't present in the vector VECT?

lower_bound returns an iterator pointing to the first element in the range [first,last) which has a value not less than 'val'. and if the value is not present in the vector then it returns the end iterator.

What is upper and lower_bound vector vector in C++?

Vector – upper_bound and lower_bound. lower_bound returns an iterator pointing to the first element in the range [first,last) which has a value not less than ‘val’. upper_bound returns an iterator pointing to the first element in the range [first,last) which has a value greater than ‘val’. using namespace std; int gfg[] = {5,6,7,7,6,5,5,6};

What is the difference between vector of pairs and lower_bound()?

It returns an iterator pointing to the first element in the range [first, last) which has a value greater than or equal to the given value “val”. But in Vector of Pairs lower_bound () for pair (x, y) will return an iterator pointing to the position of pair whose first value is equal to x and second value is greater than equals to y.

What is the use of lower bound in C++?

The lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than val. This means that the function returns the index of the next smallest number just greater than or equal to that number.

What is the difference between lower bound and upper bound in Python?

In a Vector, lower bound returns an iterator pointing to the first element in the range that does not compare the given value. Upper Bound returns an iterator pointing element in the range that smaller than given value.


1 Answers

There are two overloads of std::lower_bound:

template< class ForwardIt, class T >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );

template< class ForwardIt, class T, class Compare >
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp );

The first one is the one you used for your vector<MyClass>, it uses operator< by default. The second one allows for a custom comparison function which takes an element from the container as the first argument and the value as the second. That's what you want to use for your vector<MyClass*>:

std::vector<MyClass*> pvec;
auto low = std::lower_bound(pvec.begin(), pvec.end(), id,
    [](const MyClass* c, const MyClass& id) {
        return *c < id;
    });

It's a little odd that the comparison takes two arguments of different types, but that's just how it is.

Note: your current operator< takes its arguments by value. That incurs unnecessary copies. You'll want to change that to take them by reference to const.

like image 141
Barry Avatar answered Nov 13 '22 10:11

Barry