首页 > 图灵资讯 > 技术篇>正文

STL标准库String类型

2023-06-02 09:24:12

StringStlString类型

1.String概念

  string是stl字符串的类型,通常用来表示字符串。在使用string之前,字符串通常用char*表示。 string和char*的区别

  1. string是一个类, char*是指向字符的指针。 string包装char*,管理字符串,是char*容器。也就是说,string是一个容器,其中元素的数据类型是char*。
  2. String不需要考虑内存释放和越界。 string管理char*分配的内存。每次复制string,string类别都负责维护值,不用担心复制越界和值越界。
  3. string提供了一系列字符串操作函数 查找find,复制copy,删除erase,替换replace,插入inserte
2.初始化–构造函数
  • 默认构造函数 : string(); ///构建空字符串string s1。
  • 复制结构函数: string(const string &str); //构建与str相同的string。如string s1(s2)。
  • 带参数的结构函数 string(const char *s); ////使用字符串s初始化 string(int n,char c); ///用n个字符C初始化

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <iterator>#include <vector>#include <algorithm>#include <string>using namespace std;void play(){    string s1("aaa");    string s2 = "bbb";    string s3 = s2;    cout << "s1:" << s1 << endl;    cout << "s2:" << s2 << endl;    cout << "s3:" << s3 << endl;}int main(){     play();    system("pause");    return 1;}

3.存取字符

string字符操作:

const char &operator[] (int n) const;const char &at(int n) const;char &operator[] (int n);char &at(int n);

operator[]和at()都返回到当前字符串中的第n个字符,但两者是不同的。主要区别在于at()越界时会抛出异常,[]越界时会返回(char)0.当编译器继续越界时,直接出错。如果您的程序希望通过try和catch捕获异常,建议使用at()。

string的长度

int length() const;   //返回当前字符串的长度。长度不包括字符串结尾的“\0”。bool empty() const;     ///当前字符串是否空?

string遍历 string的遍历可分为数组模式和迭代器模式。

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <iterator>#include <vector>#include <algorithm>#include <string>using namespace std;void play(){    string s1("abcdefg");    cout << "s1:" << s1 << endl;    cout << “使用下标引用数组的方法” << endl;    for (unsigned int i = 0; i < s1.length(); i++)    {        cout << s1[i] << "\t";    }    cout << endl;    cout << “使用at成员函数的数组方法:” << endl;    for (unsigned int i = 0; i < s1.length(); i++)    {        cout << s1.at(i) << "\t";    }    cout << endl;    cout << "使用迭代器:" << endl;    for (string::iterator index = s1.begin(); index != s1.end();index++)    {        cout << (*index) << "\t";    }    cout << endl;    try{        cout << 使用at成员函数的数组方法,测试异常处理:" << endl;        for (unsigned int i = 0; i < s1.length() + 3; i++)        {            cout << s1.at(i) << "\t";///如果不使用at函数,就会出现编译错误        }        cout << endl;    }    catch (...)    {        cout << “访问越界” << endl;    }}int main(){     play();    system("pause");    return 1;}

string赋值操作操作

string &operator=(const string &s);////将字符串s赋予当前字符串string &assign(const char *s); ////将字符串s赋予当前字符串string &assign(const char *s, int n); ////将字符串s的前n个字符赋予当前字符串string &assign(const string &s);  ///将字符串s赋给当前字符串string &assign(int n,char c);  ///用n个字符c给当前字符串string &assign(const string &s,int start, int n);  ////将从start开始的字符串s中的n个字符赋予当前字符串

4.和char*类型的转换

从string到char*的成员函数主要是:const char *c_str() const; 以“\0”结尾返回字符串的第一个地址

将string复制到char*指向的内存空间的成员函数是:int copy(char *s, int n, int pos=0) const; 将以pos开始的当前串中的n个字符复制到以s为起始位置的字符数组中,并返回实际复制的数量。

注意确保s指向的空间足够大,以容纳当前的字符串,否则就会越界。

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <iterator>#include <vector>#include <algorithm>#include <string>using namespace std;void play(){    string s1("abcdefg");    /*将string转换为char*//    printf("s1:%s\n", s1.c_str());    /*将string复制到char*指向的内存空间*/    char buf[128] = {0};    s1.copy(buf, 5, 0);    cout << "buf:" << buf << endl;}int main(){     play();    system("pause");    return 1;}

5.比较操作

int compare(const string &s) const;  //与字符串s相比intt compare(const char *s) const;   //与字符串s相比

compare函数>时返回 1,<时返回 -1、==时返回 0.比较大小写,比较时参考字典顺序,排在前面的越小。大写的A比小写的A小。

6.字符串连接

string &operator+=(const string &s);  ///将字符串s连接到当前字符串结尾的string &operator+=(const char *s);///将字符串s连接到当前字符串结尾的string &append(const char *s);    ///将字符串s连接到当前字符串结尾的string &append(const char *s,int n);  ///将字符串s的前n个字符连接到当前字符串结尾string &append(const string &s);   //同operator+=()string &append(const string &s,int pos, int n);////将字符串s中从pos开始的n个字符连接到当前字符串结尾的string &append(int n, char c);   ///在当前字符串结尾添加n个字符c

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <iterator>#include <vector>#include <algorithm>#include <string>using namespace std;void play(){    string s1 = "aaa";    string s2 = "bbb";    s1 = s1 + s2;    cout << "s1:" << s1 << endl;    string s3 = "333";    string s4 = "444";    s3.append(s4);    cout << "s3:" << s3 << endl;}int main(){     play();    system("pause");    return 1;}

7.搜索和替换

子串

string substr(int pos=0, int n=npos) const;    ///返回由pos开始的n个字符组成的子字符串

查找

int find(char c,int pos=0) const;  ///从pos开始找到字符c在当前字符串的位置 int find(const char *s, int pos=0) const;  ////从pos开始在当前字符串的位置int找到字符串s find(const string &s, int pos=0) const;  ///从pos开始,如果找不到当前字符串中字符串s的位置find函数,返回-1int rfind(char c, int pos=npos) const;   ///从pos开始,从后到前找到字符c在当前字符串中的位置 int rfind(const char *s, int pos=npos) const;int rfind(const string &s, int pos=npos) const;//rfind意味着反向搜索,如果找不到, 返回-1

替换

string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后将串string插入pos &replace(int pos, int n, const string &s);  //删除从pos开始的n个字符,然后将串svoid插入pos swap(string &s2);    ///交换当前字符串和s2的值

示例代码:

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <iterator>#include <vector>#include <algorithm>#include <string>using namespace std;void play(){    string s1 = "lzj hello lzj 111  lzj 222  lzj 333 ";    /*搜索lzj在s1中首次出现的位置*/    int index = s1.find("lzj", 0);    cout << "index:" << index << endl;    /*统计lzj在s1中的总出现次数*/    int offindex = s1.find("lzj", 0);    int substr_count = 0;    while (offindex != string::npos)    {        substr_count++;        cout << "offindex:" << offindex << endl;        offindex += strlen("lzj");        offindex = s1.find("lzj", offindex);    }    cout << “总次数:” << substr_count << endl;    /*用AAA*代替开头的Aaa*/    string s3 = "aaa  bbb ccc";    s3.replace(0, 3, "AAA");    cout << "s3:" << s3 <<endl;    /*用LZJ*/*代替s1中所有的LZJ    offindex = s1.find("lzj", 0);    while (offindex != string::npos)    {        cout << "offindex:" << offindex << endl;        s1.replace(offindex, 3, "LZJ");        offindex += strlen("lzj");        offindex = s1.find("lzj", offindex);    }    cout << “S1替换后的结果: " << s1 << endl;}int main(){     play();    system("pause");    return 1;}

8.删除和插入

string &insert(int pos, const char *s);string &insert(int pos, const string &s);///前两个函数将字符串插入pos位置 &insert(int pos, int n, char c);  //在pos位置 插入n个字符cstringn &erase(int pos=0, int n=npos);  ///删除pos开始的n个字符,返回修改后的字符串

示例代码:

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <iterator>#include <vector>#include <algorithm>#include <string>using namespace std;void play(){    string s1 = “hello1 hello2 hello1";    string::iterator it = find(s1.begin(), s1.end(), 'l');    if (it != s1.end())    {        s1.erase(it);    }    cout << 删除l后的s1:" << s1 << endl;    s1.erase(s1.begin(), s1.end());    cout << "s1全部删除:" << s1 << endl;    cout << "s1长度 " << s1.length() << endl;    string s2 = "BBB";    s2.insert(0, "AAA"); // 头插法    s2.insert(s2.length(), "CCC");    cout << s2 << endl;}int main(){     play();    system("pause");    return 1;}

9.算法相关

使用transform函数转换string中的字符大小写(具体STL算法另行介绍):

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <iterator>#include <vector>#include <algorithm>#include <string>using namespace std;void play(){    string s1 = "AAAbbb";    transform(s1.begin(), s1.end(), s1.begin(), toupper);    cout << "s1" << s1 << endl;    string s2 = "AAAbbb";    transform(s2.begin(), s2.end(), s2.begin(), tolower);    cout << "s2:" << s2 << endl;}int main(){     play();    system("pause");    return 1;}

上一篇 基于SSH实现的学生成绩管理系统
下一篇 基于SSM实现酒店管理系统

文章素材均来源于网络,如有侵权,请联系管理员删除。