Partitionner un tableau selon un prédicat avec l’algorithme ‘partition()’

Author:

 iterator
{filelink=15121}


#include 
#include 
#include 
using namespace std;

bool est_pair(int elem)
 {
	return (elem%2==0);
}

int main()
{

  int tab1[10], tab2[10];
  ostream_iterator out(cout, " ");

  for (int i = 0; i < 10; ++i) {
    tab1[i] = i + 1;
  }

  copy(&tab1[0], &tab1[10], &tab2[0]);

  cout << "Contenu initial du tableau: ";
  copy(&tab2[0], &tab2[10], out); cout << endl;

   /*
    Partionner le tableau en 2
    Part1: Nombre pairs
	Part2: Nombre impairs
  */
  int* split = partition(&tab2[0], &tab2[10], est_pair);

  cout << "Tableau partionné: ";
  copy(&tab2[0], split, out); cout << "| ";
  copy(split, &tab2[10], out); cout << endl;

  return 0;
}

/*
Contenu initial du tableau: 1 2 3 4 5 6 7 8 9 10
Tableau partionnÚ: 10 2 8 4 6 | 5 7 3 9 1
 */        

Leave a Reply

Your email address will not be published. Required fields are marked *