Skip to content

Commit

Permalink
merge pezy's fork
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Apr 13, 2015
2 parents 3447414 + 1e3bff0 commit 4709586
Show file tree
Hide file tree
Showing 173 changed files with 5,607 additions and 10,451 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
##C++ Primer 5 答案
##C++ Primer 5 Answers

[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/pezy/Cpp-Primer?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![GitHub issues](https://img.shields.io/github/issues/Mooophy/Cpp-Primer.svg)](https://github.com/Mooophy/Cpp-Primer/issues)
[![GitHub license](https://img.shields.io/badge/license-CC0-blue.svg)](https://raw.githubusercontent.com/Mooophy/Cpp-Primer/master/LICENSE)
[![](https://img.shields.io/badge/%E4%B8%AD%E6%96%87-%E8%AE%A8%E8%AE%BA%E5%8C%BA-yellowgreen.svg)](https://github.com/Ocxs/Issues)
[![](https://img.shields.io/badge/douban-%E5%B0%8F%E7%BB%84-green.svg)](http://www.douban.com/group/532124/)

### Note

Expand All @@ -15,7 +18,7 @@
- Please **fork**([How?](https://help.github.com/articles/fork-a-repo)) this repository first.
- **Commit**([How?](https://help.github.com/articles/create-a-repo#commit-your-first-change)) in your own repository.
- Give me a **pull request**([How?](https://help.github.com/articles/using-pull-requests)).
- Syncing with([How?](https://help.github.com/articles/syncing-a-fork/)) the origin repository. This is very **important** because this repo updates frequently.
- **Syncing** with([How?](https://help.github.com/articles/syncing-a-fork/)) the origin repository. This is very **important** because this repo updates frequently.

### Table of Contents

Expand All @@ -35,7 +38,7 @@
- [Chapter 12. Dynamic Memory](ch12/README.md)
- Part III: Tools for Class Authors
- [Chapter 13. Copy Control](ch13/README.md)
- [Chapter 14. Overloaded Operations and Conversions](ch14)
- [Chapter 14. Overloaded Operations and Conversions](ch14/README.md)
- [Chapter 15. Object-Oriented Programming](ch15)
- [Chapter 16. Templates and Generic Programming](ch16)
- Part IV: Advanced Topics
Expand Down
2 changes: 1 addition & 1 deletion ch02/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ int main()
>Write your own version of the Sales_data.h header and
use it to rewrite the exercise from § 2.6.2(p. 76)

You can add some function in your header file. Look at [here](https://github.com/pezy/Cpp-Primer/blob/master/ch02/Sales_data.h).
You can add some function in your header file. Look at [here](ex2_42.h).

rewrite the exercise:

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion ch02/ex2_42_1.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>
#include "Sales_data.h"
#include "ex2_42.h"

int main()
{
Expand Down
2 changes: 1 addition & 1 deletion ch02/ex2_42_2.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>
#include "Sales_data.h"
#include "ex2_42.h"

int main()
{
Expand Down
2 changes: 1 addition & 1 deletion ch02/ex2_42_3.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>
#include "Sales_data.h"
#include "ex2_42.h"

int main()
{
Expand Down
4 changes: 2 additions & 2 deletions ch03/ex3_17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
///@Date Aug. 2014
///@Brief
/// Read a sequence of words from cin and store the values a vector.
/// After you've read all the words, process the vector and change each word to uppercase.
/// After you've read all the words, process the vector and change each word to uppercase.
/// Print the transformed elements, eight words to a line.
///

Expand All @@ -30,7 +30,7 @@ int main()

for (decltype(vec.size()) i=0; i != vec.size(); ++i)
{
if (i!=0&&i%8 == 0) cout << endl;
if (i != 0 && i%8 == 0) cout << endl;
cout << vec[i] << " ";
}
cout << endl;
Expand Down
2 changes: 1 addition & 1 deletion ch05/ex5_17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int main()
auto size = vec1.size() < vec2.size() ? vec1.size() : vec2.size();
for (decltype(vec1.size()) i = 0; i != size; ++i) {
if (vec1[i] != vec2[i]) { cout << "false" << endl; break; }
if (i == size - 1) cout << "ture" << endl;
if (i == size - 1) cout << "true" << endl;
}

return 0;
Expand Down
4 changes: 2 additions & 2 deletions ch06/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ using std::cout;
using std::endl;

void factorial(){

int num=0;
unsigned result=1;
cout<<"Please input a positive number: "<<endl;
Expand Down Expand Up @@ -323,7 +323,7 @@ constexpr bool isShorter(const string& str1, const string& str2)
return str1.size() < str2.size();
}
```

If you want know more about `constexpr function `, maybe [it](http://stackoverflow.com/questions/28880538/i-am-confused-about-a-constexpr-function) is useful to you.
## [Exercise 6.47](ex6_47.cpp)
## Exercise 6.48

Expand Down
19 changes: 10 additions & 9 deletions ch07/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ struct X {
## Exercise 7.37

```cpp
Sales_data first_item(cin); // use Sales_data(std::istream &is)
Sales_data first_item(cin); // use Sales_data(std::istream &is) ; its value are up to your input.

int main() {
Sales_data next; // use Sales_data(std::string s = "")
Sales_data last("9-999-99999-9"); // use Sales_data(std::string s = "")
Sales_data next; // use Sales_data(std::string s = ""); bookNo = "", cnt = 0, revenue = 0.0
Sales_data last("9-999-99999-9"); // use Sales_data(std::string s = ""); bookNo = "9-999-99999-9", cnt = 0, revenue = 0.0
}
```
Expand Down Expand Up @@ -273,10 +273,10 @@ No problem. cause `C` have the default constructor.

## Exercise 7.46

- a) A class must provide at least one constructor. (**unture**, "The compiler-generated constructor is known as the synthesized default constructor.")
- b) A default constructor is a constructor with an empty parameter list. (**unture**, A default constructor is a constructor that is used if no initializer is supplied)
- c) If there are no meaningful default values for a class, the class should not provide a default constructor. (**unture**, the class should provide.)
- d) If a class does not define a default constructor, the compiler generates one that initializes each data member to the default value of its associated type. (**unture**, only if our class does not explicitly define any constructors, the compiler will implicitly define the default constructor for us.)
- a) A class must provide at least one constructor. (**untrue**, "The compiler-generated constructor is known as the synthesized default constructor.")
- b) A default constructor is a constructor with an empty parameter list. (**untrue**, A default constructor is a constructor that is used if no initializer is supplied.What's more, A constructor that supplies default arguments for all its parameters also defines the default constructor)
- c) If there are no meaningful default values for a class, the class should not provide a default constructor. (**untrue**, the class should provide.)
- d) If a class does not define a default constructor, the compiler generates one that initializes each data member to the default value of its associated type. (**untrue**, only if our class does not explicitly define **any constructors**, the compiler will implicitly define the default constructor for us.)

## Exercise 7.47

Expand All @@ -299,9 +299,10 @@ Both are noting happened.

```cpp
(a) Sales_data &combine(Sales_data); // ok
(b) Sales_data &combine(Sales_data&); // [Error] no matching function for call to 'Sales_data::combine(std::string&)' (`std::string&` can not convert to `Sales_data` type.)
(b) Sales_data &combine(Sales_data&); // [Error] no matching function for call to 'Sales_data::combine(std::string&)' (`std::string&` can not convert to `Sales_data` type.)
(c) Sales_data &combine(const Sales_data&) const; // [Error] assignment of member 'Sales_data::units_sold' in read-only object. (we cannot combine the other `Sales_data` object.)
```
Some detailed explanation about problem (b) :It's wrong. Because `combine`’s parameter is a non-const reference , we can't pass a temporary to that parameter. If `combine`’s parameter is a reference to const , we can pass a temporary to that parameter. Like this :`Sales_data &combine(const Sales_data&); ` Here we call the `Sales_data` `combine` member function with a string argument. This call is perfectly legal; the compiler automatically creates a `Sales_data` object from the given string. That newly generated (temporary) `Sales_data` is passed to `combine`.(Also you can read C++ Primer Page 295(English Edition))
## [Exercise 7.50](ex7_50.h)
## Exercise 7.51
Expand Down Expand Up @@ -331,7 +332,7 @@ it is very natural.

## Exercise 7.52

`Sales_data` should have no in-class initializers.
In my opinion ,the aim of the problem is Aggregate Class. Test-makers think that `Sales_data` is Aggregate Class,so `Sales_data` should have no in-class initializers if we want to initialize the data members of an aggregate class by providing a braced list of member initializers:

FIXED:

Expand Down
2 changes: 1 addition & 1 deletion ch07/ex7_32.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class Window_mgr {
};

class Screen {
friend void Window_mgr::clear(ScreenIndex);
public:
using pos = std::string::size_type;
friend void Window_mgr::clear(ScreenIndex);

Screen() = default; // 1
Screen(pos ht, pos wd):height(ht),width(wd),contents(ht*wd, ' '){} // 2
Expand Down
29 changes: 17 additions & 12 deletions ch09/README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
# Chapter 9. Sequential Containers

## Exercise 9.1:
>Which is the most appropriate—a vector, a deque, or a list—for the following program tasks?
Explain the rationale for your choice.
If there is no reason to prefer one or another container, explain why not.
>Which is the most appropriate—a vector, a deque, or a list—for the following program tasks?Explain the rationale for your choice.If there is no reason to prefer one or another container, explain why not.
>- (a) Read a fixed number of words, inserting them in the container alphabetically as they are entered. We’ll see in the next chapter that associative containers are better suited to this problem.
>- (b) Read an unknown number of words. Always insert new words at the back. Remove the next value from the front.
>- (c) Read an unknown number of integers from a file. Sort the numbers and then print them to standard output.
- (a) `std::set` is the best. now, we can select `vector` or `deque`, better than `list`, cause we don't need insert or delete elements in the middle.
- (b) `deque`.
>If the program needs to insert or delete elements at the front and the back, but not in the middle, use a deque
- (c) `vector`, no need that insert or delete at the front or back. and
>If your program has lots of small elements and space overhead matters, don’t
use list or forward_list.
- (b) `deque`. If the program needs to insert or delete elements at the front and the back, but not in the middle, use a deque
- (c) `vector`, no need that insert or delete at the front or back. and If your program has lots of small elements and space overhead matters, don’t use list or forward_list.

## Exercise 9.2:
>Define a list that holds elements that are deques that hold ints.
Expand Down Expand Up @@ -60,14 +55,15 @@ vector<int>::iterator find(vector<int>::iterator beg, vector<int>::iterator end,
```cpp
list<int> lst1;
list<int>::iterator iter1 = lst1.begin(), iter2 = lst1.end();
while (iter1 < iter2) /*ERROR: operator< cannot be used in list*/
while (iter1 < iter2) /*ERROR: operator< can't be applied to iterator for list*/
```

Fixed:
```cpp
while(iter1 != iter2)
```

#### note:
operator `<` can be used in `list`,but can't be applied to iterator for `list`.
## Exercise 9.7:
>What type should be used as the index into a vector of ints?
Expand Down Expand Up @@ -129,7 +125,16 @@ vector<int> vec(other_vec.begin(), other_vec.end()); // same as other_vec
## Exercise 9.12:
>Explain the differences between the constructor that takes a container to copy and the constructor that takes two iterators.
we can use the constructor that takes two iterators to copy a **subsequence** of a container. But the constructor that takes a container to copy should copy whole container.
- Constructor that takes two iterators copies the items between `[first, last)`,e.g.
```cpp
auto data = { 1, 2, 3 };
std::vector<int> vec(data.begin(), data.begin()+1); // vec is {1}
```
- Constructor that takes another container copies all items from it. e.g.
```cpp
auto data = { 1, 2, 3 };
std::vector<int> vec(data); //vec is {1,2,3}
```
## [Exercise 9.13](ex9_13.cpp)
## [Exercise 9.14](ex9_14.cpp)
Expand All @@ -140,7 +145,7 @@ we can use the constructor that takes two iterators to copy a **subsequence** of
>Assuming c1 and c2 are containers, what (if any) constraints does the following usage place on the types of c1 and c2?
First, ther must be the identical container and same type holded.
Second,the type holded must support relational operation. (@Mooophy)
Second,the type held must support relational operation. (@Mooophy)
Both c1 and c2 are the containers except the unordered associative containers.(@pezy)
Expand Down
2 changes: 1 addition & 1 deletion ch09/ex9_26.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int main()

//! remove even value
for(auto it = vec.begin(); it != vec.end(); )
if(! *it & 0x1) it = vec.erase(it);
if(! (*it & 0x1)) it = vec.erase(it);
else ++it;

//! print
Expand Down
7 changes: 3 additions & 4 deletions ch11/ex11_3_4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ void ex11_3()
std::map<std::string, std::size_t> word_count;
std::string word;
while(std::cin >> word)
{
++word_count[word];
for (const auto &elem : word_count)
std::cout << elem.first << " : " << elem.second <<"\n";
}

for (const auto &elem : word_count)
std::cout << elem.first << " : " << elem.second <<"\n";
}

int main()
Expand Down
Loading

0 comments on commit 4709586

Please sign in to comment.