Solved

STL Question

Posted on 2001-09-13
17
283 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 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

13 Experts available now in Live!

Get 1:1 Help Now