一、string类介绍
string类位于std名称空间中,使用时std::string来引用,或者生命std的命名空间using namespace std,使用时可以省略std::。
1.string简单的使用demon
#include<iostream>
#include<string>
int main()
{
using namespace std;
char array[20];
char array1[20]="hello";
string str1;
string str2="world";
cout<<"输入数据到数组array中:";
cin>>array;
cout<<"输入数据到数组str1中:";
cin>>str1;
cout<<"array:"<<array<<endl;
cout<<"array1:"<<array1<<endl;
cout<<"str1:"<<str1<<endl;
cout<<"str2:"<<str2<<endl;
cout<<"str2[1]:"<<str2[1]<<endl;
return 0;
}
运行结果:
二、string初试化
string类的初试化同样可以像c语言那样初试化字符串
string str1={"hello world!"};
string str2 {"hello stranger!"}
三、string类间的转换
1.赋值,可以直接将一个string类赋值给另外一个string类
string str1;
string str2="hello stranger!";
str1=str2;
2.合并操作
string str1="hello";
string str2="world!"
string str3;
str3=str1+str2;//将str1和str2拼接到str3中
str1 +=str2;//将str2的内容加到str1的末尾
3.计算大小
str1="hello";
int len;
len=str1.size();
四、输入输出
输入
string str1;
getline(cin,str1);
五、find相关
string STR("start");
char a = 'a';
STR.find(a); //查找字符串STR中有无a,有返索引位置,没有返回npos
STR.find(a,pos);//查找字符串STR的pos位置起始到结束位置有无a,有返回索引位置,无返回npos
STR.find_first_of(a);//返回a在STR中第一次出现的索引位置
STR.find_last_of(a);//返回a在STR中最后一次出现的索引位置
STR.find_first_not_of("hello");//查找第一个不包含参数字符的索引,返回s的位置,因为hello中无s
【注】:npos变量是string的静态成员,它的值是string对象能存储的最大字符数。
demon——猜字游戏
#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","quit","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 << "是否准备好开始游戏?<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 << "猜测一下长度是" << length << "个字母的单词\n"
<< "每次可以输入一个字母,你有" << guesses << "次错误的机会\n";
cout << "猜测的对应位置: " << attempt << endl;
while( guesses > 0 && attempt != target)
{
char letter;
cout << "输入你猜测的字母: ";
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;
}
else
{
cout << "[" << __LINE__ << "]" <<"good guess!\n";
attempt[loc] = letter;
cout << "[" << __LINE__ << "]" << "===========loc is "<< loc << "| attempt is "<< attempt <<endl;
loc = target.find(letter,loc+1);
while(loc != string::npos)
{
cout << "[" << __LINE__ << "]" << "===========loc is "<< loc << "| attempt is "<< attempt <<endl;
attempt[loc] =letter;
cout << "[" << __LINE__ << "]" << "===========loc is "<< loc << "| attempt is "<< attempt <<endl;
loc = target.find(letter,loc+1);
}
}
cout << "you word: " << attempt << endl;
if( attempt != target)
{
if(badchars.length() > 0)
cout << "bad choices: " << badchars <<endl;
cout << guesses << "bad guess left\n";
}
}
if(guesses > 0)
cout << "That's right!\n";
else
cout << "sorry,the word is " << target << ".\n";
cout <<"will you play another?\n <y/n>";
cin >> play;
play = tolower(play);
}
cout << "Bye\n";
return 0;
}
//摘自c++ Primer plus
运行结果: