Using list

list is a collection or container class that allows you to store and process any type of values. It provides an efficient implementation for insertion and deletion operations at any location of the container. For applications that require frequent insertion and deletion operations at the front and back of a container, deque class is more efficient. The list container is implemented by using doubly linked list concept so that you can traverse the list forward and backward.

The list class has the following constructors that can be used to create a list object to store and process values.
-list(const allocator_type& __a) creates an empty list object with the default allocator.
-list(size_type __n) creates a list object with specifying the number of items to be stored in that container.
-list(size_type __n, const _Tp& __value, const allocator_type& __a) creates an list object with specifying the number of items to be stored in that container, and fills that container with n copies of value.
-list(const list& __x) creates a list object from a specified list object.

The commonly used member functions of the list class are push_back(), push_front(), insert(), pop_back(), pop_front(), remove(), remove_if(), clear(), begin(), end(), size(), merge(), sort(), reverse(), and unique(). The push_back() and push_front() functions allow you to add a new item to end and to the beginning of the list respectively. The insert() function allows you to insert a new item at any position of the list. In contrast, the pop_back() and pop_font() will remove the last item and the beginning item from the list respectively. The remove() function is used to remove any item from the list. If you want to remove items that match a specific condition, you will use the remove_if(). The remove_if function accepts a predicate function that specifies the condition. By using the clear(), you can delete all items of the list. The begin() reads the first item and the end() refers to the next position after the end of the container. The size() member returns the number of items stored in the list. The merge() function can be used to merge two lists. The sort() function sorts the list in ascending order. Another version of the sort() function that accepts a predicate function can be used to sort the list in any order. The reverse() and unique() function are able to reverse items in the list and remove duplicates from the list respectively.

#include <cstdlib>
#include <iostream>
#include <list>
#include <iterator>
using namespace std;
//function predicate used to specify sort order
bool com(int a,int b){
   if(a>=b) return true;
   else return false;

}
//function predicate used to match items less than 50
bool lessThan50(int a){
             if(a<50) return true;
             else return false;
   }

int main(int argc, char *argv[])
{
   //create an empty list object
   list<int> lst;
   list<int> lst1(5,200);
   //create output stream iterator for outputing the list on
   //console window
   ostream_iterator<int> out(cout," ");
   //add items to the front of the list lst
   lst.push_front(12);
   lst.push_front(101);
   lst.push_front(11);
   //add items to the back of the list lst
   lst.push_back(121);
   lst.push_back(100);
   lst.push_back(111);
   //insert new items
   list<int>::iterator p=lst.begin();
   lst.insert(p++,40);
   lst.insert(p++,60);
   lst.insert(p++,100);
   //the items of the list lst using iterator
   list<int>::iterator it=lst.begin();
   cout<<"Before removing items: ";  
   while(it!=lst.end()){
               cout<<*it<<" ";
               it++;
               }
   cout<<"\n";
   //delete an item from the front of list lst
   lst.pop_front();
   //delete an item from the back of list lst
   lst.pop_back();
   //remove all 101s
   lst.remove(101);
   //remove all items less than 50
   lst.remove_if(lessThan50);
   cout<<"After removing items: ";
   copy(lst.begin(),lst.end(),out);
   cout<<"\n";
   //reverse items of list  
   lst.reverse();
   cout<<"After reversing items: ";
   copy(lst.begin(),lst.end(),out);
   cout<<"\n";
   //sort the list in ascending order
   lst.sort();
   cout<<"After sorting items in ascending: ";
   copy(lst.begin(),lst.end(),out);
   cout<<"\n";
   //sort the list in descending order
   //by specifying a predicate function com
   lst.sort(com);
   cout<<"After sorting items in descending: ";
   copy(lst.begin(),lst.end(),out);
   cout<<"\n";
   //merge lists
   lst.merge(lst1);
   cout<<"After merging: ";
   copy(lst.begin(),lst.end(),out);
   cout<<"\n";
   //show min item
   cout<<"Min item:"<<*min_element(lst.begin(),lst.end())<<endl;
   //show max item
   cout<<"Max item:"<<*max_element(lst.begin(),lst.end())<<endl;
   // locate first occurrence of 100 in list      
   list< int >::iterator fit;      
   fit=find(lst.begin(), lst.end(), 100 );
   if (fit!= lst.end() ) // found 100
     cout << "Found 100\n";
   else // 100 not found
     cout << "100 not found\n";
   //remove duplicates
   lst.unique();
   cout<<"After removing duplicates: ";
   copy(lst.begin(),lst.end(),out);
   cout<<"\n";
  
   //remove all items of the list lst
   lst.clear();

   system("PAUSE");
   return EXIT_SUCCESS;
}

list

Posted by: Dara | post date: 02-03-2014 | Subject: C++ Programming Language

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.