Can STL be used in user defined class ???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tushu
    New Member
    • May 2015
    • 9

    Can STL be used in user defined class ???

    I want to learn STL feature in C++. I have gone through "list" and its function like push_front(),pu sh_back() etc...
    Now I want to ask , can I use "list" in my user defined class linkedlist ??
    In other words I want to say , if I have been given to reverse the linked list, I will create and print it by STL ( using push_front() and copy ) and main reversing logic , I will do by class linkedlist which I defined so that I will save time to create linkedlist.
    It means Can I use STL with User defined class ??
    I tried it through following code.
    Inserting node at the start of linked list
    Code:
     
    #include "stdafx.h"
    #include<iostream>
    #include<iterator>
    #include<conio.h>
    #include<list>
    #include<algorithm>
    using namespace ::std;
    struct link
    {
    	int data;                                   //data item
    	link* next;                                // pointer to link
    };
    class linklist                                 //list of links
    {
    	link* head;
    public:
    	linklist()
    	{
    		head = NULL;
    	}
    
    	void additem(int d)
    	{
    		link* temp = new link;                    // make a new link
    		temp->data = d;                           // give it data 
    		temp->next = head;
    		head = temp;
    	}
    };
    
    int main()
    {
    	linklist l1;
    	
    	list<int> l;      // l to be list container
    	ostream_iterator<int>screen(cout, " ");
    	l1.additem(1);
    	l1.additem(2);
    	l1.additem(3);
    	copy(l.begin(), l.end(), screen); 
    	cout << endl;
    	
    	_getch();
    	return 0;
    }
    above code is not giving any output.
    Can anybody help me ???
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Is this a homework assignment?

    If so, you should write your own linked list so you understand how the linked list structure works.

    Later, you should only use the STL list template since it would duplicate the code you wrote yourself. One f the problems in C++ is that everyone writes the same code over and over and a lot of it doesn't work very well. By using the STL template, everyone's code would work and work the same way.

    As far as your code goes, I see the linked list class. YOu should have pointers to the start of the list, the end of the list, and your current position within the list. The link (commonly called a Node) should have a pointer to the previous node. You can use this pointer to read the list backwards.

    But I don't know if you assignment allows you to do this.

    Comment

    • tushu
      New Member
      • May 2015
      • 9

      #3
      This is not a homework problem. I have done above program in c++, so logic is not the problem. I just want to learn STL feature in Data Structure. For that I took simple example.
      In next month I am having interview , for that my favourite subject is Algorithm. So I am trying each and every program in C++. ( BFS , DFS etc... ) While doing this I encountered with STL feature. So I thought if I use STL along with my C++ program , I will look different and it will help me in future. So I am trying to learn STL for Data Structure. So I started with easy program but I stucked.
      So here logic is not important , just how to use STL in my linkedlist class.

      Comment

      • tushu
        New Member
        • May 2015
        • 9

        #4
        There is no output for this program so I think , linked list is being created but it is not getting printed because to create linked list I have used OOP but for printing I have used STL.
        So how to match both , that is the problem.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          There is no doubt the STL will make you look better. However, the templates are a complete family with relationships to each other. They were not designed to be used piecemeal.

          I suggest you write your algorithms as a separate effort from learning the STL.

          Iterating reverse list in C++ STL looks like:

          Code:
          rlist<data> thelist;
          //add data to the list here
          rlist<data>::reverse_iterator itr = thelist.rbegin();
          
          while (itr != thelist.rend())
          {
               cout << data ;
               ++itr;
          }
          This looks different from your code because all of your code is part of the STL templates. So by using the STL you get results fast using debugged C++ code.

          However, you have to play by STL rules. Like that iterator. What is that? Also, iterating a list forward to see it listed backward?? And rbegin() and rend()???

          Truly learn the STL but take the tine to learn it in an orderly fashion.
          Last edited by weaknessforcats; May 13 '15, 01:56 AM.

          Comment

          • tushu
            New Member
            • May 2015
            • 9

            #6
            Thank you friend, I think I got your point. We can not use STL in broken pieces. ( i.e in program , half part is with STL and half is with normal OOP ) But still confused that , when to use STL and when , not. In between I encountered following link.

            Here author is saying , when to use STL .. But why only with such type of problem ?? Means , are these big problem ?? or any other reason.... ( I have not tried such problems yet ) Can you help me in understanding this ??
            Thank you.

            Comment

            • tushu
              New Member
              • May 2015
              • 9

              #7
              @ weaknessforcats : Please read my above post and reply me.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                There are two things here:

                1) Do you know what a linked list is? How it is structured? Can you code one?

                2) Can you use the STL implementation of the linked list?

                On a job interview, using the STL alone may hide the fact you don't know how the structure works because the STL templates have hidden all the messy details. So using an STL example in your interview does not show you know what you are doing.

                Everyone I know has learned how to write these things from scratch and then has learned the STL. There's also the fact that your interviewers may not have learned the STL themselves and may feel put down. You just don't know in job interviews.

                Comment

                Working...