Bloomberg LP Interview Question
Financial Software DevelopersIt is D. We need to bind 50 with greater so that it will take 50 to compare it with vectory arguments.
Options A and C can be safely eliminated from possible choices because 'erase' cannot take 3 parameters.
Option E can also be safely eliminated because there is no function such as vector::remove_if_greater().
Option D can also be safely eliminated because there is no function such as vector::remove_if(). It is in fact std::remove_if()
Option B is the only choice left which should be true, although I have not been able to compile it.
Had there been an option F, it would have been:
f.std::remove_if(items.begin(), items.end(), std::bind2nd(std::greater<int>(), 50)));
Hence, option F is the tested correct thing which I have tested myself and it works 100%
A is the answer.This is the compiled code.
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
unsigned int i;
vector<unsigned int> items;
for (i=50; i<=60; i++) items.push_back(i);
items.erase(std::remove_if(items.begin(), items.end(), std::bind2nd(std::greater<int>(), 50)), items.end());
//std::remove_if(items.begin(), items.end(), std::greater<int>(50));
//items.erase(std::remove_if(items.begin(), items.end(), std::bindlst(std::less_equal<int>(), 50)), items.end());
//items.remove_if(items.begin(), items.end(), std::bind2nd(std::greater<int>(), 50));
//items.remove_if_greater(items.begin(), items.end(), 50);
cout << "Vector contains:";
for (i=0; i<items.size(); i++)
cout << " " << items[i]<< endl;
return 0;
}
a and c both are correct answers. Try the following code by 1st uncommenting a and see the result, then uncomment c and you'll see the same result as that of a.
@ano.... you have written the 'l' alphabet instead of digit '1' in std::bind1st (its read as bind first).
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
void main()
{
std::vector<int> a;
a.push_back(10);
a.push_back(90);
a.push_back(60);
a.push_back(40);
a.push_back(35);
a.push_back(100);
std::cout << "\n Original vector: ";
for(size_t x=0; x < a.size(); x++)
std::cout << "\n " << a[x];
// a - correct
//a.erase(std::remove_if(a.begin(),a.end(),std::bind2nd(std::greater<int>(),50)),a.end());
// b - wrong
//std::remove_if(a.begin(), a.end(), std::greater<int>(50));
// c - correct
//a.erase(std::remove_if(a.begin(), a.end(), std::bind1st(std::less_equal<int>(), 50)), a.end());
// d - wrong
//a.remove_if(a.begin(), a.end(), std::bind2nd(std::greater<int>(), 50)));
// e - wrong
//a.remove_if_greater(a.begin(), a.end(), 50);
std::cout << "\n\n New vector: ";
for(size_t x=0; x < a.size(); x++)
std::cout << "\n " << a[x];
std::cout << "\n";
}
(b) is incorrect
because remove_if does NOT physically removes items. it just re-arranges them and points to first one which has the predicate = true;
10, 55, 30, 40 -> 10, 30, 40 "ITR" 50
To remove items from the vector you must still call erase.
(c) seems does quite opposite to what is required.
(d) and (e) just incorrrect because there is no such member functions in std::vector
A is the correct answer.
- wayne February 07, 2012B: greater<>() takes 0 arguments. It has a member function operator() that takes two arguments, which returns true if its first argument compares greater than the second one using operator>, and false otherwise. The greater class does not have a constructor taking one argument.
D: remove_if is not a member function of vector class.
E: remove_if_greater does not exist in vector class.
Why choose A not C:
bind2nd(greater<int>(),50) and bind1st(less_equal<int>(), 50) are the unary function objects constructed from the binary function objects greater<int>() and less_equal<int>() by binding its 2nd/1st parameters to 50. So the above function objects can be considered as:
bind2nd(greater<int>(),50) "greater than 50?" (true if > 50)
bind1st(less_equal<int>(), 50) "greater than or equal to 50?" (true if 50<=)
Function
ForwardIterator remove_if(ForwardIterator first, ForwardIterator last, Predicate pred):
Applies pred to the elements in the range [first,last), and removes those for which it does not return false from the resulting range. The resulting range consists of the elements between first and the iterator returned by the function, which points to the new end of the range. So the elements between the new end and the old end should not be valid.
So,
std::remove_if(items.begin(), items.end(), std::bind2nd(std::greater<int>(), 50)) will remove the elements >50 in items, and return a new end of items. And erase() will remove the elements between the new end and the old end.