Topic: Type problem, using STL
Author: Rich Paul <linguist@cyberspy.com>
Date: 1996/06/24 Raw View
I find the I use alot of things like this:
list<int> IntList;
...
// lots and lots of code like this:
{
list<int>::iterator first, last;
for ( first = IntList.begin(), last = IntList.end();
first!=last;
first++ )
{
// Do something worth the effort here;
};
};
Now if I decide to change the type of the list to
vector<int> IntList; // poor name for it now, but oh, well
I have to find every list<int>::iterator and change it to
vector<int>::iterator.
This is a headeach, it would be much nicer to do this:
{
IntList.iterator first, last; // access type member as member
for ( first = IntList.begin(), last = IntList.end();
first!= last;
first++ )
{
// Do something worth the effort here <G>
};
};
Now I realize that a type member if effectively static ( shared by every
instance of the type ) and therefore it is odd to be able to address it
like this. None the less, I think it's kind of important to be able to
use things regardless of type.
Now there is another option:
typedef list<int> int_list_type;
typedef int_list_type::iterator int_list_iterator;
now, you can make a single change in the int list type, and all will
work. However, it leaves you making a typedef for each VARIABLE type, if
you don't want to be stuck making EVERYTHING a vector or EVERYTHING a
list.
What do ya'll think?
--
#include <legalbs/standarddisclaimer>
Rich Paul | If you like what I say, tell my
C++, OOD, OOA, OOP, | employer, but if you don't,
OOPs, I forgot one ... | don't blame them. ;->
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: austern@mti.sgi.com (Matt Austern)
Date: 1996/06/24 Raw View
In article <31CE9F84.1520@cyberspy.com> Rich Paul <linguist@cyberspy.com> writes:
> list<int> IntList;
> ...
> // lots and lots of code like this:
> {
> list<int>::iterator first, last;
> for ( first = IntList.begin(), last = IntList.end();
> first!=last;
> first++ )
> {
> // Do something worth the effort here;
> };
> };
>
> Now if I decide to change the type of the list to
>
> vector<int> IntList; // poor name for it now, but oh, well
>
> I have to find every list<int>::iterator and change it to
> vector<int>::iterator.
One of the important things to keep in mind if you're using the STL is
that if you've written two functions that are identical except for a
type, you could probably have just written a single generic function
instead.
Generic algorithms using containers are less common than generic
algorithms using iterators, but they're still completely legitimate.
You have to be a bit careful, since vectors and lists have somewhat
different interfaces and provide different complexity guarantees,
but you'll be OK if you stick to operations guaranteed in the sequence
requirements.
For example:
template<class Sequence>
void do_something(Sequence& seq)
{
Sequence::iterator first, last;
for ( first = seq.begin(), last = seq.end();
first != last;
++first)
{
/* Do something. */
}
}
--
Matt Austern
SGI: MTI Compilers Group
austern@isolde.mti.sgi.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]