16.1.1 Construct string
The program listing 16.1 uses the 7 constructors of a string.
Program Listing 16.1 Str1.cpp
--------------------------------------------------
Str1.cpp--Introducing the String class
#include <iostream>
#include <string>
Using string constructors
int main ()
{
using namespace Std;
String One ("Lottery winner!"); ctor #1
cout << one << Endl; Overloaded <<
String (20, ' $ '); ctor #2
cout << both << Endl;
String three (one); ctor #3
cout << three << Endl;
One + = "oops!";
cout << one << Endl;
both = "sorry! That was ";
Three[0] = ' P ';
String Four; ctor #4
Four = both + three; overloaded +, =
cout << four << Endl;
Char alls[] = "All's Well", Ends Well ";
String Five (alls, 20); ctor #5
cout << Five << "!\n";
String Six (alls+6, Alls + 10); ctor #6
cout << six << Endl;
String seven (&five[6], &five[10]); COTR #6
cout << seven << "... \ n";
String Eight (four, 7, 16);
cout << eight << "in motion!" << Endl;
return 0;
}
--------------------------------------------------
Effect:
Lottery winner!
$$$$$$$$$$$$$$$$$$$$
Lottery winner!
Lottery winner! oops!
sorry! That was Pottery winner!
All ' s well that ends!
Well
Well ...
That is Pottery in motion!
--------------------------------------------------
The constructor string (initializer_list<char> il) allows you to use the list initialization syntax with the string class. In other words, it makes the following life legal:
String Piano_man = {' L ', ' I ', ' s ', ' z ', ' t '};
String Comp_lang {' L ', ' I ', ' s ', ' P '};
16.1.2 String Class input
For C-style strings, there are 3 ways to enter:
Char info[100];
CIN >> Info; Read a Word
Cin.getline (info, 100); Read a line, discard \ n
Cin.get (info, 100); Read a line, leave \ n in queue
For string objects, there are two ways of doing this:
string stuff;
CIN >> Stuff; Read a Word
Getline (cin, stuff); Read a line, discard \ n
Two versions of Getline () have an optional parameter that specifies which character to use to determine the bounds of the input:
Cin.getline (info, 100, ': '); Read up to:, Discard:
Getline (Stuff, ': '); Read up to:, Discard:
Functionally, the main difference between a C-style string and a string is that the string version of Getline () automatically adjusts the size of the target string object so that it can store the input characters exactly.
16.1.3 Using Strings
......
16.1.4 string also provides what features
......
16.1.5 String Types
This section considers the string class to be based on the char type. In fact, as noted earlier, the string library is actually based on a template class:
Template<class CharT, class traits = char, traits<chart>
Class Allocator = allocator<chart> >
basic_string {...};
The template basic_string has 4 materialization, and each materialization has a typedef name:
typedef basic_string<char> String;
typedef basic_string<wchar_t> Wstring;
typedef basic_string<char16_t> U16string; C++11
typedef basic_string<char32_t> U32string; C++11
This allows you to use strings based on type wchar_t, wchar16_t, char32_t, and Char.
"C + + Primer Plus" 16.1 String class learning Notes