@[TOC](C++ string 类)
string 类
//str1.cpp -- introducing the string class
/*
* string(const char *s)
* string(size_type n, char c)
* string(const string & str)
* string()
* string(const char * s, size_type n)
*
* temple<class Iter>
* string(Iter begin, Iter end)
*
* string(const string & str, size_type pos, size_type n=npos)
* string(string && str) noexpect
* string(initializer_list<char> il)
*/
#include <iostream>
#include <string>
int main()
{
using namespace std;
string one("Lottery Winner!");
cout << one << endl;
string two(20, '$');
cout << two << endl;
string three(one);
cout << three << endl;
one += "Oops!";
cout << one << endl;
two = "Sorry! That was ";
three[0] = 'P';
string four;
four = two + three;
cout << four << endl;
char alls[] = "All's well that ends well";
string five(alls, 20);
cout << five << "!\n";
string six(alls + 6, alls + 10);
cout << six << ", ";
string seven(&five[6], &five[10]);
cout << seven << "...\n";
string eight(four, 7, 16);
cout << eight << " in motion!" << endl;
return 0;
}
Lottery Winner!
$$$$$$$$$$$$$$$$$$$$
Lottery Winner!
Lottery Winner!Oops!
Sorry! That was Pottery Winner!
All’s well that ends!
well, well…
That was Pottery in motion!