Pirmyn sąrašas C ++ | 2 rinkinys (manipuliavimo funkcijos)

Pirmyn sąrašas C ++ | 1 rinkinys (įvadas ir svarbios funkcijos) Šiame straipsnyje aptariama daugiau funkcijų, išskyrus intarpus ir delecijas, kurios gali būti naudojamos išankstiniuose sąrašuose: 

1. sujungti () :- Ši funkcija naudojama vienam išankstiniam sąrašui sujungti su kita. Jei abu sąrašai yra surūšiuoti, tada gaunamas sąrašas taip pat yra surūšiuotas. 

2. Operatorius '=' :- Šis operatorius nukopijuoja vieną į priekį sąrašą į kitą. Šiuo atveju padaryta kopija yra gili. 

CPP
   // C++ code to demonstrate the working of    // merge() and operator=    #include          #include         using     namespace     std  ;      int     main  ()      {         // Initializing 1st forward list       forward_list   <  int  >     flist1     =     {  1       2       3  };             // Declaring 2nd forward list       forward_list   <  int  >     flist2  ;             // Creating deep copy using '='       flist2     =     flist1  ;             // Displaying flist2       cout      < <     'The contents of 2nd forward list'      ' after copy are : '  ;         for     (  int     &  x     :     flist2  )         cout      < <     x      < <     ' '  ;         cout      < <     endl  ;             // Using merge() to merge both list in 1       flist1  .  merge  (  flist2  );             // Displaying merged forward list       // Prints sorted list       cout      < <     'The contents of forward list '      'after merge are : '  ;         for     (  int     &  x     :     flist1  )         cout      < <     x      < <     ' '  ;         cout      < <     endl  ;             return     0  ;      }      

Išvestis:

The contents of 2nd forward list after copy are : 1 2 3 The contents of forward list after merge are : 1 1 2 2 3 3  

Laiko sudėtingumas: O (1)

Pagalbinė erdvė: O (1)
  3. rūšiuoti () :- Ši funkcija naudojama išankstinio sąrašo rūšiavimui. 

4. unikalus () :- Ši funkcija ištrina daugybę skaičiaus įvykių ir grąžina į priekį sąrašą su unikaliais elementais. Išankstinis sąrašas turėtų būti surūšiuotas, kad ši funkcija sėkmingai vyktų. 

CPP
   // C++ code to demonstrate the working of    // sort() and unique()    #include          #include      // for sort() and unique()    using     namespace     std  ;      int     main  ()      {         // Initializing 1st forward list       forward_list   <  int  >     flist1     =     {  1       2       3       2       3       3       1  };         // Sorting the forward list using sort()       flist1  .  sort  ();         // Displaying sorted forward list       cout      < <     'The contents of forward list after '      'sorting are : '  ;         for     (  int     &  x     :     flist1  )         cout      < <     x      < <     ' '  ;         cout      < <     endl  ;         // Use of unique() to remove repeated occurrences       flist1  .  unique  ();         // Displaying forward list after using unique()       cout      < <     'The contents of forward list after '      'unique operation are : '  ;         for     (  int     &  x     :     flist1  )         cout      < <     x      < <     ' '  ;         cout      < <     endl  ;         return     0  ;      }      

Išvestis:

The contents of forward list after sorting are : 1 1 2 2 3 3 3 The contents of forward list after unique operation are : 1 2 3  

Laiko sudėtingumas: O (1)

Pagalbinė erdvė: O (1)
 

5. atvirkštinis () :- Ši funkcija naudojama priekinio sąrašo panaikinimui.

6. apsikeitimas () :- Ši funkcija apsikeičia vieno priekinio sąrašo turiniu su kita. 

CPP
   // C++ code to demonstrate the working of    // reverse() and swap()    #include          #include      // for reverse() and swap()    using     namespace     std  ;      int     main  ()      {         // Initializing 1st forward list       forward_list   <  int  >     flist1     =     {  1       2       3  };         // Initializing 2nd forward list       forward_list   <  int  >     flist2     =     {  4       5       6  };         // Using reverse() to reverse 1st forward list       flist1  .  reverse  ();         // Displaying reversed forward list       cout      < <     'The contents of forward list after'      ' reversing are : '  ;         for     (  int     &  x     :     flist1  )         cout      < <     x      < <     ' '  ;         cout      < <     endl      < <     endl  ;         // Displaying forward list before swapping       cout      < <     'The contents of 1st forward list '      'before swapping are : '  ;         for     (  int     &  x     :     flist1  )         cout      < <     x      < <     ' '  ;         cout      < <     endl  ;         cout      < <     'The contents of 2nd forward list '      'before swapping are : '  ;         for     (  int     &  x     :     flist2  )         cout      < <     x      < <     ' '  ;         cout      < <     endl  ;         // Use of swap() to swap the list       flist1  .  swap  (  flist2  );         // Displaying forward list after swapping       cout      < <     'The contents of 1st forward list '      'after swapping are : '  ;         for     (  int     &  x     :     flist1  )         cout      < <     x      < <     ' '  ;         cout      < <     endl  ;         cout      < <     'The contents of 2nd forward list '      'after swapping are : '  ;         for     (  int     &  x     :     flist2  )         cout      < <     x      < <     ' '  ;         cout      < <     endl  ;         return     0  ;      }      

Išvestis:

The contents of forward list after reversing are : 3 2 1 The contents of 1st forward list before swapping are : 3 2 1 The contents of 2nd forward list before swapping are : 4 5 6 The contents of 1st forward list after swapping are : 4 5 6 The contents of 2nd forward list after swapping are : 3 2 1  

Laiko sudėtingumas: O (1)

Pagalbinė erdvė: O (1)
 

7. išvalyti () :- Ši funkcija išvalo išankstinio sąrašo turinį. Po šios funkcijos išankstinis sąrašas tampa tuščias.

  8. tuščias () :- Ši funkcija grąžinama teisinga, jei sąrašas tuščias kitaip klaidingas. 

CPP
   // C++ code to demonstrate the working of    // clear() and empty()    #include          #include      // for clear() and empty()    using     namespace     std  ;      int     main  ()      {         // Initializing forward list       forward_list   <  int  >     flist1     =     {  1       2       3  };             // Displaying forward list before clearing       cout      < <     'The contents of forward list are : '  ;         for     (  int     &  x     :     flist1  )         cout      < <     x      < <     ' '  ;         cout      < <     endl  ;             // Using clear() to clear the forward list       flist1  .  clear  ();             // Displaying list after clear() performed       cout      < <     'The contents of forward list after '       < <     'clearing are : '  ;         for     (  int     &  x     :     flist1  )         cout      < <     x      < <     ' '  ;         cout      < <     endl  ;             // Checking if list is empty       flist1  .  empty  ()     ?     cout      < <     'Forward list is empty'     :         cout      < <     'Forward list is not empty'  ;             return     0  ;      }      

Išvestis:

The contents of forward list are : 1 2 3 The contents of forward list after clearing are : Forward list is empty 

Laiko sudėtingumas: O (1)

Pagalbinė erdvė: O (1)
Naujausi straipsniai „Forward_List“