C++中string的用法STL

在学习数据结构的时候,任何算法都是要自己写出来,非常考验逻辑思维能力,STL的出现,很多问题的处理都变得简单,常用算法都集成在STL里面,尽管面试基本上大多数只会问数据结构的细节,还是必须得非常了解STL,这是一个软件开发人员必须具备的能力。

1、用STL中的string来计算一对字符在一个字符串中出现的次数。(注:实际上是著名的KMP算法)

用到两个string类的方法

①求字符串的长度 length()or size()

②截取一个字符串的子字符串 substr(i,length)

用到一个运算符重载==

int count(string test,string pattern)
{
int i, len=test.length();
string temp;
int num=0;
if(pattern.length()>=len)
{
for(i=0;i<=pattern.length()-len;i++)
{
temp=pattern.substr(i,len);
if(temp==test)
num++;
}
}
return num;
}

2、用STL中的string来计算一对字符在一个字符串中出现的次数。(注:实际上是著名的KMP算法)

要求:从文件里面读一对字符和一个字符串,要使用fstream,从命令行里面读文件名

注意:文件名必须是字符数组型,就是char*,不能为string型,否则不能通过编译

①打开文件

fstream fp;

fp.open(char *);//必须分开写

或者直接用fstream的构造函数

fstream fp(char *);

②string型字符串的长度 length()

③可以用批处理,学习一个异常处理办法

比上一个程序多加了文件处理的语句:

string str1,str2,strtemp;
char filename[10];
cout<<"input the file name:"<<endl;
cin>>filename;

fstream fp;
fp.open(filename);

//或者前面两句可以改成 fstream fp(filename);
fp>>str1;
fp>>str2;
fp.close();

如果用批处理方式进行处理,并且加入异常处理操作的话,完整程序如下:

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main(int argc,char *argv[])
{
try
{
string str1,str2,strtemp;
if(argc<=1)
throw(0);
if(argc>=3)
throw(1);
ifstream cin(argv[1]);
cin>>str1;
cin>>str2;
int count=0;
int i;

int lenth1=str1.length();
if(str2.length()>=lenth1)
{
for(i=0;i<=str2.length()-lenth1;i++)
{
strtemp=str2.substr(i,lenth1);
if(strtemp==str1)
count++;
}
}

cout<<"The number of occurrences of a pair of letters "<<"\""<<str1<<"\""<<endl;
cout<<"int the string "<<"\""<<str2<<"\" is "<<count<<endl;
}

catch(int a)
{
if(a==0)
{
cerr<<"No file name input!";
cout<<endl;
cerr<<"You have to input a file name in command line";
cout<<endl;
}
if(a==1)
{
cerr<<"Too more file name input!";
cout<<endl;
cerr<<"You have to input only a file name in command line";
cout<<endl;
}

}
system("pause");
return 0;
}

3、从输入里面读入一系列单词,输入"Quit"退出,"Quit"不算在内,并且要求所有输入的单词不能有重复的。

要求:1)按单词升序排序 2)按单词降序排序 3)按单词长度升序排序 (本问选择选择排序)

使用STL中的vector,string,sort方法。

①不使用STL的算法,使用选择排序法

//Don't use <algorithm>

#include<iostream>
#include<vector>
#include<string>

using namespace std;


int main()
{
vector<string> strvec;
string word;
string temp;
int i,j;
int input;
cout<<"Enter a sequence of words ..."<<endl;
cout<<endl;
while(cin>>word && word!="Quit")
{
input=1;
for(i=0;i<strvec.size();i++)
{
if(word==strvec[i])
input=0;
}
if(input)
{
strvec.push_back(word);
}
}
cout<<endl;
cout<<"Words Entered:"<<endl;
cout<<endl;
for(i=0;i<strvec.size();i++)
cout<<strvec[i]<<endl;
cout<<endl;
//ascending sort

for(i=0;i<strvec.size()-1;i++)
for(j=i;j<strvec.size();j++)
{
if(strvec[i]>strvec[j])
{
temp=strvec[i];
strvec[i]=strvec[j];
strvec[j]=temp;
}
}
cout<<endl;
cout<<"After ascending sorting ... "<<endl;
cout<<endl;
for(i=0;i<strvec.size();i++)
cout<<strvec[i]<<endl;
cout<<endl;

//deascending sort

for(i=0;i<strvec.size()-1;i++)
for(j=i;j<strvec.size();j++)
{
if(strvec[i]<strvec[j])
{
temp=strvec[i];
strvec[i]=strvec[j];
strvec[j]=temp;
}
}
cout<<endl;
cout<<"After deascending sorting ... "<<endl;
cout<<endl;
for(i=0;i<strvec.size();i++)
cout<<strvec[i]<<endl;
cout<<endl;

// sort by lenth

for(i=0;i<strvec.size()-1;i++)
for(j=i;j<strvec.size();j++)
{
if(strvec[i].length()>strvec[j].length())
{
temp=strvec[i];
strvec[i]=strvec[j];
strvec[j]=temp;
}
}
cout<<endl;
cout<<"After sorting strings from short to long order ... "<<endl;
cout<<endl;
for(i=0;i<strvec.size();i++)
cout<<strvec[i]<<endl;
cout<<endl;

system("pause");
return 0;
}

②使用sort算法

//use <algorithm>

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

struct CriteriaStruct
{
bool operator( ) (const string a, const string b) const
{
return a>b;
} // Descending order
};

struct OrderByLength
{
bool operator() (const string a,const string b) const
{
return a.length()<b.length();
}
};

int main()
{
vector<string> strvec;
string word;
string temp;
int i,j;
int input;
cout<<"Enter a sequence of words ..."<<endl;
cout<<endl;
while(cin>>word && word!="Quit")
{
input=1;
for(i=0;i<strvec.size();i++)
{
if(word==strvec[i])
input=0;
}
if(input)
{
strvec.push_back(word);
}
}
cout<<endl;
cout<<"Words Entered:"<<endl;
cout<<endl;
for(i=0;i<strvec.size();i++)
cout<<strvec[i]<<endl;
cout<<endl;
//ascending sort

sort(strvec.begin(),strvec.end());
cout<<endl;
cout<<"After ascending sorting ... "<<endl;
cout<<endl;
for(i=0;i<strvec.size();i++)
cout<<strvec[i]<<endl;
cout<<endl;

//deascending sort
sort(strvec.begin(),strvec.end(), CriteriaStruct());
cout<<endl;
cout<<"After deascending sorting ... "<<endl;
cout<<endl;
for(i=0;i<strvec.size();i++)
cout<<strvec[i]<<endl;
cout<<endl;

// sort by lenth
sort(strvec.begin(),strvec.end(), OrderByLength());

cout<<endl;
cout<<"After sorting strings from short to long order ... "<<endl;
cout<<endl;
for(i=0;i<strvec.size();i++)
cout<<strvec[i]<<endl;
cout<<endl;

system("pause");
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值