|
By Alex, on July 18th, 2010 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: 22.7 — std::string inserting
By Alex, on July 18th, 2010 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: 22.6 — std::string appending
By Alex, on July 18th, 2010 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: 22.5 — std::string assignment and swapping
By Alex, on December 7th, 2008 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: 8.5 — Random number generation
By Alex, on October 26th, 2008 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: 20.8 — Exception dangers and downsides
By Alex, on October 26th, 2008 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:
|
int& IntArray::operator[](const int index) { return m_data[index]; } |
Although this function will work great as . . . → Read More: 20.5 — Exceptions, classes, and inheritance
By Alex, on October 25th, 2008 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: 20.4 — Uncaught exceptions and catch-all handlers
By Alex, on October 5th, 2008 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: 20.3 — Exceptions, functions, and stack unwinding
By Alex, on October 4th, 2008 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: 20.2 — Basic exception handling
By Alex, on October 4th, 2008 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: 20.1 — The need for exceptions
|
|