|
-
January 19th, 2007, 11:21 AM
#1
why is this for loop not working?
Hello all,
I have the following for loop. When size() returns a 0, the for condition should be -1 and it should not go into the for loop, correct?
for(vector<CNodeData*>::size_type i = 0; i < GetInputRVRs().size() - 1; i++)
{
for some reason when size() returns 0, the for loop is still executed. What am I doing wrong here?
Thanks!
-
January 19th, 2007, 11:34 AM
#2
Re: why is this for loop not working?
size_type is actually an unsigned int. When the size is 0, you are testing "i < -1". Unfortunately, since it's an unsigned in, "-1" is treated as INT_MAX which means you're actually testing "i < INT_MAX" -> hence it runs a ridiculous number of times!
What you actually want to test for is:
for(vector<CNodeData*>::size_type i = 0; i < GetInputRVRs().size(); i++)
Note the lack of the "- 1". Convince yourself that this is correct. Alternatively, you can do:
for(vector<CNodeData*>::size_type i = 0; i <= GetInputRVRs().size() - 1; i++)
-
January 19th, 2007, 11:54 AM
#3
Re: why is this for loop not working?
 Originally Posted by panzer2k4
size_type is actually an unsigned int. When the size is 0, you are testing "i < -1". Unfortunately, since it's an unsigned in, "-1" is treated as INT_MAX which means you're actually testing "i < INT_MAX" -> hence it runs a ridiculous number of times!
What you actually want to test for is:
for(vector<CNodeData*>::size_type i = 0; i < GetInputRVRs().size(); i++)
Note the lack of the "- 1". Convince yourself that this is correct. Alternatively, you can do:
for(vector<CNodeData*>::size_type i = 0; i <= GetInputRVRs().size() - 1; i++)
That´s true if he wants to iterate over all elements in the vector. Maybe he wants to leave out the last one, so he added the -1 to the code. Nevertheless, an excellent clue!
Regards,
Guido
- Guido
-
January 19th, 2007, 05:08 PM
#4
Re: why is this for loop not working?
 Originally Posted by GNiewerth
That´s true if he wants to iterate over all elements in the vector. Maybe he wants to leave out the last one, so he added the -1 to the code. Nevertheless, an excellent clue!
Regards,
Guido
If that's the case, then:
Code:
vector<CNodeData*>::iterator endIter = GetInputRVRs().end();
--endIter;
for (vector<CNodeData*>::iterator iter = GetInputRVRs().begin(); iter != endIter; ++iter)
{
...
}
Or:
Code:
vector<CNodeData*>::reverse_iterator iter = GetInputRVRs().rbegin();
++iter;
for (; iter != GetInputRVRs().rend(); ++iter)
{
...
}

Viggy
-
January 19th, 2007, 05:42 PM
#5
Re: why is this for loop not working?
 Originally Posted by panzer2k4
What you actually want to test for is:
for(vector<CNodeData*>::size_type i = 0; i < GetInputRVRs().size(); i++)
Exactly.
 Originally Posted by panzer2k4
Alternatively, you can do:
for(vector<CNodeData*>::size_type i = 0; i <= GetInputRVRs().size() - 1; i++)
Doesn't that just cause the same problem?
If size == 0, i <= 0xFFFFffff will still not work.
Also, wouldn't Viggy's code malfunction if size == 0 ?
-
January 20th, 2007, 01:14 PM
#6
Re: why is this for loop not working?
 Originally Posted by Zaccheus
Doesn't that just cause the same problem? If size == 0, i <= 0xFFFFffff will still not work. Also, wouldn't Viggy's code malfunction if size == 0 ?Also, wouldn't Viggy's code malfunction if size == 0 ?
Yes, you are correct, in both cases...
Alternatively, can use for_each.. somewhat like this:
Code:
#include<vector>
#include<algorithm>
template<typename T>
struct DoSomething{
void operator()(const T& t) const
{
//do something with t
}
void operator()(T& t) const
{
//do something with t
}
};
int main()
{
const std::vector<int> vec;
std::vector<int> vec2;
//fill vec/vec2 or do whatever
std::for_each(vec.begin(), vec.end(), DoSomething<int>());
std::for_each(vec2.begin(), vec2.end(), DoSomething<int>());
}
I considered that the last element does not need any special treatment... if that's the case the above can be put inside an if-block if(size>0) and then used (of course with for_each working till .end()-1). Hope it helps...
Last edited by exterminator; January 20th, 2007 at 01:22 PM.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|