I l
@ve RuBoard
•
Table of Contents
Exceptional C++: 47 Engineering Puzzles,
Programming Problems, and Solutions
By
Herb Sutter
Publisher
: Addison Wesley
Pub Date : November 18, 1999
ISBN
: 0-201-61562-2
Pages
: 240
Exceptional C++
shows by example how to go about sound software engineering in standard C++. Do
you enjoy solving thorny C++ problems and puzzles? Do you relish writing robust and extensible
code? Then take a few minutes and challenge yourself with some tough C++ design and programming
problems.
The puzzles and problems in
Exceptional C++
not only entertain, they will help you hone your skills to
become the sharpest C++ programmer you can be. Many of these problems are culled from the
famous Guru of the Week feature of the Internet newsgroup comp.lang.c++.moderated, expanded
and updated to conform to the official ISO/ANSI C++ Standard.
Each problem is rated according to difficulty and is designed to illustrate subtle programming mistakes
or design considerations. After you've had a chance to attempt a solution yourself, the book then
dissects the code, illustrates what went wrong, and shows how the problem can be fixed. Covering a
broad range of C++ topics, the problems and solutions address critical issues such as:
Generic programming and how to write reusable templates
Exception safety issues and techniques
Robust class design and inheritance
Compiler firewalls and the Pimpl Idiom
Name lookup, namespaces, and the Interface Principle
Memory management issues and techniques
Traps, pitfalls, and anti-idioms
Optimization
Try your skills against the C++ masters and come away with the insight and experience to create
I l
@ve RuBoard
Solution
The answers to the three questions are as follows.
What does "
case-insensitive
" mean?
What "
case-insensitive
" actually means depends entirely on your application and language. For
example, many languages do not have cases at all. For those that do, you still have to decide
whether you want accented characters to compare equal to unaccented characters, and so on.
This Item provides guidance on how to implement case-insensitivity for standard strings in
whatever sense applies to your situation.
1.
Write a
ci_string
class that is identical to the standard std::string class but that is case-
insensitive in the same way as the commonly provided extension
stricmp()
.
The "how can I make a case-insensitive string?" question is so common that it probably
deserves its own FAQ—hence this Item.
Here's what we want to achieve:
2.
So "
string
" really means "basic_string<char, char_traits<char>, allocator<char>
>
," possibly with additional defaulted template parameters specific to the implementation you're
using. We don't need to worry about the
allocator
part, but the key here is the char_traits
part, because char_traits defines how characters interact—and compare!
So let's compare
string
s.
basic_string supplies useful comparison functions that let you
compare whether one
string
is equal to another, less than another, and so on. These
string