This C++ program illustrates usage of list. The std::list class is a container that provides constant time insertion removal of elements from anywhere in the list but fast random access is not supported by list. This list supports bidirectional iteration and is usually implemented as a doubly linked list.
Here is the source code of the C++ program which illustrates usage of list. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to illustrate usage of list container
*/
#include <iostream>
#include <list>
using namespace std;
void printList(list<int> l)
{
for(list<int>::const_iterator i = l.begin(); i != l.end(); i++)
cout << *i << " ";
cout << "\n";
}
int main() {
list<int> l1 = {5, 2, 1, 10, 3, 4, 7, 6, 9, 8}, l2 = {13, 14, 12, 15};
cout << "Unsorted List l1 : ";
printList(l1);
cout << "Unsorted List l2 : ";
printList(l2);
l1.sort();
cout << "Sorted List l1 : ";
printList(l1);
l2.sort();
cout << "Sorted List l2 : ";
printList(l2);
l1.merge(l2);
cout << "Merged List l1 : ";
printList(l1);
}
$ gcc test.cpp $ a.out Unsorted List l1 : 5 2 1 10 3 4 7 6 9 8 Unsorted List l2 : 13 14 12 15 Sorted List l1 : 1 2 3 4 5 6 7 8 9 10 Sorted List l2 : 12 13 14 15 Merged List l1 : 1 2 3 4 5 6 7 8 9 10 12 13 14 15
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Check C++ Books
- Check Programming Books
- Check Computer Science Books