Skip to content

Commit

Permalink
added 14.8
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Apr 7, 2015
1 parent 361a3b1 commit 42895c0
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 228 deletions.
32 changes: 32 additions & 0 deletions ch14/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,35 @@ There is no dynamic allocation to deal with, so the synthesized destructor is en
> Define a class that holds a pointer to a `StrBlobPtr`. Define the overloaded arrow operator for that class.
[hpp](ex14_32.h) | [cpp](ex14_32.cpp)

## Exercise 14.33:
> How many operands may an overloaded function-call operator take?
An overloaded operator function has the same number of parameters as the operator has operands. Hence the maximum value should be around 256.
([question on SO](http://stackoverflow.com/questions/21211889/how-many-operands-may-an-overloaded-function-call-operator-take))

## Exercise 14.34:
> Define a function-object class to perform an if-then-else operation: The call operator for this class should take three parameters. It should test its first parameter and if that test succeeds, it should return its second parameter; otherwise, it should return its third parameter.
```cpp
struct Test {
int operator()(bool b, int iA, int iB) {
return b ? iA : iB;
}
};
```
## Exercise 14.35:
> Write a class like `PrintString` that reads a line of input from an `istream` and returns a `string` representing what was read. If the read fails, return the empty `string`.
[Test](ex14_35.cpp)
## Exercise 14.36:
> Use the class from the previous exercise to read the standard input, storing each line as an element in a vector.
[Test](ex14_36.cpp)
## Exercise 14.37:
> Write a class that tests whether two values are equal. Use that object and the library algorithms to write a program to replace all instances of a given value in a sequence.
[Test](ex14_37.cpp)
17 changes: 0 additions & 17 deletions ch14/ex14.33.34.35.36.37/CppPrimer.pro

This file was deleted.

2 changes: 0 additions & 2 deletions ch14/ex14.33.34.35.36.37/inputstring.cpp

This file was deleted.

43 changes: 0 additions & 43 deletions ch14/ex14.33.34.35.36.37/inputstring.h

This file was deleted.

91 changes: 0 additions & 91 deletions ch14/ex14.33.34.35.36.37/main.cpp

This file was deleted.

9 changes: 0 additions & 9 deletions ch14/ex14.33.34.35.36.37/test.cpp

This file was deleted.

22 changes: 0 additions & 22 deletions ch14/ex14.33.34.35.36.37/test.h

This file was deleted.

2 changes: 0 additions & 2 deletions ch14/ex14.33.34.35.36.37/testnreplace.cpp

This file was deleted.

42 changes: 0 additions & 42 deletions ch14/ex14.33.34.35.36.37/testnreplace.h

This file was deleted.

21 changes: 21 additions & 0 deletions ch14/ex14_35.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <string>

class GetInput {
public:
GetInput(std::istream &i = std::cin) : is(i) { }
std::string operator()() const {
std::string str;
std::getline(is, str);
return is ? str : std::string();
}

private:
std::istream &is;
};

int main()
{
GetInput getInput;
std::cout << getInput() << std::endl;
}
25 changes: 25 additions & 0 deletions ch14/ex14_36.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
#include <string>
#include <vector>

class GetInput {
public:
GetInput(std::istream &i = std::cin) : is(i) { }
std::string operator()() const {
std::string str;
std::getline(is, str);
return is ? str : std::string();
}

private:
std::istream &is;
};

int main()
{
GetInput getInput;
std::vector<std::string> vec;
for ( std::string tmp; !( tmp = getInput() ).empty(); ) vec.push_back( tmp );
for (const auto &str : vec) std::cout << str << " ";
std::cout << std::endl;
}
20 changes: 20 additions & 0 deletions ch14/ex14_37.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <vector>
#include <algorithm>

class IsEqual {
int value;
public:
IsEqual(int v) : value(v) {}
bool operator()(int elem) {
return elem == value;
}
};

int main()
{
std::vector<int> vec = {3,2,1,4,3,7,8,6};
std::replace_if(vec.begin(), vec.end(), IsEqual(3), 5);
for (int i : vec) std::cout << i << " ";
std::cout << std::endl;
}

0 comments on commit 42895c0

Please sign in to comment.