Last active
August 29, 2015 14:21
-
-
Save krysseltillada/484112ccff486dd708c5 to your computer and use it in GitHub Desktop.
printing values from 2d array using range for, subscripts and pointers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| int main() | |
| { | |
| int ia[3][4] = { | |
| {0, 1 ,2 ,3}, | |
| {4, 5, 6, 7}, | |
| {8, 9, 10, 11} | |
| }; | |
| /// using range for | |
| for (int (&i)[4] : ia) { | |
| for(int &i2 : i) { | |
| std::cout << i2 << " "; | |
| } | |
| std::cout << std::endl; | |
| } | |
| /// using subscript | |
| for (size_t i = 0; i != 3; ++i) { | |
| for (size_t i2 = 0; i2 != 4; ++i2) { | |
| std::cout << ia[i][i2] << " "; | |
| } | |
| std::cout << std::endl; | |
| } | |
| /// using pointers | |
| for (int (*p)[4] = ia; p != std::end(ia); ++p) { | |
| for (int *q = (*p); q != std::end(*p); ++q) { | |
| std::cout << *q << " "; | |
| } | |
| std::cout << std::endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment