fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. vector<int> data = {1, 5, 2, 20, 10, 7, 9, 1000};
  10. vector<const int*> ptr(data.size());
  11. transform(data.begin(), data.end(), ptr.begin(), [](const int& d) {return &d;});
  12. auto mid = next(ptr.begin(), data.size() / 2);
  13. nth_element(ptr.begin(), mid, ptr.end(), [](const int* lhs, const int* rhs) {return *lhs < *rhs;});
  14. ptrdiff_t pos = *mid - &data[0];
  15. cout << pos << endl << data[pos] << endl;
  16. return 0;
  17. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
6
9