forked from pezy/CppPrimer
-
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
31 changed files
with
1,692 additions
and
1,461 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -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; | ||
} |
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 |
---|---|---|
@@ -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. |
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
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
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
Oops, something went wrong.