473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

What is the correct type for iterating through vector?

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
Jul 22 '05 #1
6 1287
Michael wrote:
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);
Jul 22 '05 #2
> 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
Jul 22 '05 #3
> 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
Jul 22 '05 #4
"Michael" <sl***********@hotmail.com> wrote in message news:<co**********@titan.btinternet.com>...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #5

"Michael" <sl***********@hotmail.com> wrote in message news:co**********@titan.btinternet.com...
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.
Jul 22 '05 #6
Michael wrote:
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?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tomas Ukkonen | last post by:
Hi I'm currently trying to make my old code to compile with new gcc 3.4.0. It compiles correctly in 3.3.3 so I'm not sure if this is compiler bug or not. Code very close to a example below...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
14
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
5
by: RocTheEngy | last post by:
Greetings c.l.c... I am trying to understand some structure definitions in working code (e.g. compiles, runs & produces expected results) that I have. I think I've trimmed the code to what is...
16
by: utab | last post by:
Dear all, In programming terminology, what is a wrapper and where is it used? Regards
5
by: Martin Jørgensen | last post by:
Hi, The piece of code I'm struggling with is so simple, that I hope nobody wants a complete example for answering the question: -------- string color_line; int data_type = 0; for(...
5
by: Alan | last post by:
I was wondering whether it is good programming practice or asking for trouble to modify a vector while iterating through it. That is, I want to do something like the following pseudocode in C++: ...
4
by: Vince C. | last post by:
Hi all. I can see the standard template library has changed a lot since I begun C++ 10 years ago... Now I'm facing with outputing the members of a struct, say Option, which contains 2 members...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.