sort the smallest 4 elemetns in the Numbers and copy the results in Result : vector sort « vector « C++ Tutorial






#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(void)
{
  const int VECTOR_SIZE = 8;
  typedef vector<int> IntVector;
  typedef IntVector::iterator IntVectorIt;

  IntVector Numbers(VECTOR_SIZE);
  IntVector Result(4);

  IntVectorIt start, end, it;

  Numbers[0] = 4;
  Numbers[1] = 10;
  Numbers[2] = 70;
  Numbers[3] = 30;
  Numbers[4] = 10;
  Numbers[5] = 69;
  Numbers[6] = 96;
  Numbers[7] = 7;

  start = Numbers.begin();  
  end = Numbers.end();    

  for (it = start; it != end; it++)
    cout << *it << " ";

  partial_sort_copy(start, end, Result.begin(), Result.end());
  for(it = start; it != end; it++)
    cout << *it << " ";
  for(it = Result.begin(); it != Result.end(); it++)
    cout << *it << " ";
}








16.29.vector sort
16.29.1.sort the smallest 4 elemetns in the Numbers and copy the results in Result
16.29.2.sort NumbersVestor, mersge requires the sequences to be sorted
16.29.3.Vector Insert Erase Sort