fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. class SquarePushBackIterator
  6. {
  7. std::vector<int>& dst;
  8. public:
  9. SquarePushBackIterator(std::vector<int>& dst) : dst(dst) {}
  10. // These just return references to the iterator.
  11. // The only operation that matters is assignment.
  12. SquarePushBackIterator& operator*() { return *this; }
  13. SquarePushBackIterator& operator++() { return *this; }
  14. SquarePushBackIterator& operator++(int) { return *this; }
  15.  
  16. // The assignment squares and inserts into the destination.
  17. void operator=(int val)
  18. {
  19. dst.push_back(val * val);
  20. }
  21. };
  22.  
  23. void mergeSortedArray(std::vector<int>& data)
  24. {
  25.  
  26. // Find the iterator range for the positive values.
  27. using iter = std::vector<int>::const_iterator;
  28. iter endPositive = std::end(data);
  29. iter loopPositive = std::find_if(std::begin(data), std::end(data),
  30. [](int val) {return val >= 0; });
  31.  
  32. // Find the iterator range for the negative values.
  33. using reve = std::reverse_iterator<iter>;
  34. reve endNegative = reve(std::begin(data));
  35. reve loopNegative = reve(loopPositive);
  36.  
  37. // Create an array to put the results into.
  38. std::vector<int> result;
  39. result.reserve(data.size());
  40.  
  41. // Perform a standard merge
  42. std::merge(loopPositive, endPositive, loopNegative, endNegative,
  43. SquarePushBackIterator(result),
  44. [](int val1, int val2){return std::abs(val1) < std::abs(val2); });
  45.  
  46. // Use move assignment to put the result in the output vector.
  47. // Alternatively we could return an array by value.
  48. data = std::move(result);
  49. }
  50.  
  51. int main()
  52. {
  53. std::vector<int> arr={ -3, -2, 0, 4, 5, 8 };
  54.  
  55. mergeSortedArray(arr);
  56.  
  57. for (const auto& i : arr)
  58. {
  59. std::cout << i << ' ';
  60. }
  61. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
                 from /usr/include/c++/4.8/ios:40,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from prog.cpp:1:
/usr/include/c++/4.8/bits/stl_algobase.h: In instantiation of ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const int*; _OI = SquarePushBackIterator]’:
/usr/include/c++/4.8/bits/stl_algobase.h:428:38:   required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<const int*, std::vector<int> >; _OI = SquarePushBackIterator]’
/usr/include/c++/4.8/bits/stl_algobase.h:460:17:   required from ‘_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const int*, std::vector<int> >; _OI = SquarePushBackIterator]’
/usr/include/c++/4.8/bits/stl_algo.h:5640:19:   required from ‘_OIter std::merge(_IIter1, _IIter1, _IIter2, _IIter2, _OIter, _Compare) [with _IIter1 = __gnu_cxx::__normal_iterator<const int*, std::vector<int> >; _IIter2 = std::reverse_iterator<__gnu_cxx::__normal_iterator<const int*, std::vector<int> > >; _OIter = SquarePushBackIterator; _Compare = mergeSortedArray(std::vector<int>&)::__lambda1]’
prog.cpp:44:67:   required from here
/usr/include/c++/4.8/bits/stl_algobase.h:382:57: error: no type named ‘value_type’ in ‘struct std::iterator_traits<SquarePushBackIterator>’
       typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
                                                         ^
/usr/include/c++/4.8/bits/stl_algobase.h:387:9: error: no type named ‘value_type’ in ‘struct std::iterator_traits<SquarePushBackIterator>’
         && __are_same<_ValueTypeI, _ValueTypeO>::__value);
         ^
/usr/include/c++/4.8/bits/stl_algobase.h: In instantiation of ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = std::reverse_iterator<__gnu_cxx::__normal_iterator<const int*, std::vector<int> > >; _OI = SquarePushBackIterator]’:
/usr/include/c++/4.8/bits/stl_algobase.h:428:38:   required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = std::reverse_iterator<__gnu_cxx::__normal_iterator<const int*, std::vector<int> > >; _OI = SquarePushBackIterator]’
/usr/include/c++/4.8/bits/stl_algobase.h:460:17:   required from ‘_OI std::copy(_II, _II, _OI) [with _II = std::reverse_iterator<__gnu_cxx::__normal_iterator<const int*, std::vector<int> > >; _OI = SquarePushBackIterator]’
/usr/include/c++/4.8/bits/stl_algo.h:5640:20:   required from ‘_OIter std::merge(_IIter1, _IIter1, _IIter2, _IIter2, _OIter, _Compare) [with _IIter1 = __gnu_cxx::__normal_iterator<const int*, std::vector<int> >; _IIter2 = std::reverse_iterator<__gnu_cxx::__normal_iterator<const int*, std::vector<int> > >; _OIter = SquarePushBackIterator; _Compare = mergeSortedArray(std::vector<int>&)::__lambda1]’
prog.cpp:44:67:   required from here
/usr/include/c++/4.8/bits/stl_algobase.h:382:57: error: no type named ‘value_type’ in ‘struct std::iterator_traits<SquarePushBackIterator>’
       typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
                                                         ^
/usr/include/c++/4.8/bits/stl_algobase.h:387:9: error: no type named ‘value_type’ in ‘struct std::iterator_traits<SquarePushBackIterator>’
         && __are_same<_ValueTypeI, _ValueTypeO>::__value);
         ^
stdout
Standard output is empty