C++ Primer Plus 第六版 第16章源码

本文通过多个示例程序介绍了C++中标准库字符串类(String)的使用方法及特性,包括不同构造方法、运算符重载、常用成员方法等,并展示了智能指针模板类的使用场景。

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值