C++ Programming Cookbook Herb Schildt’s

Computer Programming and IT Books ITBooksHub.com
Click Here To Download Book
Pages 529
Views 16657
Size 4.2 MB
Download 109

Table Of Contents7



What’s Inside21

How the Recipes Are Organized22

A Few Words of Caution23

C++ Experience Required23

What Version of C++?24

Two Coding Conventions24

Returning a Value from main( )24

Using Namespace std?24

String Handling27

Overview of Null-Terminated Strings28

Overview of the stringClass31

String Exceptions36

Perform Basic Operations on Null-Terminated Strings36

Search a Null-Terminated String40

Options and Alternatives42

Reverse a Null-Terminated String43

Options and Alternatives45

Ignore Case Differences When Comparing Null-Terminated Strings47

Options and Alternatives51

Create a Search-and-Replace Function for Null-Terminated Strings51

Options and Alternatives56

Categorize Characters Within a Null-Terminated String59

Bonus Example: Word Count61

Options and Alternatives63

Tokenize a Null-Terminated String64

Options and Alternatives67

Perform Basic Operations on stringObjects71

Options and Alternatives78

Search a stringObject79

Bonus Example: A Tokenizer Class for stringObjects83

Options and Alternatives85

Create a Search-and-Replace Function for stringObjects86

Options and Alternatives89

Operate on stringObjects Through Iterators90

Options and Alternatives95

Create Case-Insensitive Search and Search-and-Replace Functions for stringObjects96

Options and Alternatives101

Convert a stringObject into a Null-Terminated String103

Options and Alternatives105

Implement Subtraction for stringObjects105

Options and Alternatives110

Working with STL Containers113

STL Overview114


Function Objects115



Binders and Negators116

The Container Classes116

Common Functionality118

Performance Issues121

Basic Sequence Container Techniques122

Options and Alternatives129

Use vector131

Options and Alternatives138

Use deque138

Options and Alternatives144

Use list144

Options and Alternatives150

Use the Sequence Container Adaptors: stack, queue and priority_queue152

Bonus Example: Use stackto Create a Four-Function Calculator157

Options and Alternatives160

Store User-Defined Objects in a Container160

Options and Alternatives164

Basic Associative Container Techniques165

Options and Alternatives175

Options and Alternatives182

Options and Alternatives187

Use setand multiset189

Bonus Example: Use multisetto Store Objects with Duplicate Keys194

Options and Alternatives198

Algorithms, Function Objects, and Other STL Components201

Algorithm Overview202

Why Algorithms?202

Algorithms Are Template Functions202

The Algorithm Categories203

Function Object Overview204

Binders and Negators Overview208

Sort a Container209

Options and Alternatives211

Find an Element in a Container212

Bonus Example: Extract Sentences from a Vector of Characters215

Options and Alternatives217

Use search( ) to Find a Matching Sequence219

Options and Alternatives222

Reverse, Rotate, and Shuffle a Sequence223

Bonus Example: Use Reverse Iterators to Perform a Right-Rotate226

Options and Alternatives227

Cycle Through a Container with for_each( )228

Options and Alternatives230

Use transform( )to Change a Sequence231

Options and Alternatives234

Perform Set Operations237

Options and Alternatives241

Permute a Sequence242

Options and Alternatives244

Copy a Sequence from One Container to Another245

Options and Alternatives247

Replace and Remove Elements in a Container247

Options and Alternatives250

Merge Two Sorted Sequences251

Options and Alternatives254

Create and Manage a Heap255

Options and Alternatives258

Create an Algorithm258

Bonus Example: Use a Predicate with a Custom Algorithm262

Options and Alternatives264

Use a Built-In Function Object265

Options and Alternatives268

Create a Custom Function Object268

Bonus Example: Use a Function Object to Maintain State Information273

Options and Alternatives275

Use a Binder275

Options and Alternatives278

Use a Negator279

Options and Alternatives281

Use the Pointer-to-Function Adaptor282

Options and Alternatives285

Use the Stream Iterators285

Bonus Example: Create an STL-Based File Filter292

Options and Alternatives293

Use the Insert Iterator Adaptors294

Options and Alternatives297

Working with I/O299

I/O Overview300

C++ Streams300

The C++ Stream Classes301

The Stream Class Specializations305

C++’s Predefined Streams307

The Format Flags307

The I/O Manipulators307

Checking for Errors308

Opening and Closing a File309

Write Formatted Data to a Text File313

Options and Alternatives316

Read Formatted Data from a Text File316

Options and Alternatives320

Write Unformatted Binary Data to a File320

Options and Alternatives324

Read Unformatted Binary Data from a File325

Options and Alternatives329

Use get( )and getline( )to Read from a File330

Options and Alternatives333

Read from and Write to a File334

Options and Alternatives337

Detecting EOF337

Bonus Example: A Simple File-Comparison Utility340

Options and Alternatives342

Use Exceptions to Detect and Handle I/O Errors342

Options and Alternatives346

Use Random-Access File I/O346

Bonus Example: Use Random-Access I/O to Access Fixed-Size Records349

Options and Alternatives352

Look Ahead in a File352

Options and Alternatives356

Use the String Streams357

Options and Alternatives360

Create Custom Inserters and Extractors361

Options and Alternatives364

Create a Parameterless Manipulator364

Options and Alternatives367

Create a Parameterized Manipulator368

Options and Alternatives372

Obtain or Set a Stream’s Locale372

Options and Alternatives375

Use the C-Based File System375

Options and Alternatives381

Rename and Remove a File383

Options and Alternatives385

Formatting Data387

Formatting Overview388

The Format Flags388

The Field Width, Precision, and Fill Character389

Format-Related Stream Member Functions390

The I/O Manipulators390

Format Data Using the Localization Library390

The printf( )Family of Functions391

The strftime( )Function391

Access the Format Flags via Stream Member Functions394

Bonus Example: Display the Format Flag Settings396

Options and Alternatives398

Display Numeric Values in Various Formats399

Options and Alternatives402

Set the Precision403

Options and Alternatives404

Set the Field Width and Fill Character405

Bonus Example: Line Up Columns of Numbers407

Options and Alternatives408

Justify Output408

Options and Alternatives411

Use I/O Manipulators to Format Data411

Options and Alternatives415

Format Numeric Values for a Locale415

Options and Alternatives417

Format Monetary Values Using the money_putFacet418

Options and Alternatives421

Use the moneypunctand numpunctFacets422

Options and Alternatives425

Format Time and Date Using the time_putFacet427

Options and Alternatives431

Format Data into a String432

Options and Alternatives434

Format Time and Date Using strftime( )434

Options and Alternatives437

Use printf( )to Format Data438

Options and Alternatives444


Operator Overloading Basic Techniques446

Options and Alternatives455

Overload the Function Call Operator ( )457

Options and Alternatives460

Overload the Subscripting Operator [ ]461

Options and Alternatives465

Overload the –>Operator465

Bonus Example: A Simple Safe Pointer Class467

Options and Alternatives471

Overload newand delete471

Options and Alternatives476

Overload the Increment and Decrement Operators477

Options and Alternatives482

Create a Conversion Function483

Options and Alternatives486

Create a Copy Constructor486

Bonus Example: A Safe Array that Uses Dynamic Allocation491

Options and Alternatives497

Determine an Object’s Type at Runtime498

Options and Alternatives504

Use Complex Numbers504

Options and Alternatives507

Use auto_ptr507

Options and Alternatives510

Create an Explicit Constructor511

Options and Alternatives514