int vs unsigned int

With the following code I get a warning that I am comparing signed and unsigned int.
1
2
3
vector<float> test[10];

for (int i=0; i<test.size(); i++){...} 


On the topic I have found this (http://www.cplusplus.com/forum/beginner/8529/) which I think is quite ok, however the last two replies have confused me a bit.

In the case of loops, it is better to make sure that i is of the same type as SIZE. In the case of std containers use the appropriate size_type defined by the container rather than int or unsigned int.


How do I find the type in question?
What is it for a vector?

Thanks,
Ben
http://www.cplusplus.com/reference/stl/vector/
Scroll down to Member Types to see size_type.

It is used as such, in your example:
for (std::vector<float>::size_type i=0; i < test.size(); ++i);

You may omit adding the std:: if you have using namespace std;.

If you use C++11 (the newest standard of C++, late 2011), you can simplify this loop greatly:
for (float &f: test);
You compiler might not support the so-called range-based for() loop, though.
Thanks Catfish,
the first version works fine for me. But why do I have specify what type the vector members have? Shouldn't the size always be the same type?

Regarding the simplified loop I guess you are right and my compiler is not up to it. In any case apart from that I also don't quite understand it. I would assume it gets the type from the used vector "test", but that should also mean that it knows that the vector contains floats, right?
Would I have to use 'f' in your example (e.g. in test[f])?
What is the '&' in this case?

Thanks,
Ben
But why do I have specify what type the vector members have?

Because std::vector is a template, and it needs that information to generate the actual code.

Shouldn't the size always be the same type?

I think it probably is, in practice. I could compile that loop with std::vector<bool>::size_type instead, without an error being issued.

As for the range-based for loop... the f takes the value of each element in the vector.
The & means that f is a reference: this means that when you change f inside the loop, it will change in the vector as well.
1
2
3
4
5
for (float &f: test)
    f = 33.0f; // update all values to 33.0f

for (float f: test)
    std::cout << f << std::endl; // print all values from test 

Last edited on
Ok, so let me see if I got this right, but first one new question. In f = 33.0f, what does the second 'f' stand for, and why can it be written to a float?

Now to the understanding part.
This means in fact if my verctor holds a struct I could do the following?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "std_lib_facilities.h"

struct testStruct{
   // Definition of struct members
   float a;
   int b;
   string c;
};

int main(){
vector<testStruct> test;
testStruct temp;

// adding some more values to the vector
for (int i=0; i<3; i++){
    temp.a = i*2.4;
    temp.b = i;
    temp.c = "someting";
    test.push_back(temp);
}

for (testStruct &f: test){
   f.a = f.a+3;
   f.b = f.b+2;
   f.c = f.c+" and "+f.c // i know that is kind of not so ....
}

}


So, if I assume that the above is right (as I cant test it). How would I implement something where I try to access the previous value? Something like:
 
if(i>0) test[i].a = test[i-1]+3;


Thanks!
In f = 33.0f, what does the second 'f' stand for, and why can it be written to a float?


33 is a simple value, and is by default treated as an int.
Therefore I added .0f to make sure it will be treated as a float.
I personally consider it to be good practice but it's not always needed to be done.
So the f doesn't get written anywhere, it's just convention.

Read more here: http://www.cplusplus.com/doc/tutorial/constants/

So, if I assume that the above is right (as I cant test it). How would I implement something where I try to access the previous value?

It compiles just fine (after editing for the includes of course, and adding the missing semicolon).
The previous value? No, in that case a range-based for loop is not what you should use. You're better off using a regular for loop.
Last edited on
Thank a lot for the help! I think this has answered all my questions.
Tusend Tak (danish for 1000 thanks - something they say here)
Last edited on
Good luck with C++11, if you go that way.
Thanks, but for now I will stick with what I have. I have a fast approaching deadline on the project. But maybe for future.
So from your point of view, is it a blessing or rather not?
Blessing, mostly. It has many usability improvements, like the special for loop, automatic type deduction, and initializer lists, which would allow you to initialize your float vector like this:
vector<float> test = {0.4f, 33.0f, 144.54f};

In spite of this, I'd rather divert you to the D language.
Topic archived. No new replies allowed.