16.1 String类
程序清单 16.1 str1.cpp
#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;
cin.get();
return 0;
}
程序清单 16.2 strfile.cpp
//从文件中读取若干字符串
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
int main()
{
using namespace std;
ifstream fin;
fin.open("tobuy.txt");
if(fin.is_open()==false)
{
cerr<<"Can't open file.Bye.\n";
exit(EXIT_FAILURE);
}
string item;
int count=0;
getline(fin,item,':');
while(fin)
{
++count;
cout<<count<<":"<<item<<endl;
getline(fin,item,':');
}
cout<<"Done\n";
fin.close();
system("pause");
return 0;
}
程序清单 16.3 hangman.cpp
//字符串常用方法
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<cctype>
using std::string;
const int NUM = 26;
const string wordlist[NUM] = { "apiary","bettle","cereal",
"danger","ensign","florid","garage","health","insult",
"jackal","keeper","loaner","manage","nonce","onset",
"plaid","quilt","remote","stolid","train","useful",
"valid","whence","xenon","yearn","zippy" };
int main()
{
using std::cout;
using std::cin;
using std::tolower;
using std::endl;
std::srand(std::time(0));
char play;
cout << "Will you play a word game?<y/n>";
cin >> play;
play = tolower(play);
while (play == 'y')
{
string target = wordlist[std::rand() % NUM];
int length = target.length();
string attempt(length, '-');
string badchars;
int guesses = 6;
cout << "Guess my secret word.It has " << length
<< " letters, and you guess\n"
<< "one letter at a time. You get " << guesses
<< " wrong guesses.\n";
cout << "Your word: " << attempt << endl;
while (guesses>0 && attempt != target)
{
char letter;
cout << "Guess a letter: ";
cin >> letter;
if (badchars.find(letter) != string::npos
|| attempt.find(letter) != string::npos)
{
cout << "You already guessed that. Tray again.\n";
continue;
}
int loc = target.find(letter);
if (loc == string::npos)
{
cout << "Oh,bad guess!\n";
--guesses;
badchars += letter;
}
else
{
cout << "Good guess!\n";
attempt[loc] = letter;
//check if letter appears again
loc = target.find(letter, loc + 1);
while (loc != string::npos)
{
attempt[loc] = letter;
loc = target.find(letter, loc + 1);
}
}
cout << "Your word: " << attempt << endl;
if (attempt != target)
{
if (badchars.length()>0)
cout << "Bad choices:" << badchars << endl;
cout << guesses << " bad guesses left\n";
}
}
if (guesses>0)
cout << "That's right!\n";
else
cout << "Sorry,the word is " << target << ".\n";
cout << "Will you play another?<y/n>";
cin >> play;
play = tolower(play);
}//end while
cout <<"Bye\n";
cin.get();
return 0;
}
程序清单 16.4 str2.cpp
// string capacity()和reserve()用法示例
#include<iostream>
#include<string>
int main()
{
using namespace std;
string empty;
string small = "bit";
string larger = "Elephants are a girl's best friend";
cout << "Sizes:\n";
cout << "\tempty: " << empty.size() << endl;
cout << "\tsmall: " << small.size() << endl;
cout << "\tlarger " << larger.size() << endl;
cout << "Capacities:\n";
cout << "\tempty: " << empty.capacity() << endl;
cout << "\tsmall: " << small.capacity() << endl;
cout << "\tlarger " << larger.capacity() << endl;
empty.reserve(50);
cout << "Capacity after empty.reserve(50): "
<< empty.capacity() << endl;
cin.get();
return 0;
}
16.2 智能指针模板类
程序清单 16.5 smrtptrs.cpp