Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Subenle committed Aug 22, 2016
2 parents 7da20b0 + 28d5cc3 commit ef9db35
Show file tree
Hide file tree
Showing 31 changed files with 1,692 additions and 1,461 deletions.
841 changes: 445 additions & 396 deletions ch02/README.md

Large diffs are not rendered by default.

40 changes: 27 additions & 13 deletions ch02/ex2_35.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
#include <iostream>
#include <typeinfo>

int main()
{
const int i = 42;
auto j = i;
const auto& k = i;
auto* p = &i;
const auto j2 = i, & k2 = i;
int main() {
const int i = 42;
auto j = i;
const auto &k = i;
auto *p = &i;
const auto j2 = i, &k2 = i;

// print i means int, and PKi means pointer to const int.
std::cout << "j is " << typeid(j).name() << "\nk is " << typeid(k).name()
<< "\np is " << typeid(p).name() << "\nj2 is "
<< typeid(j2).name() << "\nk2 is " << typeid(k2).name()
<< std::endl;
std::cout << "i is " << typeid(i).name() << "\n";
std::cout << "j is " << typeid(j).name() << "\n";
std::cout << "k is " << typeid(k).name() << "\n";
std::cout << "p is " << typeid(p).name() << "\n";
std::cout << "j2 is " << typeid(j2).name() << "\n";
std::cout << "k2 is " << typeid(k2).name() << "\n";

return 0;
std::cout << std::endl;
std::cout << std::boolalpha;

std::cout << "i and j have same type? "
<< std::is_same<decltype(i), decltype(j)>::value << "\n";
std::cout << "i and k have same type? "
<< std::is_same<decltype(i), decltype(k)>::value << "\n";
std::cout << "i and j2 have same type? "
<< std::is_same<decltype(i), decltype(j2)>::value << "\n";
std::cout << "j and j2 have same type? "
<< std::is_same<decltype(j), decltype(j2)>::value << "\n";
std::cout << "k and k2 have same type? "
<< std::is_same<decltype(k), decltype(k2)>::value << "\n";

return 0;
}
62 changes: 29 additions & 33 deletions ch05/ex5_14.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
#include <iostream>
#include <string>

using std::cout;
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::pair;
//subsection 5.4.1 is about 'while',so it's appropriate to use 'while'
void max_duplicate_while(){
pair<string,int> max_duplicated;
int count = 0;
string str, prestr;
while(cin >> str){
if(str == prestr) ++count;
else count = 0;
if (count > max_duplicated.second) max_duplicated = {prestr, count};
prestr = str;
}
if (max_duplicated.first.empty()) cout << "There's no duplicated string." << endl;
else cout << "the word " << max_duplicated.first << " occurred " << max_duplicated.second + 1 << " times. " << endl;
}

void max_duplicate_for(){
pair<string, int> max_duplicated;
int count = 0;
for (string str, prestr; cin >> str; prestr = str)
{
if (str == prestr) ++count;
else count = 0;
if (count > max_duplicated.second) max_duplicated = { prestr, count };
int main()
{
string pre_word, word, max_repeat_word;
int repeat_times = 0, max_repeat_times = 0;

while (cin >> word) {
if (word == pre_word) {
++repeat_times;
} else {
repeat_times = 1;
pre_word = word;
}

if (max_repeat_times < repeat_times) {
max_repeat_times = repeat_times;
max_repeat_word = pre_word;
}
}

if (max_duplicated.first.empty()) cout << "There's no duplicated string." << endl;
else cout << "the word " << max_duplicated.first << " occurred " << max_duplicated.second + 1 << " times. " << endl;
if (max_repeat_times <= 1){
cout << "no word was repeated" << endl;
} else {
cout << "the word '" << max_repeat_word << "' occurred " << max_repeat_times << " times" << endl;
}
}

int main()
{
max_duplicate_while();
//max_duplicate_for();
return 0;
}
// test case
// how now now now brown cow cow : the word 'now' occurred 3 times
// how how now now cow cow : the word 'how' occurred 2 times
// how how how : the word 'how' occurred 3 times
// (nothing, enter Ctrl + Z/D, EOF) : no word was repeated.
2 changes: 1 addition & 1 deletion ch05/ex5_21.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main()
while (cin >> curr) {
if (!isupper(curr[0]))
continue;
else if (prev == curr) {
if (prev == curr) {
cout << curr << " occurs twice in succession." << endl;
no_twice = true;
break;
Expand Down
15 changes: 8 additions & 7 deletions ch06/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ casue `c` maybe a temp varable. such as `find_char(s, 'a', occurs)`

>What would happen if we made `s` a plain reference? What if we made `occurs` a reference to const?
`s` could be changed in the function, and `occurs` whould not be changed. so `occurs = 0;` is an error.
`s` could be changed in the function, and `occurs` would not be changed. so `occurs = 0;` is an error.

## Exercise 6.16
```cpp
Expand Down Expand Up @@ -195,13 +195,14 @@ The type of `elem` in the `for` loop is `const std::string&`.

## Exercise 6.29

We should use `const reference` as the loop control variable. because the elements in an `initializer_list` are always const values, so we cannot change the value of an element in an `initializer_list`.
Depends on the type of elements of `initializer_list`. When the type is [PODType](http://en.cppreference.com/w/cpp/concept/PODType), reference is unnecessary. Because `POD` is **cheap to copy**(such as `int`). Otherwise, Using reference(`const`) is the better choice.

## Exercise 6.30

Error (Clang):
>Non-void function 'str_subrange' should return a value. // error #1

>Control may reach end of non-void function. // error #2
## Exercise 6.31
Expand Down Expand Up @@ -271,7 +272,7 @@ decltype(arrStr)& arrPtr(int i)
(b) legal, and match.
(c) legal, but not match. `wd` whould be setting to '*'.
(c) legal, but not match. `wd` would be setting to '*'.
## [Exercise 6.42](ex6_42.cpp)
## Exercise 6.43
Expand Down Expand Up @@ -299,18 +300,18 @@ more discusses: [#22](https://github.com/ReadingLab/Discussion-for-Cpp/issues/22
This loop let user input a word all the way until the word is sought.
It isn't a good use of assert. because if user begin to input a word, the `cin` would be always have content. so the `assert` would be always `true`. It is meaningless. using `assert(s == sought)` is more better.
It isn't a good use of `assert`. The `assert` macro is often used to check for conditions that “cannot happen”. But the `assert` would always happen when users input `EOF` directly. The behavior is very natural, so the check is meaningless. using `assert(!cin || s == sought)` is more better.
## Exercise 6.49
candidate function:
>Set of functions that are considered when resolving a function call. (all the functions
with the name used in the call for which a declaration is in scope at the time of the call.)
>with the name used in the call for which a declaration is in scope at the time of the call.)
viable function:
>Subset of the candidate functions that could match a given call.
>It have the same number of parameters as arguments to the call,
and each argument type can be converted to the corresponding parameter type.
>and each argument type can be converted to the corresponding parameter type.
## Exercise 6.50
Expand Down Expand Up @@ -339,7 +340,7 @@ int calc(const int&, const int&); // calls lookup(const int&)
(b)
```cpp
int calc(char*, char*); // calls lookup(char*)
int calc(const char*, const char*); calls lookup(const char *)
int calc(const char*, const char*); //calls lookup(const char *)
```
(c)
Expand Down
4 changes: 2 additions & 2 deletions ch06/ex6_47.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ using std::vector;
using std::cout;
using std::endl;

#define NDEBUG
//#define NDEBUG

void printVec(vector<int>& vec)
{
#ifdef NDEBUG
#ifndef NDEBUG
cout << "vector size: " << vec.size() << endl;
#endif
if (!vec.empty()) {
Expand Down
16 changes: 8 additions & 8 deletions ch07/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ we can try to divide it like that:

```cpp
std::istream &firstStep = read(cin, data1);
sdt::istream &secondStep = read(firstStep, data2);
std::istream &secondStep = read(firstStep, data2);
if (secondStep)
```

Expand Down Expand Up @@ -306,18 +306,18 @@ Such as `Book`:
class Book {
public:
Book() = default;
Book(unsigned no, std::string name, std::string author, std::string pubdate) : no*(no), name*(name), author*(author), pubdate*(pubdate) { }
Book(unsigned no, std::string name, std::string author, std::string pubdate) : no_(no), name_(name), author_(author), pubdate_(pubdate) { }
Book(std::istream &in) { in >> no_ >> name_ >> author_ >> pubdate_; }
private:
unsigned no*;
std::string name*;
std::string author*;
std::string pubdate*;
unsigned no_;
std::string name_;
std::string author_;
std::string pubdate_;
};
```

Exercise 7.41 [Header](ex7_41.h)|[Cpp](ex7_41.cpp)|[Test](ex7_41_TEST.cpp)
Exercise 7.41 [Header](ex7_41.h)|[CPP](ex7_41.cpp)|[Test](ex7_41_TEST.cpp)
--------------------------------------------------------------------------

Exercise 7.42
Expand Down Expand Up @@ -384,7 +384,7 @@ Exercise 7.49
```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.)
(c) Sales_data &combine(const Sales_data&) const; // The trailing const mark can't be put here, as it forbids any mutation on data members. This comflicts with combine's semantics.
(c) Sales_data &combine(const Sales_data&) const; // The trailing const mark can't be put here, as it forbids any mutation on data members. This conflicts with combines semantics.
```

Some detailed explanation about problem (b) :
Expand Down
2 changes: 1 addition & 1 deletion ch09/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ vec.resize(10); // erases 90 elements from the back of vec
that takes a single argument place on the element type?

If the container holds elements of a class type and resize adds elements
we **must supply an initializer** or the element type must have a **default constructor**.
the element type must have a **default constructor**.

## Exercise 9.31 [use list](ex9_31_1.cpp) | [use forward_list](ex9_31_2.cpp)
## [Exercise 9.32](ex9_32.cpp)
Expand Down
4 changes: 2 additions & 2 deletions ch09/ex9_50.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
#include <string>
#include <vector>

int sum_for_int(const std::vector<std::string> const& v)
int sum_for_int(const std::vector<std::string> &v)
{
int sum = 0;
for (auto const& s : v) sum += std::stoi(s);
return sum;
}

float sum_for_float(const std::vector<std::string> const& v)
float sum_for_float(const std::vector<std::string> &v)
{
float sum = 0.0;
for (auto const& s : v) sum += std::stof(s);
Expand Down
Loading

0 comments on commit ef9db35

Please sign in to comment.