C++ STL(容器:string类)

string容器:https://www.runoob.com/cprogramming/c-standard-library-string-h.html 

目录

0String 容器

0.1string容器基本概念

1string类的常用API

1.1string类的构造函数:

1.2string的赋值:

1.3string类的字符操作:

1.4string的连接:

1.5string类的查找函数:

1.6string类的替换函数: 

1.7string的比较:

1.8string的子串:

1.9string的特性描述: 

2.0string类的迭代器处理: 


0String 容器

0.1string容器基本概念

C风格字符串(以空字符串结尾的字符数组)太过于复杂难于掌握,不适合大程序的开发,所以C++标准库定义了一种string类,定义在头文件<string>

String和C风格字符串对比:

Char *是一个指针,Sting是一个类

string封装了char *,管理这个字符串,是一个char *型的容器。

String封装了很多实用的成员方法

查找find,拷贝copy,删除delete,替换replace,插入insert

不用考虑内存释放和越界

string管理char *所分配的内存,每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。

1string类的常用API

此处只列举部分API

较好文章分享

标准C++中的string类的用法总结https://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html(比较全)

C++string类总结: https://www.cnblogs.com/X-Do-Better/p/8628492.html

需要包含头文件和名称空间:

#include<string>
using namespace std;

1.1string类的构造函数:

string();//创建空指针
string(const string& str);//拷贝构造函数
string(const char* s);//使用其他字符串进行初始化
string(int n, char c);//使用n个c对字符串进行初始化

操作和输出:

    //1
    string str;//默认构造
    //2
    string str2(str);//拷贝构造
    //3
    string str3 = str;
    string str4 = "abcd";
    //4
    string str5(10,'a');
    cout<<"str4:"<<str4<<endl<<"str5:"<<str5;

   

1.2string的赋值:

string &operator=(const string &s);//把字符串s赋给当前字符串
string &operator=(const char *s);//char*类型字符串,赋值给当前的字符串

string &assign(const char *s);//用c类型字符串s赋值
string &assign(const char *s,int n);//用c字符串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);//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串

操作和输出:

    //1
    str = "hellow";
    str3 = str4;
    cout<<endl<<"str:"<<str<<endl<<"str3:"<<str3;
    //2
    str3.assign("abcdef",4);
    cout<<endl<<"str3:"<<str3;
    //3
    str2.assign("master",1,3);
    cout<<endl<<"str3:"<<str3<<endl;


1.3string类的字符操作:

const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n);
operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。

 操作和输出:

    string s = "hello world";
    for(int i = 0;i<s.size();++i)
    {
        cout<<s[i]<<s.at(i);
        //【】访问越界直接死
        //at越界会抛出异常
    }
    try
    {
        cout<<s.at(100)<<endl;
    }
    catch(out_of_range & e)
    {
        cout<<endl<<e.what()<<endl;
    }

1.4string的连接:


string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾 
string &append(const char *s);            //把c类型字符串s连接到当前字符串结尾
string &append(const char *s,int n);//把c类型字符串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
string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾

  操作和输出:

    //1
    string s1 = "h";
    string s2 = "ello";
    s1 += s2;
    cout<<endl<<"s1:"<<s1<<endl;//hello

    //2
    s1.append(" world");
    cout<<"s1:"<<s1<<endl;

1.5string类的查找函数:

 
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值 
int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值 
int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos 
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos 
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const; 
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找

具体操作:

    //查找
    string s3 = "abcdefg";
    int pos = s3.find("bc");
    //找不到返回是-1
    cout<<"pos:"<<pos<<endl;

    int pos2 = s3.rfind("bc");
    cout<<"pos2:"<<pos2<<endl;

1.6string类的替换函数: 


string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串

具体操作和输出: 

    //替换
    string s5 = "hello";
    s5.replace(1,3,"1111");
    cout<<"s5:"<<s5<<endl;

 

1.7string的比较:

bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
int compare(const string &s) const;//比较当前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小

具体操作:

    //比较
    string s6 = "bbc";
    string s7 = "cnn";
    if(s6.compare(s7) == 0)
    {
        cout<<"s6==s2"<<endl;
    }
    else if(s6.compare(s7) == 1)
    {
        cout<<"s6>s2"<<endl;
    }
    else
    {
        cout<<"s6<s2"<<endl;
    }

1.8string的子串:


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

具体操作:

    //string
    string s8 = "police";
    string s9 = s8.substr(1,3);
    cout<<s9<<endl;

    string email = "[email protected]";
    cout<<email.find("@")<<endl;
    string username = email.substr(0,email.find("@"));
    cout<<"username:"<<username<<endl;

1.9string的特性描述: 

int capacity()const;    //返回当前容量(即string中不必增加内存即可存放的元素个数)
int max_size()const;    //返回string对象中可存放的最大字符串的长度
int size()const;        //返回当前字符串的大小
int length()const;       //返回当前字符串的长度
bool empty()const;        //当前字符串是否为空
void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分

以上只是对部分API进行了介绍

2.0string类的迭代器处理: 


string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。
用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:

const_iterator begin()const;
iterator begin();//返回一个迭代器,它指向容器c的第一个元素

const_iterator end()const;
iterator end();//返回一个迭代器,它指向容器c的最后一个元素的下一个位置

const_iterator rbegin()const;
iterator rbegin();//返回一个逆序迭代器,它指向容器c的最后一个元素

const_iterator rend()const;
iterator rend();//返回一个逆序迭代器,它指向容器c的第一个元素前面的位置

rbegin和rend用于从后向前的迭代访问,通过设置迭代器

以上接口返回的都是迭代器类型 

具体操作:利用迭代器进行遍历

     string str = "I see a monsters";
     string::iterator S;
     for(S = str.begin();S!=str.end();++S)
     {
         try
         {
             cout<<*S<<endl;
         }
         catch(out_of_range & e)
         {
             cout<<endl<<e.what()<<endl;
         }
     }

     S = str.begin();
     cout<<"str.begin():"<<*S<<endl;

     S = str.end();
     cout<<"str.end():"<<*S<<endl;

     string::reverse_iterator S_R;


     S_R = str.rbegin();
     cout<<"str.rbegin():"<<*S_R<<endl;

     S_R = str.rend();
     cout<<"str.rend():"<<*S_R<<endl;

发布了85 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41605114/article/details/104938665
今日推荐