Link to home
Start Free TrialLog in
Avatar of CTPAS
CTPAS

asked on

What is the output of this sort function?

IF
vector<int>   A;   // 10 elements, say 0-9
vector<long>   B;  // 10   elements, say all == 1
AND
struct SPredicate
{
      const long* _rowBegin;
      SPredicate(const vector<long>& v) : _rowBegin( v.begin() ) { }
      bool operator()(int i, int j) { return _rowBegin[ i ]  <  _rowBegin[ j ] ; }
};
What would the ouput be of the following?
std::sort(A.begin(), A.end(),SPredicate(B));


Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

The predicate is indexing B and comparing B using the current value of i and j (both of which will be values of A); however, all elements in B are 1 so I'd expect that A would remain in the same order as it was initially.
^^^this assumes that the iterator of a vector is convertible to a type T *, which the C++ Standard doesn't as far as I can remember, define.
Avatar of CTPAS
CTPAS

ASKER

Thanks for quick resonse
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CTPAS

ASKER

yes. I was only unclear about where i , j came from in the comparison.
This method is a compact way to order set A elements based on the ordering of set B