More Exceptional C++
By Herb Sutter
Generic Programming and the C++ Standard
Library
One of C++'s most powerful features is its support for generic programming. This power is reflected
directly in the flexibility of the C++ standard library, especially in its containers, iterators, and
algorithms portion, originally known as the standard template library (STL).
This opening section focuses on how to make the best use of the C++ standard library, particularly the
STL. When and how can you make best use of
std::vector and std::deque? What pitfalls
might you encounter when using
std::map and std::set, and how can you safely avoid them?
Why doesn't
std::remove() actually remove anything?
This section also highlights some useful techniques, as well as pitfalls, that occur when writing
generic code of your own, including code that's meant to work with and extend the STL. What kinds
of predicates are safe to use with the STL; what kinds aren't, and why? What techniques are available
for writing powerful generic template code that can change its own behavior based on the capabilities
of the types it's given to work with? How can you switch easily between different kinds of input and
output streams? How does template specialization and overloading work? And what's with this funny
typename keyword, anyway?
This and more, as we delve into topics related to generic programming and the C++ standard library.
Item 1. Switching Streams
Difficulty: 2
What's the best way to dynamically use different stream sources and targets, including the standard
console streams and files?
1. What are the types of
std::cin and std::cout?
2. Write an
ECHO program that simply echoes its input and that can be invoked equivalently in
the two following ways:
ECHO <infile >outfile
ECHO infile outfile
评论0
最新资源