Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Thinking In C++, 2nd Edition, Volume 2 Standard Libraries& Advanced Topics - Eckel B..pdf
Скачиваний:
313
Добавлен:
24.05.2014
Размер:
2.09 Mб
Скачать

The STL replace( ) algorithm only works with single objects (in this case, char objects), and will not perform replacements of quoted char arrays or of string objects.

Since a string looks like an STL container, there are a number of other STL algorithms that can be applied to it, which may solve other problems you have that are not directly addressed by the string member functions. See Chapter XX for more information on the STL algorithms.

Concatenation using non-member overloaded operators

One of the most delightful discoveries awaiting a C programmer learning about C++ string handling is how simply strings can be combined and appended using operator+ and operator+=. These operators make combining strings syntactically equivalent to adding numeric data.

//: C01:AddStrings.cpp #include <string> #include <iostream> using namespace std;

int main() {

string s1("This "); string s2("That "); string s3("The other ");

//operator+ concatenates strings s1 = s1 + s2;

cout << s1 << endl;

//Another way to concatenates strings s1 += s3;

cout << s1 << endl;

//You can index the string on the right s1 += s3 + s3[4] + "oh lala";

cout << s1 << endl;

}///:~

The output looks like this:

This

This That

This That The other

This That The other ooh lala

Chapter 14: Templates & Container Classes

37

Соседние файлы в предмете Программирование