Search

17.7 — std::string inserting

Inserting

Inserting characters into an existing string can be done via the insert() function.

string& string::insert (size_type index, const string& str) string& string::insert (size_type index, const char* str)

Both functions insert the characters of str into the string at index Both function return *this so they can be “chained”. Both functions throw out_of_range if index . . . → Read More: 17.7 — std::string inserting

17.6 — std::string appending

Appending

Appending strings to the end of an existing string is easy using either operator+=, append(), or push_back() function.

string& string::operator+= (const string& str) string& string::append (const string& str)

Both functions append the characters of str to the string. Both function return *this so they can be “chained”. Both functions throw a length_error exception . . . → Read More: 17.6 — std::string appending

17.5 — std::string assignment and swapping

String assignment

The easiest way to assign a value to a string is to use the overloaded operator= function. There is also an assign() member function that duplicates some of this functionality.

string& string::operator= (const string& str) string& string::assign (const string& str) string& string::operator= (const char* str) string& string::assign (const char* str) string& string::operator= (char . . . → Read More: 17.5 — std::string assignment and swapping

5.9 — Random number generation

The ability to generate random numbers can be useful in certain kinds of programs, particularly in games, statistics modeling programs, and scientific simulations that need to model random events. Take games for example — without random events, monsters would always attack you the same way, you’d always find the same treasure, the dungeon layout would . . . → Read More: 5.9 — Random number generation

14.8 — Exception dangers and downsides

As with almost everything that has benefits, there are some potential downsides to exceptions as well. This article is not meant to be comprehensive, but just to point out some of the major issues that should be considered when using exceptions (or deciding whether to use them).

Cleaning up resources

One of the biggest problems . . . → Read More: 14.8 — Exception dangers and downsides

14.5 — Exceptions, classes, and inheritance

Exceptions and member functions

Up to this point in the tutorial, you’ve only seen exceptions used in non-member functions. However, exceptions are equally useful in member functions, and even moreso in overloaded operators. Consider the following overloaded [] operator as part of a simple integer array class:

Although this function will work great as . . . → Read More: 14.5 — Exceptions, classes, and inheritance

14.4 — Uncaught exceptions, catch-all handlers, and exception specifiers

By now, you should have a reasonable idea of how exceptions work. In this lesson, we’ll cover a few more interesting exception cases.

Uncaught exceptions

In the past few examples, there are quite a few cases where a function assumes its caller (or another function somewhere up the call stack) will handle the exception. In . . . → Read More: 14.4 — Uncaught exceptions, catch-all handlers, and exception specifiers

14.3 — Exceptions, functions, and stack unwinding

In the previous lesson on basic exception handling, we explained how throw, try, and catch work together to enable exception handling. In this lesson, we’ll talk about how exception handling interacts with functions.

Throwing exceptions outside of a try block

In the examples in the previous lesson, the throw statements were placed directly within a . . . → Read More: 14.3 — Exceptions, functions, and stack unwinding

14.2 — Basic exception handling

In the previous lesson on the need for exceptions, we talked about how using return codes causes your control flow and error flow to be intermingled, constraining both. Exceptions in C++ are implemented using three keywords that work in conjunction with each other: throw, try, and catch.

Throwing exceptions

We use signals all the time . . . → Read More: 14.2 — Basic exception handling

14.1 — The need for exceptions

In the previous lesson on handling errors, we talked about ways to use assert(), cerr(), and exit() to handle errors. However, we punted on one further topic that we will now cover: exceptions.

When return codes fail

When writing reusable code, error handling is a necessity. One of the most common ways to handle potential . . . → Read More: 14.1 — The need for exceptions