What is the correct type for iterating through vector?

M

Michael

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
 
R

Ron Natalie

Michael said:
Should i write:
for(size_t i=0;i<vec.size();i++) doSumfink();
or
for(int i=0;i<vec.size();i++) doSumfink();

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);
 
J

Jonathan Mcdougall

Should i write:
for(size_t i=0;i<vec.size();i++) doSumfink();
or
for(int i=0;i<vec.size();i++) doSumfink();

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
 
J

Jonathan Mcdougall

Should i write:
for(size_t i=0;i<vec.size();i++) doSumfink();
or
for(int i=0;i<vec.size();i++) doSumfink();


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
 
J

Jerry Coffin

Michael said:
Should i write:
for(size_t i=0;i<vec.size();i++) doSumfink();
or
for(int i=0;i<vec.size();i++) doSumfink();

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.
 
D

Duane Hebert

Michael said:
Should i write:
for(size_t i=0;i<vec.size();i++) doSumfink();
or
for(int i=0;i<vec.size();i++) doSumfink();

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.
 
I

Ioannis Vranos

Michael said:
Should i write:
for(size_t i=0;i<vec.size();i++) doSumfink();
or
for(int i=0;i<vec.size();i++) doSumfink();

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top