So what is a vector?
It’s an array with that difference that we can change it’s size during the time that the program is functioning
Let’s show a short program with it:
- #include <vector>
- include <iostream>
- using namespace std;
- int main()
- {
- vector<int> example;
- for(int i=0;int i<=6;++i)
- {
- example.push_back(i);
- cout<<example[i]<<endl;
- }
- getchar();
- return 0;
- }
First we include a file at the top to be able to creat vector arrays
The next part you don’t understand is probably this:
vector<int> example;
Well it’s actually really simple. In this line we define a vector with int values
What is the size?
Well actually there is no size because the vector allocates it’s memory dynamicly that means it can change it’s memory while the program is running and that is what makes it so nifty
The next part you don’t understand is probably this:
for(int i=0;int i<=6;++i)
{example.push_back(i);
cout<<example[i]<<endl;
}
What does this code do?
Well if you don’t know the concept of loops it might me strange(I’ll explain them in my next tutorial) but basicly it repeats itself 7 times until the i value is bigger than 7 and while it repeats itself it does:
example.push_back(i);
cout<<example[i]<<endl;
First it insert’s the value of i into the table(at the very and of the vector array) and then it print’s it on the screen
Well I hope I explained the problem
greetings patryksharks321