[LC++]STL Iterators and integers
Mark Phillips
mark at austrics.com.au
Tue Aug 7 12:10:12 UTC 2001
Grzegorz Mazur wrote:
> > Yes this is okay for my example, but I am
> > wanting something which will work more
> > generally. I tried a variant on your approach
> > based on a class:
>
> What kind of generality does the above example lack?
You may have a class that contains, as part of it, a vector
of strings, but also has other things associated with it.
The "array of strings" approach isn't general enough
because it isn't a class. What would be nice is a class
constructor which can take a hardwired list of strings
as an argument. One way would be to create a hardwired
array of strings, and then pass this array variable into
the constructor. But this is a two-step process with an
unnecessary variable declaration --- it would be much
cleaner and nicer if this could be done in a single
hard-wired step.
> > class wiseSayingsTy: public vector<string> {
> > public:
> > wiseSayingsTy(): vector<string>() {}
> > wiseSayingsTy(uint N, char const* initList[]): vector<string>() {
> > for (int i=0; i<N; i++)
> > push_back(initList[i]);
> > }
> > };
>
> I'm quite sure, that similar (at least functionally) constructor is
> already defined for vectors. So there is no point in defining it once
> again. And deriving from vector is not the best idea anyway.
I have been hoping this functionality was possible, but
have not yet discovered how to do it. The docs for vector
list the following constructors:
vector() Creates an empty vector.
vector(size_type n) Creates a vector with n elements.
vector(size_type n, const T& t) Creates a vector with n copies of t.
vector(const vector&) The copy constructor.
template <class InputIterator>
vector(InputIterator, InputIterator) Creates a vector with a copy of a range.
This last one is the only one I have had any hope
might do what I want. But I didn't think it did.
However, your comment:
> 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.
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.
> > 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.
Cheers,
Mark.
More information about the tuxCPProgramming
mailing list