Iterate through a C++ Vector using a 'for' loop

后端 未结 12 1715
天命终不由人
天命终不由人 2020-11-29 16:02

I am new to the C++ language. I have been starting to use vectors, and have noticed that in all of the code I see to iterate though a vector via indices, the first parameter

12条回答
  •  情深已故
    2020-11-29 16:40

    The correct way of iterating the loop and printing its values are as follows:

    #include
    
    //declare the vector of type int
    vector v;
    
    //insert the 5 element in the vector
    for ( unsigned int i = 0; i < 5; i++){
        v.push_back(i);
    }
    
    //print those element
    for (auto it = 0; it < v.end(); i++){
        std::cout << *it << std::endl;
    }
    

提交回复
热议问题