Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Class cplusplus
01-23-2010, 06:28 AM
Post: #1
List Class cplusplus
List Class is pretty much the same in all programming languages. The only thing why people avoid using some classes is because of iterators. But you need to learn only basics and everything else will look so logical.
I am going to explain how to use list and i will give only integer examples, because all other types are pretty the same as integer.
So first declaring list class on the top should look like this :
Code:
# include <list>

Now let's show how to declare your list inside of the program :
Code:
list<type> name_of_the_list;
or if you want to add some values on declaring into your list :
Code:
list<int> name_of_the_list (number_of_times, number_that_shows);
"number_of_times" is a number that represent how many times "number_that_shows" will be shown in the list.
For example list output would be :
Code:
5 5 5
if you declare your list like this :
Code:
list<int> name_of_the_list (3,5);
Now let's see some functions that you can use in list :
Code:
empty     Test whether container is empty
size             Return size
max_size     Return maximum size
resize     Change size
push_front     Insert element at beginning
push_back     Add element at the end
reverse     Reverse the order of elements
sort             Sort elements in container
clear             Clear content
Those are main functions, there are more, but these are basically everything that you need to use.
Now let's show some examples.
Let's read one list from the screen and write it's members backwards from user entering :
Code:
# include <iostream>
# include <cstdio>
# include <list>

using namespace std;

int main ()
{
  list<int> mylist;
  int i,k,n;
cin>>n;
for (i=0;i<n;i++)
  {
    cin>>k;
    mylist.push_front(k);
  }
  for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    cout<<" "<<*it;
  cout<<endl;
  system("pause");
}
Basically in the for loop you can use use of iterators. At the beginning just learn it as it is and later it will be more clear when you start to use them.
Sort() function sorts list ascending. Now let's show that example :
Code:
# include <iostream>
# include <cstdio>
# include <list>

using namespace std;

int main ()
{
  list<int> mylist;
  int i,k,n;
cin>>n;
for (i=0;i<n;i++)
  {
    cin>>k;
    mylist.push_front(k);
  }
  mylist.sort();
  for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    cout<<" "<<*it;
  cout<<endl;
  system("pause");
}
As you can see there is the problem to sort it descending. You need to use one trick. Sort it ascending and do reverse function :
Code:
# include <iostream>
# include <cstdio>
# include <list>

using namespace std;

int main ()
{
  list<int> mylist;
  int i,k,n;
cin>>n;
for (i=0;i<n;i++)
  {
    cin>>k;
    mylist.push_front(k);
  }
  mylist.sort();
  mylist.reverse();
  for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
    cout<<" "<<*it;
  cout<<endl;
  system("pause");
}
Those are some small tricks about using lists. There are much more to say about this, but for now it is enough.
Hope you like it !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: