Hi, i am currently learning vectors, pretty easy. But i have a question in this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <vector>
usingnamespace std;
int main()
{
vector<double> student_marks(2);
for (vector<double>::size_type i = 0; i < 2; i++)
{
cout << "Enter marks for student #" << i+1 << ": " << flush;
cin >> student_marks[i];
}
}
There is (vector<double>::size_type i = 0; i < 2; i++)
What is :: for? i know its the BSRO and is size_type a c++ keyword? or just one that was made up by whoever made the example? when i remove the Scope Operator i get this error:
C:\Users\Chay Hawk\Desktop\Vector\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp|10|error: expected initializer before 'i'|
C:\Users\Chay Hawk\Desktop\Vector\main.cpp|10|error: 'i' was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings ===|
vector<double>::size_type is just a data type. the :: is called the scope resolution operator, but in plain english, you could translate :: into "belongs to". So that it's a way of saying "size_type belongs to vector<double>"
(As with a lot of things in C++, it makes more sense when you read your code 'backwards' from right-to-left).
So this bit of code vector<double>::size_type i = 0;
is doing nothing more than defining a variable called 'i' and initialising it with a value of 0.
(and that data type is a typedef for size_t, which in turn is "usually" a typedef for unsigned long. So after all that 'i' is just a boring old unsigned integer variable!)