使用字符串
现在,您知道可以使用不同方式来创建 string 对象、显示 string 对象的内容、将数据读取和附加到 string 对象中、给 string 对象赋值以及将两个 string 对象连结起来。除此之外,还能做些什么呢?
可以比较字符串。String 类对全部 6 个关系运算符都进行了重载。如果在机器排列序列中,一个对象位于另一个对象的前面,则前者被视为小于后者。如果机器排列序列为 ASCII 码,则数字将小于大写字符,而大写字符小于小写字符。对于每个关系运算符,都以三种方式被重载,以便能够将string对象与另一个string对象、C-风格字符串进行比较,并能将C-风格字符串与 string 对象进行比较:
string snake1("cobra");
string snake2("coral");
char snake3[20] = "anaconda";
if (snake1 < snake2) // operator<(const string &, const string &)
...
if (snake1 == snake3) // operator==(const string &, const char *)
...
if (snake3 != snake2) // operator!=(const char * , const string &)
...
要确定字符串的长度。size() 和 length() 成员函数都返回字符串中的字符数:
if (snake1.length() == snake2.length() )
cout << "Both strings have the same length.\n"
为什么这两个函数完成相同的任务呢?length() 成员来自较早版本的 string 类,而 size() 则是为提供 STL 兼容性而添加的。
可以以多种不同的方式在字符串中搜索给定的子字符串或字符。下表简要地描述了 find() 方法的4个版本。如前所述,string::npos 是字符串可存储的最大字符数,通常是无符号 int 或无符号 long 的最大取值。
方法原型 | 描述 |
---|---|
size_type find(const string & str, size_type pos = 0 ) const | 从字符串的 pos 位置开始,查找子字符串 str。如果找到,则返回该字符串首次出现时其首字符的索引;否则,返回string::npos |
size_type fine(const char * s, size_type pos = 0) const | 从字符串的pos位置开始,查找子字符串s。如果找到,则返回该字符串首次出现时其首字符的索引;否则,返回 string::npos |
size_type find(const char * s, size_type pos = 0, size_type n ) | 从字符串的 pos 位置开始,查找s的前n个字符组成的子字符串。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回 string::npos |
size_type fine(char ch, size_type pos = 0) const | 从字符串的 pos 位置开始,查找字符 ch。如果找到,则返回该字符首次出现的位置;否则,返回 string:npos |
string 库还提供了相关的方法:rfind()、find_first_of()、find_last_of()、find_first_not_of() 和 find_last_not_of(),它们的重载函数特征标都与 find() 方法相同。rfind() 方法查找子字符串或字符最后一次出现的位置;find_first_of() 方法在字符串中查找参数中任何一个字符首次出现的位置。例如,下面的语句返回 r 在 “cobra” 中的位置(即索引3),因为这是 “hark” 中各个字母在 “cobra” 首次出现的位置:
int where = snake1.find_first_of("hark");
find_last_of() 方法的功能与此相同,只是它查找的是最后一次出现的位置。因此,下面的语句返回 a 在 “cobra” 中的位置:
int where = snake1.find_last_of("hark");
find_first_not_of() 方法在字符串中查找第一个不包含在参数中的字符,因此下面的语句返回 c 在“cobra”中的位置,因为“hark”中没有 c:
int where = snake1.find_first_not_of("hark");
在本章最后的练习中,您将了解 find_last_not_of()。
还有很多其他的方法,这些方法足以创建一个非图形版本的 Hangman 拼字游戏。该游戏将一系列的单词存储在一个 string 对象数组中,然后随机选择一个单词,让人猜测单词的字母。如果猜错6次,玩家就输了。该程序使用 find() 函数来检查玩家的猜测,使用 += 运算符创建一个 string 对象来记录玩家的错误猜测。为记录玩家猜对的情况,程序创建了一个单词,其长度与被猜的单词相同,但包含的是连字符。玩家猜对字符时,将用该字符替换相应的连字符。下面的程序列出了该程序的代码。
// hangman.cpp -- some string methods
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<cctype>
using std::string;
const int NUM = 26;
const string wordlist[NUM] = {
"apiary", "beetle", "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. Try again.\n";
continue;
}
int loc = target.find(letter);
if (loc == string::npos){
cout << "Oh, bad guess!\n";
--guesses;
badchars += letter; // add to string
}
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);
}
cout << "Bye\n";
return 0;
}
该程序的运行情况如下:
Will you play a word game? <y/n> y
Guess my secret word. It has 6 letters, and you guess
one letter at a time. You get 6 wrong guesses.
Your word: ------
Guess a letter: i
Good guess!
Your word: ----i-
6 bad guesses left
Guess a letter: j
Oh, bad guess!
Your word: ----i-
Bad choices: j
5 bad guesses left
Guess a letter: l
Good guess!
Your word: ---li-
Bad choices: j
5 bad guesses left
Guess a letter: a
Oh, bad guess!
Your word: ---li-
Bad choices: ja
4 bad guesses left
Guess a letter: w
Oh, bad guess!
Your word: ---li-
Bad choices: jaw
3 bad guesses left
Guess a letter: h
Oh, bad guess!
Your word: ---li-
Bad choices: jawh
2 bad guesses left
Guess a letter: r
Oh, bad guess!
Your word: ---li-
Bad choices: jawhr
1 bad guesses left
Guess a letter: m
Oh, bad guess!
Your word: ---li-
Bad choices: jawhrm
0 bad guesses left
Sorry, the word is stolid.
Will you play another? <y/n>y
Guess my secret word. It has 6 letters, and you guess
one letter at a time. You get 6 wrong guesses.
Your word: ------
Guess a letter: j
Oh, bad guess!
Your word: ------
Bad choices: j
5 bad guesses left
Guess a letter: i
Good guess!
Your word: i-----
Bad choices: j
5 bad guesses left
Guess a letter: k
Oh, bad guess!
Your word: i-----
Bad choices: jk
4 bad guesses left
Guess a letter: n
Good guess!
Your word: in----
Bad choices: jk
4 bad guesses left
Guess a letter: s
Good guess!
Your word: ins---
Bad choices: jk
4 bad guesses left
Guess a letter: u
Good guess!
Your word: insu--
Bad choices: jk
4 bad guesses left
Guess a letter: k
You already guessed that. Try again.
Guess a letter: m
Oh, bad guess!
Your word: insu--
Bad choices: jkm
3 bad guesses left
Guess a letter: l
Good guess!
Your word: insul-
Bad choices: jkm
3 bad guesses left
Guess a letter: t
Good guess!
Your word: insult
That's right!
Will you play another? <y/n>n
Bye
程序说明
在上面的程序中,由于关系运算符被重载,因此可以像对待数值变量那样对待字符串:
while (guesses > 0 && attempt != target)
与对 C-风格字符串使用 strcmp() 相比,这样简单些。
该程序使用 find() 来检查玩家以前是否猜过某个字符。如果是,则它要么位于 badchars 字符串(猜错)中,要么位于 attempt 字符串(猜对)中:
if(badchars.find(letter) != string::npos || attempt.find(letter) != string::npos)
npos 变量是 string 类的静态成员,它的值是 string 对象能存储的最大字符数。由于索引从 0 开始,所以它比最大的索引值大1,因此可以使用它来表示没有查找到字符或字符串。
该程序利用了这样一个事实:+=运算符的某个重载版本使得能够将一个字符附加到字符串中:
badchars += letter; // append a char to a string object
该程序的核心是从检查玩家选择的字符是否位于被猜测的单词中开始的:
int loc = target.find(letter);
如果 loc 是一个有效的值,则可以将该字母放置在答案字符串的相应位置:
attempt[loc] = letter;
然而,由于字母在被猜测的单词中可能出现多次,所以程序必须一直进行检查。该程序使用了 find() 的第二个可选参数,该参数可以指定从字符串什么位置开始搜索。因为字母是在位置loc找到的,所以下一次搜索应从loc+1开始。while循环使搜索一直进行下去,知道找不到该字符为止。如果loc位于字符串尾,则表明find() 没有找到该字符。
// check if letter appears again
loc = target.find(letter, loc+1);
while(loc!=string::npos){
attempt[loc] = letter;
loc = target.find(letter, loc+1);
}
string 还提供了哪些功能
string 库提供了很多其他的工具,包括完成下述功能的函数:删除字符串的部分或全部内容、用一个字符串的部分或全部内容替换另一个字符串的部分或全部内容、将数据插入到字符串中或删除字符串中的数据、将一个字符串的部分或全部内容与另一个字符串的部分或全部内容进行比较、从字符串中提取子字符串、将一个字符串的内容复制到另一个字符串中、交换两个字符串的内容。这些函数中的大多数都被重载,以便能够同时处理C-风格字符串和string对象。附录F简要地介绍了string库中的函数。
首先来看自动调整大小的功能。在上面的程序中,每当程序将一个字母附加到字符串末尾时将发生什么呢?不能仅仅将已有的字符串加大,因为相邻的内存可能被占用了。因此,可能需要分配一个新的内存块,并将原来的内容复制到新的内存单元中。如果执行大量这样的操作,效率将非常低,因此很多C++实现分配一个比实际字符串大的内存块,为字符串提供了增大空间。然而,如果字符串不断增大,超过了内存块的大小,程序将分配一个大小为原来两倍的新内存块,以提供足够的增大空间,避免不断地分配新的内存块。方法 capacity() 返回当前分配给字符串的内存块的大小,而 reserve() 方法让您能够请求内存块的最小长度。
下面的程序时一个使用这些方法的示例:
// str2.cpp -- capacity() and 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 << "Size:\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;
return 0;
}
这是使用某种C++实现时,上面程序的输出:
Size:
empty: 0
small: 3
larger: 34
Capacities:
empty: 15
small: 15
larger: 34
Capacity after empty.reserve(50): 50
注意,该实现使用的最小容量为 15 个字符,这比标准容量选择(16的倍数)小1。其他实现可能做出不同的选择。
如果您有 string 对象,但需要 C-风格字符串,该如何办呢?例如,您可能打开一个其名称存储在 string 对象中的文件:
string filename;
cout << "Enter file name: ";
cin >> filename;
ofstream fout;
不幸的是,open() 方法要求使用一个 C-风格字符串作为参数;幸运的是,c_str() 方法返回一个指向 C- 风格字符串的指针,该 C-风格字符串的内容与用于调用 c_str() 方法的 string 对象相同。因此可以这样做:
fout.open(filename.c_str());
字符串种类
本节将 string 类看作是基于 char 类型的。事实上,正如前面指出的,string 库实际上是基于一个模板类的:
template<class char T, class traits = char_traits<charT>, class Allocator = allocator<charT> >
basic_string { ... };
模板 basic_string 有 4 个具体化,每个具体化都有一个 typedef 名称:
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
这让您能够使用基于类型 wchar_t、char16_t、char32_t 和 char 的字符串。甚至可以开发某种类似字符的类,并对它使用basic_string 类模板(只要它满足某些要求)。traits 类描述关于选定字符类型的特定情况,如如何对值进行比较。对于 wchar_t、char16_t、char32_t 和 char 类型,有预定义的 char_traits 模板具体化,它们都是 traits 的默认值。Allocator 是一个管理内存分配的类。对于各种字符类型,都有预定义的 allocator 模板具体化,它们都是默认的。它们使用 new 和 delete。