forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using std::vector; using std::cout; using std::endl; using std::begin; using std::end; | ||
|
||
int main() | ||
{ | ||
int int_arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | ||
vector<int> ivec(begin(int_arr), end(int_arr)); | ||
|
||
for (auto i : ivec) cout << i << " "; | ||
cout << endl; | ||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using std::vector; using std::cout; using std::endl; using std::begin; using std::end; | ||
|
||
int main() | ||
{ | ||
vector<int> ivec{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | ||
int int_arr[10]; | ||
|
||
for (int* i = begin(int_arr); i != end(int_arr); ++i) | ||
*i = ivec[i-begin(int_arr)]; | ||
|
||
for (auto i : int_arr) cout << i << " "; | ||
cout << endl; | ||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <iostream> | ||
|
||
using std::cout; using std::endl; | ||
|
||
int main() | ||
{ | ||
int ia[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; | ||
|
||
// a range for to manage the iteration | ||
for (const int (&p)[4] : ia) | ||
for (int q : p) | ||
cout << q << " "; | ||
cout << endl; | ||
|
||
// ordinary for loop using subscripts | ||
for (size_t i = 0; i != 3; ++i) | ||
for (size_t j = 0; j != 4; ++j) | ||
cout << ia[i][j] << " "; | ||
cout << endl; | ||
|
||
// using pointers. | ||
for (int (*p)[4] = ia; p != ia + 3; ++p) | ||
for (int *q = *p; q != *p + 4; ++q) | ||
cout << *q << " "; | ||
cout << endl; | ||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <iostream> | ||
|
||
using std::cout; using std::endl; | ||
|
||
int main() | ||
{ | ||
int ia[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; | ||
|
||
// a range for to manage the iteration | ||
// use type alias | ||
using int_array = int[4]; | ||
for (int_array& p : ia) | ||
for (int q : p) | ||
cout << q << " "; | ||
cout << endl; | ||
|
||
// ordinary for loop using subscripts | ||
for (size_t i = 0; i != 3; ++i) | ||
for (size_t j = 0; j != 4; ++j) | ||
cout << ia[i][j] << " "; | ||
cout << endl; | ||
|
||
// using pointers. | ||
// use type alias | ||
for (int_array* p= ia; p != ia + 3; ++p) | ||
for (int *q = *p; q != *p + 4; ++q) | ||
cout << *q << " "; | ||
cout << endl; | ||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include <iostream> | ||
|
||
using std::cout; using std::endl; | ||
|
||
int main() | ||
{ | ||
int ia[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; | ||
|
||
// a range for to manage the iteration | ||
for (auto& p : ia) | ||
for (int q : p) | ||
cout << q << " "; | ||
cout << endl; | ||
|
||
// ordinary for loop using subscripts | ||
for (size_t i = 0; i != 3; ++i) | ||
for (size_t j = 0; j != 4; ++j) | ||
cout << ia[i][j] << " "; | ||
cout << endl; | ||
|
||
// using pointers. | ||
for (auto p = ia; p != ia + 3; ++p) | ||
for (int *q = *p; q != *p + 4; ++q) | ||
cout << *q << " "; | ||
cout << endl; | ||
|
||
return 0; | ||
} |
This file contains 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
This file contains 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