Solved

STL Question

Posted on 2001-09-13
17
282 Views
Last Modified: 2013-12-14
I need to add 4 insertion methods to the STL for list.  What I would like to know is whether or not using a generic doubly linked list file with pointers to its head and tail which a 3rd party has written would be considered to be using STL, or do I have to modify the list file which came with my compiler ( Visual C++ 6.0 )?  If I don't have to use the list which came with my compiler can someone point me to a clearly implemented doubly linked list?
0
Comment
Question by:nidan
  • 7
  • 6
  • 2
  • +1
17 Comments
 
LVL 5

Expert Comment

by:proskig
ID: 6479971
I would recommend just derive (or use aggregation - depends on your needs) your class from std::list and add there your insertion functions.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6480043
>>clearly implemented doubly linked list?
Use std::deque or std::vector class.
These can be found in the following header:

#include <deque>
#include <vector>
0
 
LVL 30

Expert Comment

by:Axter
ID: 6480055
>>I need to add 4 insertion methods to the STL for list
Why do you need to add 4 insertion methods?  Is this homework?


>>What I would like to know is whether or not
>>using a generic doubly linked list file with pointers to
>>its head and tail which a 3rd party has written
>>would be considered to be using STL

This sounds like you're trying to make the std::list class work like an std::vector class.  If you need these types of features, you should use an std::vector class instead.

0
 
LVL 4

Expert Comment

by:IainHere
ID: 6480102
I was starting to write an answer, but was called away, and it seems I'm a bit late.  Consider this background :-)

Firstly, don't modify the list file that came with the compiler. That would be a Bad Idea.  The STL has behaviours as defined by the C++ standard, but each implementor is free to code it as they see fit.  As long as they are coded to the standard, and you use them correctly, they would in theory be interchangable (ie you could get your third party STL and use it with VC++, and it would still be "using STL").  However, unless you really know what you're doing, you should stick with the one provided, since it's optimized for the compiler.

Also, don't derive from STL containers.  By all means aggregate, but the STL was not designed to be derived from.

To reiterate Axter's comment:  Don't reinvent - reuse.
0
 
LVL 1

Author Comment

by:nidan
ID: 6480111
This is for an assignment.  Using the STL for list I need to add 4 insertion methods.  I have a generic doubly linked list from my textbook however, I don't know if that's considered to be STL.  I looked at the microsoft list file and it's hard to understand the code.  I guess maybe I should derive a new class from std::list like proskiq said instead of directly adding my insertion methods directly into the microsoft file.  
0
 
LVL 5

Expert Comment

by:proskig
ID: 6480131
>By all means aggregate

As I said before "it depends on your needs". If you aggregate you have to provide a lot of functions. How's about all iterators? If you want to use all advantages of STL and be able to switch between containers, use algorithms, etc, then your class (container) shall provide all! functions underlying class provides.
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6480203
Sorry proskig, you're right.  I was attempting (albeit too strongly worded) to warn nidan against superfluous derivation from STL containers, which was something I was guilty of when I started out.  It is a mistake that can be easily made, because it is normally a lot quicker to derive then aggregate (as you point out).  It is quite often the wrong choice, and a beginner will often miss the fact that they've contravened the Is-A rule.

nidan:  If this is an assignment, are you sure that it will be acceptable to reuse someone else's code?  Normally the assignment would be something along the lines of "implement this container to live by the rules of the STL".  What you're answering seems to be "get someone else's code to do these extra things".
0
 
LVL 30

Expert Comment

by:Axter
ID: 6480362
IainHere,
Why do you think it is a mistake to derive from an STL class?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 30

Expert Comment

by:Axter
ID: 6480386
nidan,
I could be mistaken, but it sounds like your instructor wants you to create a class that is derived from std::list, and add 4 methods to your derived class.

Is this correct?

Something like the following:

template<class T>
class foo : public std::list<T>
{
public:
T someMethod1(const T &src);
T someMethod2(void);
void someMethod3(const T &src);
T someMethod4(const T &src, int);
};

IMHO: I would not consider it STL unless you're using some type of template code.
0
 
LVL 1

Author Comment

by:nidan
ID: 6480668
Yes, I'm going to create a class that is derived from std::list.  I need to add 4 of the following 6 methods to my derived class.  Will this be possible?

Front: If an item is not already present in the list, put it at the front, else ignore it.

Rear: If an item is not already present in the list, put it at the rear, else ignore it.

Move-to-front: If an item is already present in the list, move it to the front, else add it to the rear.

Transpose: If an item is already present in the list, interchange it with the item immediately before it, else add it to the rear..

Count: If an item is already present in the list, order list items by the frequency with which they have occurred, most frequent first, else add it to the rear.

Order: Maintain the list in ascending (numeric or alphabetical) order.
0
 
LVL 30

Accepted Solution

by:
Axter earned 70 total points
ID: 6480750
>>Will this be possible?
Yes, they're possible.  Which one do you want to do?

You should decide which of the four you want to create.  Then try doing some of the code, and if you run into problems with it, post a specific question to the problem you're having with the code.

According to EE policy, we're not allowed to do homework questions, but we can assist you with code that you have already created.

I'm sure once you're able to do one of the above functions, the other three will be easy to do.
0
 
LVL 1

Author Comment

by:nidan
ID: 6480766
Thanks for your help axter.  If I run into any problems I will post another question.
0
 
LVL 1

Author Comment

by:nidan
ID: 6480827
Thanks for your help axter.  If I run into any problems I will post another question.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6480850
>>Thanks for your help axter.  If I run into any problems
>>I will post another question.
If you have problems with this class, feel free to post it here.  If it's simple, I will not mine providing additional assistance in this same question.
0
 
LVL 1

Author Comment

by:nidan
ID: 6481143
I am working on the first 4 insertion methods:
Front: If an item is not already present in the list, put it at the front, else ignore it.

Rear: If an item is not already present in the list, put it at the rear, else ignore it.

Move-to-front: If an item is already present in the list, move it to the front, else add it to the rear.

Transpose: If an item is already present in the list, interchange it with the item immediately before
it, else add it to the rear..

I'm also adding a boolean member function isInList() because all these methods require that you find out whether the item is in the list or not.  My code always returns true though:

template <class T>
bool MyList<T>::isInList(const T item)
{
     const_iterator it;
     it=begin();
     while(it!=end())
     {
          if (*it == item) // is this the problem?
               return true;
          it++;
     }
}
0
 
LVL 1

Author Comment

by:nidan
ID: 6481150
I forgot to mention that this works with ints, but not strings.  Also when I use strings I get several warnings.  So my question is how come it's not working for the string data type?
0
 
LVL 1

Author Comment

by:nidan
ID: 6481167
The only strings it works with are char*.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now