C++ Program to Demonstrate Splice Operation on Lists

This C++ program demonstrates the splice operation on std::list container. The splice operation removes the elements from one std::list and copies them into another. The program uses two versions of std::list::splice – one which moves only the element pointed by an iterator and second which moves the elements between the beginning and ending iterators.

Here is the source code of the C++ program which demonstrates the splice operation on std::list container. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Demonstrate Splice Operation on Lists
  3.  */
  4. #include <iostream>
  5. #include <list>
  6. #include <algorithm>
  7. #include <string>
  8. using namespace std;
  9.  
  10. void print(list<string>& l)
  11. {
  12.     list<string>::const_iterator i = l.begin();
  13.  
  14.     while(i != l.end()) {
  15.         cout << *i << "  ";
  16.         i++;
  17.     }
  18.     cout << "\n";
  19. }
  20.  
  21. int main() {
  22.     list<string> l1, l2;
  23.  
  24.     l1.push_back(string("John Keats"));
  25.     l1.push_back(string("William Wordsworth"));
  26.     l1.push_back(string("Walt Whitman"));
  27.     l1.push_back(string("Robert Frost"));
  28.     l1.push_back(string("William Shakespeare"));
  29.     l1.push_back(string("Percy Shelley"));
  30.     cout << "List 1 : ";
  31.     print(l1);
  32.     cout << "List 2 : ";
  33.     print(l2);
  34.  
  35.     l2.splice(l2.end(), l1, l1.begin());
  36.     cout << "After l2.splice(l2.end(), l1, l1.begin());\n";
  37.     cout << "List 1 : ";
  38.     print(l1);
  39.     cout << "List 2 : ";
  40.     print(l2);
  41.  
  42.     l2.splice(l2.begin(), l1, ++(l1.begin()), --(l1.end()));
  43.     cout << "After l2.splice(l2.begin(), l1, ++(l1.begin()), --(l1.end()));\n";
  44.     cout << "List 1 : ";
  45.     print(l1);
  46.     cout << "List 2 : ";
  47.     print(l2);
  48. }

$ gcc test.cpp
$ a.out
List 1 : John Keats  William Wordsworth  Walt Whitman  Robert Frost  William Shakespeare  Percy Shelley  
List 2 : 
After l2.splice(l2.end(), l1, l1.begin());
List 1 : William Wordsworth  Walt Whitman  Robert Frost  William Shakespeare  Percy Shelley  
List 2 : John Keats  
After l2.splice(l2.begin(), l1, ++(l1.begin()), --(l1.end()));
List 1 : William Wordsworth  Percy Shelley  
List 2 : Walt Whitman  Robert Frost  William Shakespeare  John Keats

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.