Online Book Reader

Home Category

Professional C__ - Marc Gregoire [249]

By Root 1475 0
with operator[].

As the following code shows, when string operations require extending the string, the memory requirements are automatically handled by the string class, so memory overruns are a thing of the past:

string myString = "hello";

myString += ", there";

string myOtherString = myString;

if (myString == myOtherString) {

myOtherString[0] = 'H';

}

cout << myString << endl;

cout << myOtherString << endl;

Code snippet from CppStrings\CppStrings.cpp

The output of this code is:

hello, there

Hello, there

There are several things to note in this example. One point to note is that there are no memory leaks even though strings are allocated and resized left and right. All of these string objects were created as stack variables. While the string class certainly had a bunch of allocating and resizing to do, the string destructors cleaned up this memory when string objects went out of scope.

Another point to note is that the operators work the way you would want them to. For example, the = operator copies the strings, which is most likely what you wanted. If you are used to working with array-based strings, this will either be refreshingly liberating for you or somewhat confusing. Don’t worry — once you learn to trust the string class to do the right thing, life gets so much easier.

For compatibility, you can use the c_str() method on a string to get a const character pointer, representing a C-style string. However, the returned const pointer becomes invalid whenever the string has to perform any memory reallocation, or when the string object is destroyed. You should call the method just before using the result so that it accurately reflects the current contents of the string.

The Standard Library Reference resource on the website lists all the operations you can perform on string objects.

Numeric Conversions

C++11 includes a number of new helper functions making it easier to convert numerical values into strings or strings into numerical values. The following functions are available to convert numerical values into strings:

string to_string(int val);

string to_string(unsigned val);

string to_string(long val);

string to_string(unsigned long val);

string to_string(long long val);

string to_string(unsigned long long val);

string to_string(float val);

string to_string(double val);

string to_string(long double val);

They are pretty straightforward to use. For example, the following code converts a long double value into a string:

long double d = 3.14;

string s = to_string(d);

There are also wide string versions available, which are called to_wstring and return a wstring. Wide strings are discussed later in this chapter.

Converting in the other direction is done by the following set of functions. In these prototypes, str is the string that you want to convert, idx is a pointer that will receive the index of the first non-converted character, and base is the mathematical base that should be used during conversion. The idx pointer can be a null pointer in which case it will be ignored. They throw invalid_argument if no conversion could be performed and throw out_of_range if the converted value is outside the range of the return type.

int stoi(const string& str, size_t *idx=0, int base=10);

long stol(const string& str, size_t *idx=0, int base=10);

unsigned long stoul(const string& str, size_t *idx=0, int base=10);

long long stoll(const string& str, size_t *idx=0, int base=10);

unsigned long long stoull(const string& str, size_t *idx=0, int base=10);

float stof(const string& str, size_t *idx=0);

double stod(const string& str, size_t *idx=0);

long double stold(const string& str, size_t *idx=0);

For example:

const string s = "1234";

int i = stoi(s); // i will be 1234

A similar set of functions is available accepting a wstring instead of a string.

Raw String Literals

C++11 adds the concept of raw string literals, which are string literals where escape sequences like \t and \n are not processed as escape sequences but as normal text. These escape characters are discussed in Chapter

Return Main Page Previous Page Next Page

®Online Book Reader