Skip to content

Commit

Permalink
Finish ch3:ok_hand:
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Sep 12, 2014
1 parent 840fe55 commit 0243568
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- I don't know the standard answer, but I tried my best to keep the correctness, if you found any bug, please [tell me](https://github.com/Mooophy/Cpp-Primer/issues/new), thanks.
- I have downloaded the headers from this book's [web site](http://www.informit.com/store/c-plus-plus-primer-9780321714114) and put them in the `include` folder.
- In order to test the program in an efficient way, I also put the test data file in the `data` folder.
- If you find any mistake of the questions. Please check the [Errata](http://ptgmedia.pearsoncmg.com/images/9780321714114/errata/9780321714114_errata_10-31-12.html) first.

### If you want to contribute this repository.

Expand Down
15 changes: 15 additions & 0 deletions ch03/ex3_41.cpp
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;
}
18 changes: 18 additions & 0 deletions ch03/ex3_42.cpp
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;
}
28 changes: 28 additions & 0 deletions ch03/ex3_43.cpp
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;
}
31 changes: 31 additions & 0 deletions ch03/ex3_44.cpp
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;
}
28 changes: 28 additions & 0 deletions ch03/ex3_45.cpp
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;
}
8 changes: 6 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ C++ Primer 5th Answer Note
[3.36](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_36.cpp) |
[3.37 ~ 3.38](/Cpp-Primer/ch03/ex3.37_3.38) |
[3.39](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_39.cpp) |
[3.40](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_40.cpp)

[3.40](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_40.cpp) |
[3.41](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_41.cpp) |
[3.42](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_42.cpp) |
[3.43](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_43.cpp) |
[3.44](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_44.cpp) |
[3.45](https://github.com/pezy/Cpp-Primer/blob/master/ch03/ex3_45.cpp)
- Chapter 4. Expressions
- Chapter 5. Statements
- Chapter 6. Functions
Expand Down
2 changes: 1 addition & 1 deletion docs/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
margin-top: 1em !important;
}
h3 { text-transform: uppercase; font-size: 0.99em; }
body { font-family: 'Merriweather', Georgia, serif; }
body { font-family: 'Merriweather', Georgia, serif; background-color: #fff6e7; }
.content { font-size: 1em; }
ul, ol { list-style-position: inside !important; }
</style>
Expand Down

0 comments on commit 0243568

Please sign in to comment.