C++:STL篇(三)容器之string容器

C++:STL篇(三)容器之string容器

3.1string 容器基本概念

  1. c风格的字符串(以空字符结尾的字符数组)太过于复杂,难以掌握,不适合大程序的开发,c++标准库定义了一种string类,定义在头文件中;
  2. string和c的风格对比:
    A、Char是一个指针,而string是一个类;string封装了char,管理字符串,是char型的一个容器;
    B、string封装了成员方法。如:find,copy,delete,replace,insert。
    C、string不用考虑内存的额释放和越界。string管理char
    分配的内存,每一次string的复制,取值都由string维护,不用担心复制越界和取值越界等等。
  3. 字符串基本操作如下:
#include<iostream>
#include<stdexcept>//异常的头文件
#include<string>
using namespace std;

/*
字符串的构造和赋值操作
*/
void test01()
{
    //基本构造
    string str;//默认
    string str2(str);//拷贝构造
    string str3=str;

    string str4="afubu";
    string str5(10,'a');
    //string(int n,char c)的意思是用n个字符c赋值给当前的字符串
    cout<<str4<<endl;
    cout<<str5<<endl;

    //基本赋值:注意assign是string的一个属性,所以调用过程是.
    str="hbhcbjc";
    str2=str4;

    //string& assign(char*s ,int n)表示将char*的前n个字符赋值给字符串
    str3.assign("abcdefg",4);
    cout<<str3<<endl;

    //string& assign(const string &s, int start, int n);表示将s从start开始n个字符复制给字符串
    str4.assign("hello everybody",2,4);
    cout<<str4<<endl;
}


/*
string 存取字符操作:
char& operator[](int n);//通过[]方式取字符;
char& at(int n);通过at的方式获取字符;
[]和at的区别?
[]访问越界会直接挂掉,而at会抛出异常
*/
void test02()
{
    //普通方式
    string s="hello  world!";
    for(int i=0; i<s.size(); i++)
    {
        cout<<s[i];
    }
    cout<<endl<<"-----------------------------------"<<endl;
    //at方式
    for(int i=0; i<s.size(); i++)
    {
        cout<<s.at(i);
    }
    cout<<endl<<"-----------------------------------"<<endl;
    try
    {
        //cout<<s[100]<<endl;//普通方式会直接挂掉,不会抛出异常;
        cout<<s.at(100)<<endl;//at方式会抛出异常;
    }
    catch(out_of_range & e)
    {
        cout<<e.what()<<endl;
    }
}


/*
string 拼接操作
string& operator+=(const string& str);//重载+=操作符
string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
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个字符;
*/
void test03()
{
    string str1="hello ";
    string str2="world";
    str1+=str2;
    cout<<str1<<endl;
    str1.append("!I am here");
    cout<<str1<<endl;
    str1.append("!I am here",4);
    cout<<str1<<endl;
    str1.append("!I am here",2,4);
    cout<<str1<<endl;
    str1.append(6,'b');
    cout<<str1<<endl;
}

/*
string 查找操作:
//从pos开始查找字符c在当前字符串的位置,找不到的话返回-1
int find(char c, int pos = 0) const;

//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos = 0) const;

//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const char *s, int pos, int n) const;

//从pos开始查找字符串s在当前串中的位置
int find(const string &s, int pos = 0) const;

//以上查找成功时返回所在位置,失败返回string::npos的值
//从pos开始查找字符c第一次出现的位置
int find_first_of(char c, int pos = 0) const;
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

//从当前串中查找第一个不在串s中的字符出现的位置,失败返回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;

//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
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;
*/
void test04()
{
    string str="whathappened?";
    int pos=str.find("happa");
    int pos1=str.rfind("en");//rfind和find的查找方式不同,但是返回值相同
    cout<<pos<<endl;
    cout<<pos1<<endl;
}

/*
string 替换操作
*/
void test05()
{
    string str="hrllo";
    str.replace(1,3,"jklio");//替换从pos开始n个字符为字符串str;
    cout<<str<<endl;
}
/*
string 比较操作compare函数在>时返回1,<时返回-1,==时返回0;
比较区分大小写,比较时参考字典顺序,排在越前面越小;
大写的A比小写的a小;
int compare(const string &s) const;//与字符串s相比较
int compare(const char *s)const;
*/
void test06()
{
    string s1="abcd";
    string s2="abcd";
    if(s1.compare(s2)==0)
    {
        cout<<"s1=s2"<<endl;
    }
    else if(s1.compare(s2)==1)
    {
        cout<<"s1>s2"<<endl;
    }
    else
    {
        cout<<"s1<s2"<<endl;
    }
}

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

void test07()
{
    string s1="youwillbehappy";
    string s2=s1.substr(1,4);
    cout<<s2<<endl;
    //需求 寻找一个右键的用户名
    string s3="lisi@sina.com";
    int pos=s3.find("@");
    cout<<pos<<endl;
    string userName=s3.substr(0,pos);
    cout<<"用户名字是:"<<userName<<endl;
}

/*
string 插入和删除
string& insert(int pos, const char* s);//插入字符串
string& insert(int pos, const string& str);//插入字符串
string& insert(int pos, int n ,char c);//在指定位置插入n个字符c
string& erase(int pos, int n =npos);//删除从pos开始的n个字符
*/

void test08()
{
    string s1="hello";
    cout<<s1<<endl;
    s1.insert(1,"sdsds");
    cout<<s1<<endl;

    //删除操作
    s1.erase(1,5);
    cout<<s1<<endl;
}


/*
string和c-style字符串转换
实用性比较高
*/
void func(string s)
{
    cout<<s<<endl;
}
void func2(char * s)
{
    cout<<s<<endl;
}
void test09()
{
    string s="abcd";
    //string->char *;
    func(s);//const char * 隐式类型转换为sstring
    const char * p=s.c_str();

    //const char *->string;
    string s2(p);
    //func2(s2);运行时会报错,所以string不能隐式类型转换成为char *

}

/*
如果字符串经过更改,重新分配内存,那么之前的引用就不能正确的指向字符串中的字符,
如果再次更改指向字符串中字符的引用,则会报错。
*/
int main()
{
    test01();
    test02();
    test03();
    test04();
    test05();
    test06();
    test07();
    test08();
    test09();
    return 0;
}


  1. 练习:写一个函数,函数内部将string字符串中的所有小写字符转换成大写字符
#include<iostream>
#include<string>
using namespace std;
void change()
{
    string s="bcSvhWHUhjncmbhmb";

    for(int i=0;i<s.size();i++)
    {
        //变成大写
        s[i]=toupper(s[i]);
        //变成小写
        //s[i]=tolower(s[i]);
    }
    cout<<s;
}
int main()
{
    change();
    return 0;
}

!!!
重点总结:
1.对字符串存取,[]和at的区别:
[] 访问越界会直接挂掉,at访问越界会抛出异常 out of range
2.substr 配合find 查找邮件名
3.char * 和string转换:
char * 可以隐式转换成string 反之不可以
4. 字符大小写转换:toupper和tolower函数
5. find 如果找不到的话返回-1,找到则返回第一次出现的位置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值