Connecting Tech Pros Worldwide Forums | Help | Site Map

What is the correct type for iterating through vector?

Michael
Guest
 
Posts: n/a
#1: Jul 22 '05
Should i write:
for(size_t i=0;i<vec.size();i++) doSumfink();
or
for(int i=0;i<vec.size();i++) doSumfink();

fanks
mike



Ron Natalie
Guest
 
Posts: n/a
#2: Jul 22 '05

re: What is the correct type for iterating through vector?


Michael wrote:[color=blue]
> Should i write:
> for(size_t i=0;i<vec.size();i++) doSumfink();
> or
> for(int i=0;i<vec.size();i++) doSumfink();
>[/color]

That all depends on what "vec" is.

If vec is std::vector, then the type for i would probalby
be better to be veotor::size_type.

Of course, since you do nothing with the index, why not
compute the size once?
for(int i = vec.size(); i > 0; --i) doSumfink();

if you are going to look at each element of the vector:
for(vector<t>::iterator i = vec.begin(); i != vec.end(); ++i) doSumfink(i);


Jonathan Mcdougall
Guest
 
Posts: n/a
#3: Jul 22 '05

re: What is the correct type for iterating through vector?


> Should i write:[color=blue]
> for(size_t i=0;i<vec.size();i++) doSumfink();
> or
> for(int i=0;i<vec.size();i++) doSumfink();[/color]

std::vector<int> v;
for (std::vector<int>::iterator itor=v.begin(); itor!=v.end(); ++itor)

But if you're asking if it's alright not to use the same type size()
returns, I think you've already answered your question.


Jonathan
Jonathan Mcdougall
Guest
 
Posts: n/a
#4: Jul 22 '05

re: What is the correct type for iterating through vector?


> Should i write:[color=blue]
> for(size_t i=0;i<vec.size();i++) doSumfink();
> or
> for(int i=0;i<vec.size();i++) doSumfink();[/color]


std::vector<int> v;
for (std::vector<int>::iterator itor=v.begin(); itor!=v.end(); ++itor)

But if you're asking if it's alright not to use the same type size()
returns, I think you've already answered your question.


Jonathan
Jerry Coffin
Guest
 
Posts: n/a
#5: Jul 22 '05

re: What is the correct type for iterating through vector?


"Michael" <slick_mick_00@hotmail.com> wrote in message news:<coabdj$804$1@titan.btinternet.com>...[color=blue]
> Should i write:
> for(size_t i=0;i<vec.size();i++) doSumfink();
> or
> for(int i=0;i<vec.size();i++) doSumfink();[/color]

Neither of the above, at least as a general rule.

I'm going to guess that vec is a container and that you really want
doSumfink to operate on the items in that container. In that case, I'd
recommend something like:

std::for_each(vec.begin(), vec.end(), doSumfink);

However, I feel obliged to add that I think for_each is over-used --
I'd say the majority of times I've seen it used, something else would
have worked better. OTOH, you haven't told us enough to indicate using
anything else either.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Duane Hebert
Guest
 
Posts: n/a
#6: Jul 22 '05

re: What is the correct type for iterating through vector?



"Michael" <slick_mick_00@hotmail.com> wrote in message news:coabdj$804$1@titan.btinternet.com...[color=blue]
> Should i write:
> for(size_t i=0;i<vec.size();i++) doSumfink();
> or
> for(int i=0;i<vec.size();i++) doSumfink();[/color]

comparing signed and unsigned values throws warnings
with many compilers.

I would write for(size_t i = 0, len = vec.size(); i < len; ++i)...
or use an iterator.


Ioannis Vranos
Guest
 
Posts: n/a
#7: Jul 22 '05

re: What is the correct type for iterating through vector?


Michael wrote:[color=blue]
> Should i write:
> for(size_t i=0;i<vec.size();i++) doSumfink();
> or
> for(int i=0;i<vec.size();i++) doSumfink();[/color]

The return type of size() is vector::size_type which is an unsigned
integer type.


So for example for a vector of ints, the proper form is:

for(vector<int>::size_type i=0; i<vec.size(); ++i)
// ...


If you are sure that your vector size cannot grow larger than
numeric_limits<size_t>::max() then you can use size_t, or any other type
in this style.


However why not use the real one?



--
Ioannis Vranos

http://www23.brinkster.com/noicys
Closed Thread