Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

ranged based for loop c++

#include <vector>
#include <iostream>

std::vector<int> testingVector{1,2,3,4,5};
/* firstly you define the type in this case it is int then you pass it as
 reference with "&" then you choose your "name" for element in this case
 it is element after the : you type in name of the vector variable
 you use the element as a varibale which will loop around every element in
 the vector
 */
for (int& element : testingVector)
{
	element++;
}
OUTPUT: 2 3 4 5 6
//equivalent in normal for loop would be this
for(int i{}; i < testingVector.size(); i++)
{
  testingVector[i]++;
}
 
PREVIOUS NEXT
Tagged: #ranged #based #loop
ADD COMMENT
Topic
Name
8+5 =