本文介绍STL中的string的用法示例代码。
// string 的典型操作
//字符串的初始化
#include "iostream"
using namespace std;
#include "string"
#include "algorithm"
void main11()
{
string s1 = "aaaaa";
string s2("bbbb");
string s3 = s2;//通过copy构造函数初始化s3
string s4(10,'a');//打印10个a
cout << "s1:" << s1<<endl;
cout << "s2:" << s2<<endl;
cout << "s3:" << s3<<endl;
cout << "s4:" << s4<<endl;
}
//string 的遍历
void main12()
{
string s1 = "abcdefg";
// 数组方法
for (int i=0;i<s1.length();i++)
{
cout << s1[i] << " ";
}
cout << endl;
// 迭代器的方法
for (string::iterator it = s1.begin();it != s1.end();it++)
{
cout << *it << " ";
}
cout << endl;
try
{
for (int i=0;i<s1.length()+3;i++)
{
cout << s1.at(i) << " ";// at 可以抛出异常
}
cout << endl;
}
catch (...)
{
cout << "发生异常\n";
}
cout << endl;
try
{
for (int i=0;i<s1.length()+3;i++)
{
cout << s1[i] << " ";//出现错误不向外面抛出异常 引起程序的中断
}
cout << endl;
}
catch (...)
{
cout << "发生异常\n";
}
}
// 字符指针和string的转换
void main13()
{
string s1 = "aaabbb";
// s1 ===> char *
printf("s1:%s\n",s1.c_str());
// s1 的内容copy到 buf 中
char buf1[128] = {0};
s1.copy(buf1,3,0);
cout << "buf1:" << buf1 << endl;
}
// 字符串的连接
void main14()
{
string s1 = "aaa";
string s2 = "bbb";
s1 = s1 + s2;
cout << s1 << endl;
string s3 = "333";
string s4 = "444";
s3.append(s4);
cout << s3 << endl;
}
//字符串的查找和替换
//查找
// int find(char c,int pos=0) const; 从pos开始查找字符c在当前字符串的位置
// int find(const char *s,int pos=0) const;从pos开始查找字符串s在当前字符串的位置
// 如果查不到 就返回-1
// int rfind(char c,int pos=npos) const; 从pos开始从后向前查找字符c在当前字符串的位置
// int rfind(const char *s,int pos=0) const;
// int rfind(const char &s,int pos=0) const;
// rfind 是反向查找的意思,如果查不到,返回-1
// 替换
//string &replace(int pos,int n,const char *s);//删除从pos开始的n个字符,然后再pos处插入串s
//string &replace(int pos,int n,const char string &s);//删除从pos开始的n个字符,然后再pos处插入串s
void main15()
{
string s1 = "zcbvchgtwbmui234 wbm 999 wbm sdg wbm ";
int index = s1.find("wbm",0);// 下标从0开始
cout << "index:" << index << endl;
//案例 1 求wbm出现的次数 每一次出现的数组下标
int offindex = s1.find("wbm",0);
while (offindex != string::npos)
{
cout << "offindex:" << offindex << endl;
offindex = offindex +1;
offindex = s1.find("wbm",offindex);
}
// 案例2 替换 把小写换成大写
string s3 = "aaa bbb ccc";
s3.replace(0,3,"AAA");
cout << s3 << endl;
offindex = s1.find("wbm",0);
while (offindex != string::npos)
{
cout << "offindex:" << offindex << endl;
s1.replace(offindex,3,"WBM");
offindex = offindex +1;
offindex = s1.find("wbm",offindex);
}
cout << "替换后的结果" << s1 << endl;
}
// 截断(区间删除)和插入
void main16()
{
string s1 = "hello cvbvhelloc 123 liucheng 7hello978f";
string::iterator it = find(s1.begin(),s1.end(),'l');
if (it != s1.end())
{
s1.erase(it);
}
cout << "删除后:" << s1 << endl;
s1.erase(s1.begin(),s1.end());// 删除全部
cout << s1 << endl;
// 插入
string s2 = "BBB CCC";
s2.insert(0,"AAA");// 头插法
cout << s2 << endl;
s2.insert(s2.length(),"DDD");
cout << s2;
}
void main17()
{
string s1 = "AAbbb";
//1 函数的入口地址 2 函数对象 3预定义的函数对象
transform(s1.begin(),s1.end(),s1.begin(),toupper);// 全变成大写
cout << s1 << endl;
string s2 = "AAAAccvv";
transform(s2.begin(),s2.end(),s2.begin(),tolower);// 全变成小写
cout << s2 << endl;
}
void main()
{
//main11();
//main12();
//main13();
//main14();
//main15();
//main16();
main17();
cout << "hello..."<< endl;
system("pause");
}