Click here to Skip to main content
15,914,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What does this for do? How does it work?

for (std::vector<t>& column : mCells) { column.resize(mHeight); }

What I have tried:

What does this for do? How does it work?

for (std::vector<t>& column : mCells) { column.resize(mHeight); }
Posted
Updated 21-Dec-18 10:47am
Comments
KarstenK 22-Dec-18 3:47am    
consider learning the language syntax by some tutorials in the internet. This is a simple for iteration.

It is a for each loop in C++, and iterates over the columns that you have in your (most likely) grid. It is called a range-based loop; since you get a range implicitly in it.

And once it gets the column, it resizes it to a specific height—also notice the reference sign. Without the pass-by-reference your operation will have no effect on the columns. A quick example I created to demonstrate is here,
C++
void processprint() {
    int array[] = { 1, 2, 3, 4 };
    
    // Increment the values, without modifying actual elements. 
    for (int item : array) {
        item += 2;
    }
    
    for (int item : array) {
        std::cout << item << std::endl;
    }
}
This will print the same array, unaltered, however if you change the line,
C++
void processprint() {
    int array[] = { 1, 2, 3, 4 };
    
    // Increment the values, change actual values.
    for (int& item : array) {
        item += 2;
    }
    
    for (int item : array) {
        std::cout << item << std::endl;
    }
}
This will now print incremented values. Check it out here, C++ Shell[^]

Range-based for loop (since C++11) - cppreference.com[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900