[LC++]STL Iterators and integers

Grzegorz Mazur mazur at chemia.uj.edu.pl
Tue Aug 7 19:35:05 UTC 2001


Hey,

On Tue, 7 Aug 2001, Mark Phillips wrote:

> > std::size_t no_saings = sizeof sayings / sizeof sayings[0];
> >
> > This way you have all the necessary information to create a pair of
> > iterators giving you full power of STL.
> 
> leads me to wonder whether maybe there is a way!
> But I haven't worked out how yet.

vector<string> v(sayings, sayings + no_sayings);

> Regarding your comment that deriving from vector
> is not the best idea...
> 
> Perhaps you're right, but I'm not convinced of this.
> If a vector with extra functionality is what you
> want, then I don't see why you shouldn't do this.
> Also, the C++ programming books I have,
> "Thinking in C++", volumes I and II, by Bruce
> Eckel, say that  inheriting from STL classes _is_
> often a good idea as it means you can use STL
> algorithms on the derived classes.  Perhaps there
> are good reasons not to inherit a vector, but I
> haven't come across any yet.

There's one important reason to avoid deriving classes from STL containers
- AFAIK no STL container defines virtual destructor. This basically means
that you can't ensure proper destruction of such a derived container.
Unfortunately, I haven't read Thinking in C++, so I can't really comment
on that.

> > > Then I hoped I could write:
> > >
> > >   wiseSayingsTy wiseSayings(4, {"eat more chocolate", "smell the roses",
> > >     "laugh a little", "keep it simple"});
> >
> > Perhaps a book covering some C++ basics would help?
> 
> The Bruce Eckel books have been very helpful.  I haven't
> yet come across any way of doing something like the
> above, but I am still hopeful that maybe there is a way.

If you are _really_ desperate, there is a method of initializing
containers (but not the STL ones, you have to write some additional
interface on your own) which gives you syntax very similar to
initialization of built-in arrays described in a paper "Techniques for
Scientific C++" by Todd Veldhuizen (unfortunately, don't have the link
handy, so you'll have to use some search engine).

--
Grzesiek





More information about the tuxCPProgramming mailing list