[LC++]STL Iterators and integers
mark at austrics.com.au
Fri Aug 3 19:41:36 UTC 2001
Grzegorz Mazur wrote:
Thanks for your reply! Very useful.
> > vector<string>::iterator it(wiseSayings.start());
True. My mistake.
> > for (unsigned int i=0; i<wiseSayings.size(); ++i)
> > cout<<i<<": "<<wiseSayings[i]<<endl;
> > which is much quicker to write, but
> > probably inefficient speed wise, because
> > the address of wiseSayings[i] needs to be
> > calculated at each step. Unless of course
> Which basically means _one_ addition. Not that bad, compared to the total
> number of instructions being executed in the loop. Especially I/O is
> pretty expensive and will dominate the rest.
Doesn't it mean one addition _and_ one multiplication? Ie,
address = base_address + i*sizeof(string)
You are right though, about the I/O being expensive
comparatively. The example I gave was to illustrate
the idea. I'm sure I could find an example where the
address calculation expense was significant.
> > cout<<(unsigned int) it<<": "<<*it<<endl;
> > For this to work, the conversion (unsigned int) it
> > would need to extract an integral value, this
> > being the distance from wiseSayings.start() that
> > it has travelled. Ie a count of the number of
> > increments applied to it. Such a feature would
> > mean we could do away with defining a separate
> > count variable.
> Which would mean that everybody, even those who don't need such a feature,
> would need to pay for it. So it's highly unlikely to be ever done.
Well you are right that if it were implemented as
an extra count variable as part of the iterator, that
it would be expensive for people who didn't use it.
A function that calculates it on the fly however,
would hold no expense for those who didn't use it.
> > Is there some facility for doing something like
> > this with STL iterators? Perhaps it would be
> > accessed via a member function, it.count() or
> > something. I have not been able to find anything
> > like this and was wondering if some such
> > thing existed.
> In case of vector, std::distance() should do. (It's quite efficient for
> ramndom access iterators.)
Okay, thanks! So what I would have is:
for (vector<string>::iterator it(wiseSayings.begin());
I've realized another solution is:
I'm not sure which one would be more efficient. Possibly
they are the same thing???
> Anyway, the cost of calling distance, althougs
> small, will be probably comparable to the cost of other solutions you've
I suspect you are right. It would require one subtraction and
one division I suspect.
One other question, on a different tack, is as follows.
When I setup my vector of strings I do the following:
wiseSayings.push_back("eat more chocolate");
wiseSayings.push_back("smell the roses");
wiseSayings.push_back("laugh a little");
wiseSayings.push_back("keep it simple");
What would be really nice, is if I could write
the above as:
vector<string> wiseSayings = "eat more chocolate", "smell the roses",
"laugh a little", "keep it simple";
Unfortunately, it doesn't work. Is there any way of
doing something like this?
Thanks again for your input!
More information about the tuxCPProgramming