amirteymuri amirteymuri - 8 months ago 58
C++ Question

Adding items to vector using for loop in C++

New to C++ here. I want to add elements to a vector using a

loop. Why is this not possible. The following code prints nothing:

#include <iostream>
#include <vector>

int main(){

std::vector<int> my_vec;

for(int i = 10; i < 10; ++i){

for(std::vector<int>::iterator my_iter = my_vec.begin(); my_iter !=
my_vec.end(); ++my_iter){
std::cout << *my_iter << " ";

return 0;


Ok, i was a bit sleepy! It was a typing problem.


Your for loop construction has an error. The initializer should be i = 0 rather than i = 10

  for(int i = **10**; i < 10; ++i){

A key part of learning C++ is learning to use the debugger. Setting break points in your code and walking through until you discover the problem is a critical skill. C++ is a complex language and difficult to debug by inspection by learning to use a debugger will save you a lot of time and misery.