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?
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 usingnamespace 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?
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
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?
#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:
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.
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.
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.