Search
 
SCRIPT & CODE EXAMPLE
 

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]++;
}
Comment

C++ Ranged Based for Loop

for (variable : collection) {
    // body of loop
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: 1672. Richest Customer Wealth leetcode solution in c++ 
Cpp :: What will be the values of variables p, q and i at the end of following loop? int p = 5; int q = 18; for(int i=1;i&lt;5;i++) p++; --q; 
Cpp :: windows install cppcheck 
Cpp :: Chef and IPC Certificates codechef solution in c++ 
Cpp :: string class cpp 
Cpp :: operator using 
Cpp :: multilevel inheritance in c++ private method 
Cpp :: android call custom managed method from native code 
Cpp :: is there anything like vector<intx[100] 
Cpp :: C++ 4.3.2 (gcc-4.3.2) sample 
Cpp :: Consider a pair of integers, (a,b). The following operations can be performed on (a,b) in any order, zero or more times: - (a,b) - ( a+b, b ) - (a,b) - ( a, a+b ) 
Cpp :: sum of 2 arrays c++ 
Cpp :: 771. Jewels and Stones leetcode solution in c++ 
Cpp :: executing linux scripts 
Cpp :: c++ ascii value 
Cpp :: auto i cpp 
Cpp :: middle node of linked list 
Cpp :: C++ Area and Circumference of a Circle 
Cpp :: c++ destructor 
Cpp :: insert into a vector more than once c++ 
Cpp :: cout<<"helloworld"<<endl problem 
C :: C bold output 
C :: how to download file in powershell 
C :: space after format specifiers in c 
C :: how to prevent user from entering char when needing int in c 
C :: prime numbers c 
C :: cannot get / react router dom 
C :: find smallest number in array in c 
C :: how to print value of pointer in c 
C :: how to sort assending in c 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =