Addison wesley more exceptional c plus plus 40 new engineering puzzles programming problems and solutions dec 2001 ISBN 020170434x pdf

234 40 0
Addison wesley more exceptional c plus plus 40 new engineering puzzles programming problems and solutions dec 2001 ISBN 020170434x pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

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 Switching Streams Difficulty: What's the best way to dynamically use different stream sources and targets, including the standard console streams and files? What are the types of std::cin and std::cout? Write an ECHO program that simply echoes its input and that can be invoked equivalently in the two following ways: ECHO outfile ECHO infile outfile In most popular command-line environments, the first command assumes that the program takes input from cin and sends output to cout The second command tells the program to take its input from the file named infile and to produce output in the file named outfile The program should be able to support all of the above input/output options Solution What are the types of std::cin and std::cout? The short answer is that cin boils down to: std::basic_istream and cout boils down to: std::basic_ostream The longer answer shows the connection by following some standard typedefs and templates First, cin and cout have type std::istream and std::ostream, respectively In turn, those are typdef'd as std::basic_istream and std::basic_ostream Finally, after accounting for the default template arguments, we get the above Note: If you are using a prestandard implementation of the iostreams subsystem, you might still see intermediate classes, such as istream_with_assign Those classes not appear in the standard Write an ECHO program that simply echoes its input and that can be invoked equivalently in the two following ways: ECHO outfile ECHO infile outfile The Tersest Solution For those who like terse code, the tersest solution is a program containing just a single statement: // Example 1-1: A one-statement wonder // #include #include int main( int argc, char* argv[] ) { using namespace std; (argc > ? ofstream(argv[2], ios::out | ios::binary) : cout) ? ifstream(argv[1], ios::in | ios::binary) : cin) rdbuf(); } This works because of two cooperating facilities: First, basic_ios provides a convenient rdbuf() member function that returns the streambuf used inside a given stream object, in this case either cin or a temporary ifstream, both of which are derived from basic_ios Second, basic_ostream provides an operator 0; ++first, n ) ; if( first != last ) { FwdIter dest = first; return copy( ++first, last, dest ); } } return last; There is one problem in Example 2-3(c), and that one problem has two aspects: Correct preconditions: We don't require that n

Ngày đăng: 20/03/2019, 15:53

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan