Skip to content

Commit

Permalink
Add an ostream parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Queequeg92 committed Nov 17, 2014
1 parent bc15988 commit c7afdc7
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions ch16/ex16.19.20/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,41 @@

//! ex16.19
template<typename Container>
std::ostream& print(Container& c);
std::ostream& print(Container& c, std::ostream &os);

//! ex16.20
template<typename Container>
std::ostream& print2(Container& c);
std::ostream& print2(Container& c, std::ostream &os);

int main()
{
std::vector<int> v = {1,23,6,4,5,7,4};
std::list<std::string> l = {"ss","sszz","saaas","s333s","ss2","sss"};
print2(v);
print2(l);



print2(v, std::cout);
print2(l, std::cout);

return 0;
}

//! ex16.19 using size() to control the loop
template<typename Container>
std::ostream & print(Container &c)
std::ostream & print(Container &c, std::ostream &os)
{
typedef typename Container::size_type size_type;

auto it = c.begin();
for(size_type i = 0; i!= c.size(); ++i)
std::cout << *it++ << "\n";
os << *it++ << "\n";

return std::cout;
return os;
}

//! ex16.20 using iterator to control the loop
template<typename Container>
std::ostream& print2(Container& c)
std::ostream& print2(Container& c, std::ostream &os)
{
for (auto it = c.begin(); it != c.end(); ++it)
std::cout << *it << "\n";
os << *it << "\n";

return std::cout;
return os;
}

0 comments on commit c7afdc7

Please sign in to comment.