You must be getting this error
error: ‘i’ was not declared in this scope
cout << vect_name[i] <<endl;
^
Enclose the statements in parentheses to indicate loop body. Without the braces, only one following statement is taken as loop body.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vect_name;
for (int i = 10; i < 21; i = i + 2) {
vect_name.push_back(i);
cout << vect_name[i] << endl; // Will print zeroes
}
return 0;
}
However this will still give wrong results because arrays/vectors are 0 indexed in C++. You need to print in a separate loop.
A correct version of code will look like
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v(6, 10);
for (int i = 0; i < v.size(); i++) {
v[i] += (i * 2);
cout << v[i] <<endl;
}
return 0;
}
Output
10
12
14
16
18
20