std :: nodalījums C ++ STL

C ++ ir klase STL algoritmu bibliotēkā, kas ļauj mums viegli sadalīt algoritmus, izmantojot noteiktas iebūvētas funkcijas. Sadalījums attiecas uz konteineru elementu dalīšanu atkarībā no noteiktā stāvokļa. 
Nodalīšanas operācijas :
1. nodalījums (beg gala nosacījums) :- šī funkcija ir pieradusi sadalīt elementus uz Stāvokļa pamats minēts tās argumentos.
2. Is_partitioned (beg gala nosacījums) :- Šī funkcija atgriežas Būla taisnība, ja konteiners ir sadalīts cits atgriežas nepatiess.

CPP
   // C++ code to demonstrate the working of    // partition() and is_partitioned()   #include       #include       // for partition algorithm   #include      // for vector   using     namespace     std  ;   int     main  ()   {      // Initializing vector      vector   <  int  >     vect     =     {     2       1       5       6       8       7     };          // Checking if vector is partitioned       // using is_partitioned()      is_partitioned  (  vect  .  begin  ()     vect  .  end  ()     [](  int     x  )      {      return     x  %  2  ==  0  ;          })  ?          cout      < <     'Vector is partitioned'  :      cout      < <     'Vector is not partitioned'  ;      cout      < <     endl  ;          // partitioning vector using partition()      partition  (  vect  .  begin  ()     vect  .  end  ()     [](  int     x  )      {      return     x  %  2  ==  0  ;          });          // Checking if vector is partitioned       // using is_partitioned()      is_partitioned  (  vect  .  begin  ()     vect  .  end  ()     [](  int     x  )      {      return     x  %  2  ==  0  ;          })  ?          cout      < <     'Now vector is partitioned after partition operation'  :      cout      < <     'Vector is still not partitioned after partition operation'  ;      cout      < <     endl  ;          // Displaying partitioned Vector      cout      < <     'The partitioned vector is : '  ;      for     (  int     &  x     :     vect  )     cout      < <     x      < <     ' '  ;          return     0  ;       }   

Izlaide: 

Vector is not partitioned Now vector is partitioned after partition operation The partitioned vector is : 2 8 6 5 1 7 

Iepriekš minētajā koda nodalījuma funkciju nodalījumos vektors atkarībā no tā, vai elements ir vienmērīgs vai nepāra, pat elementi ir sadalīti no nepāra elementiem nekādā noteiktā secībā. 
3. Stable_partition (beg gala nosacījums) :- šī funkcija ir pieradusi sadalīt elementus uz Stāvokļa pamats minēts tā argumentos Tāds veids, kā tiek saglabāta elementu relatīvā kārtība. Apvidū
4. nodalījums_point (beg gala nosacījums) :- šī funkcija Atgriež iteratoru, kas norāda uz nodalījuma punktu konteinera t.i., pirmais elements sadalītajā diapazonā [sākas), par kuru nosacījums nav patiess. Konteineram jau jābūt sadalītam, lai šī funkcija darbotos.

CPP
   // C++ code to demonstrate the working of    // stable_partition() and partition_point()   #include       #include       // for partition algorithm   #include      // for vector   using     namespace     std  ;   int     main  ()   {      // Initializing vector      vector   <  int  >     vect     =     {     2       1       5       6       8       7     };          // partitioning vector using stable_partition()      // in sorted order      stable_partition  (  vect  .  begin  ()     vect  .  end  ()     [](  int     x  )      {      return     x  %  2     ==     0  ;         });          // Displaying partitioned Vector      cout      < <     'The partitioned vector is : '  ;      for     (  int     &  x     :     vect  )     cout      < <     x      < <     ' '  ;      cout      < <     endl  ;          // Declaring iterator      vector   <  int  >::  iterator     it1  ;          // using partition_point() to get ending position of partition      auto     it     =     partition_point  (  vect  .  begin  ()     vect  .  end  ()     [](  int     x  )      {      return     x  %  2  ==  0  ;      });          // Displaying partitioned Vector      cout      < <     'The vector elements returning true for condition are : '  ;      for     (     it1  =     vect  .  begin  ();     it1  !=  it  ;     it1  ++  )      cout      < <     *  it1      < <     ' '  ;      cout      < <     endl  ;          return     0  ;       }   

Izlaide: 

The partitioned vector is : 2 6 8 1 5 7 The vector elements returning true for condition are : 2 6 8 

Iepriekš minētajā kodā pat un nepāra elementi ir sadalīti un pieaugošā secībā (sakārtoti). Ne vienmēr pieaugošā secībā, lai gan šeit elementi (pat un nepāra) parādījās paaugstinātā secībā, tāpat ir rezultāts pēc sadalīšanas. Ja VECT būtu bijis {217865} pēc stabila_partition (), tas būtu {286175}. Tiek uzturēta izskata secība.
5. Nodalīšana_copija (beg end beg1 beg2 nosacījums) :- šī funkcija kopē sadalītos elementus dažādos konteineros, kas minēti tā argumentos. Tas prasa 5 argumentus. Jaunā konteinera sākuma un beigu pozīcija. Apvidū Mainīgs Jauni konteineri ir nepieciešams par šo funkciju.

CPP
   // C++ code to demonstrate the working of    // partition_copy()   #include       #include       // for partition algorithm   #include      // for vector   using     namespace     std  ;   int     main  ()   {      // Initializing vector      vector   <  int  >     vect     =     {     2       1       5       6       8       7     };          // Declaring vector1      vector   <  int  >     vect1  ;          // Declaring vector1      vector   <  int  >     vect2  ;          // Resizing vectors to suitable size using count_if() and resize()      int     n     =     count_if     (  vect  .  begin  ()     vect  .  end  ()     [](  int     x  )      {      return     x  %  2  ==  0  ;          }     );      vect1  .  resize  (  n  );         vect2  .  resize  (  vect  .  size  ()  -  n  );          // Using partition_copy() to copy partitions      partition_copy  (  vect  .  begin  ()     vect  .  end  ()     vect1  .  begin  ()         vect2  .  begin  ()     [](  int     x  )      {      return     x  %  2  ==  0  ;      });              // Displaying partitioned Vector      cout      < <     'The elements that return true for condition are : '  ;      for     (  int     &  x     :     vect1  )         cout      < <     x      < <     ' '  ;      cout      < <     endl  ;          // Displaying partitioned Vector      cout      < <     'The elements that return false for condition are : '  ;      for     (  int     &  x     :     vect2  )         cout      < <     x      < <     ' '  ;      cout      < <     endl  ;          return     0  ;      }   

Izlaide: 

The elements that return true for condition are : 2 6 8 The elements that return false for condition are : 1 5 7