Skip to content

Commit

Permalink
ch01-ch10
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaowei Hu committed Dec 7, 2017
1 parent 80757d8 commit 881163c
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 8 deletions.
3 changes: 3 additions & 0 deletions ch05/ex5_09.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ int main()
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
char ch;
while (cin >> ch) {
cout << ch << ' ';
if (ch == 'a')
++aCnt;
else if (ch == 'e')
Expand All @@ -20,6 +21,8 @@ int main()
else if (ch == 'u')
++uCnt;
}
cout << endl;

cout << "Number of vowel a: \t" << aCnt << '\n' << "Number of vowel e: \t"
<< eCnt << '\n' << "Number of vowel i: \t" << iCnt << '\n'
<< "Number of vowel o: \t" << oCnt << '\n' << "Number of vowel u: \t"
Expand Down
5 changes: 3 additions & 2 deletions ch05/ex5_11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ int main()
<< "Number of vowel e(E): \t" << eCnt << '\n'
<< "Number of vowel i(I): \t" << iCnt << '\n'
<< "Number of vowel o(O): \t" << oCnt << '\n'
<< "Number of vowel u(U): \t" << uCnt << '\n' << "Number of space: \t"
<< spaceCnt << '\n' << "Number of tab char: \t" << tabCnt << '\n'
<< "Number of vowel u(U): \t" << uCnt << '\n'
<< "Number of space: \t" << spaceCnt << '\n'
<< "Number of tab char: \t" << tabCnt << '\n'
<< "Number of new line: \t" << newLineCnt << endl;

return 0;
Expand Down
2 changes: 1 addition & 1 deletion ch06/ex6_51.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void f(double, double)

int main()
{
f(2.56, 42); // error: 'f' is ambiguous.
// f(2.56, 42); // error: 'f' is ambiguous.
f(42);
f(42, 0);
f(2.56, 3.14);
Expand Down
16 changes: 16 additions & 0 deletions ch07/ex7_05.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Created by xhu on 17-11-29.
//
#include "ex7_05.h"
#include <iostream>
#include <string>

int main() {
Person p;
std::string &name = p.getName();
std::string &address = p.getAddress();
name = "y";
address = "y";
std::cout << p.getName() << p.getAddress() << std::endl;
}

11 changes: 7 additions & 4 deletions ch07/ex7_05.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
#include <string>

class Person {
std::string name;
std::string address;
private:
std::string name = "x";
std::string address = "x";

public:
const std::string& getName() const { return name; }
const std::string& getAddress() const { return address; }
// const std::string& getName() const { return name; }
// const std::string& getAddress() const { return address; }
std::string& getName() { return name; }
std::string& getAddress() { return address; }
};

#endif
Expand Down
6 changes: 6 additions & 0 deletions ch08/ex8_02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ istream& func(istream& is)
{
std::string buf;
while (is >> buf) std::cout << buf << std::endl;

std::cout << is.good() << ' ' << is.fail() << ' '
<< is.eof() << ' ' << is.bad() << std::endl;
is.clear();
std::cout << is.good() << ' ' << is.fail() << ' '
<< is.eof() << ' ' << is.bad() << std::endl;

return is;
}

Expand Down
2 changes: 2 additions & 0 deletions ch08/ex8_04.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ void ReadFileToVec(const string& fileName, vector<string>& vec)
if (ifs) {
string buf;
while (std::getline(ifs, buf)) vec.push_back(buf);
std::cout << ifs.good() << ' ' << ifs.fail() << ' '
<< ifs.eof() << ' ' << ifs.bad() << std::endl;
}
}

Expand Down
6 changes: 6 additions & 0 deletions ch08/ex8_09.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ istream& func(istream& is)
{
std::string buf;
while (is >> buf) std::cout << buf << std::endl;

std::cout << is.good() << ' ' << is.fail() << ' '
<< is.eof() << ' ' << is.bad() << std::endl;
is.clear();
std::cout << is.good() << ' ' << is.fail() << ' '
<< is.eof() << ' ' << is.bad() << std::endl;

return is;
}

Expand Down
1 change: 1 addition & 0 deletions ch09/ex9_43.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void Replace(string& s, const string& oldVal, const string& newVal)
std::distance(oldVal.begin(), oldVal.end());) {
if (string{beg, beg + oldVal.size()} == oldVal) {
beg = s.erase(beg, beg + oldVal.size());
// void string::insert(iter p, iter b, iter e)
beg = s.insert(beg, newVal.cbegin(), newVal.cend());
std::advance(beg, newVal.size());
}
Expand Down
1 change: 1 addition & 0 deletions ch10/ex10_05.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ int main()
char c2[10] = "eipi10";
std::vector<char*> roster1{c1};
std::list<char*> roster2{c2};
// compaare address not string
std::cout << std::equal(roster1.cbegin(), roster1.cend(), roster2.cbegin());
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions ch10/ex10_11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ inline bool is_shorter(std::string const& lhs, std::string const& rhs)

void elimdups(std::vector<std::string>& vs)
{
// Both sort and std::sort are OK!
std::sort(vs.begin(), vs.end());
auto new_end = std::unique(vs.begin(), vs.end());
vs.erase(new_end, vs.end());
Expand Down
2 changes: 2 additions & 0 deletions ch10/ex10_28.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ int main()
list<int> lst3;
copy(vec.cbegin(), vec.cend(), front_inserter(lst3));
print(lst3);

return 0;
}
5 changes: 4 additions & 1 deletion ch10/ex10_30.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ int main()
{
std::istream_iterator<int> in_iter(std::cin), eof;
std::vector<int> vec;
while (in_iter != eof) vec.push_back(*in_iter++);
while (in_iter != eof) {
vec.push_back(*in_iter);
vec.push_back(*in_iter++);
}
std::sort(vec.begin(), vec.end());
std::copy(vec.cbegin(), vec.cend(),
std::ostream_iterator<int>(std::cout, " "));
Expand Down

0 comments on commit 881163c

Please sign in to comment.