C++(STL)学习(二)—string容器

基本概念

string类,定义在头文件<string>中

String和C风格字符串相比:
  • Char *是一个指针,String是一个类
    String封装了char* ,管理这个字符串,是个 char*类型的容器
  • String封装了很多使用的成员方法
    查找、拷贝、删除、替换、插入
  • 不用考虑内存释放和越界
    String管理Char*所分配的内存,每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等

string容器常用操作

string构造函数
string();	//  创建一个空的字符串
string(const string & str);  //  使用一个string对象来初始化另一个string对象
string(const char *s);  //	使用字符串s初始化
string(int n,char c);  //  使用n个字符初始化
string基本赋值操作
string &operator=(const char *s);  //  把char *类型字符串赋值给当前的字符串
string &operator=(const string &s);  //		把字符串s赋值给当前的字符串
string &operator=(char c);	// 字符赋值给当前的字符串
string &assign(const char *s);//	把字符串s赋值给当前的字符串
string &assign(const char *s,int n);
string &assign(const string &s);//	把字符串s赋值给当前的字符串
string &assign(int n,char c);
string &assign(const string &s,int start,int n);
#include <iostream>
#include <string>
#include <stdexcept>
#include <vector>
#include <algorithm>
using namespace std;


//  String案例
void test01()
{
    string s1;
    string s2(s1);
    string s3("aaa");
    string s4(10,'c');

    cout<<s3<<endl;
    cout<<s4<<endl;

    string s5;
    s5 = s4;
    cout<<s5<<endl;
    s5.assign("abcdefg",3);

    cout<<s5<<endl;

    string s6;
    s6.assign("abcdefg",3,4);
    cout<<s6<<endl;



}

//  字符存取

void  test02()
{
    string s = "hello,world!";

    for(int i=0;i<s.size();i++)
    {
        //  cout<<s[i]<<endl;
        cout<<s.at(i)<<endl;    //  at和[]区别  []访问越界会直接挂掉  而at访问越界  会抛出一个异常
    }

    try
    {
        //  s[100];
        s.at(100);
    }
    catch(out_of_range &e)
    {
        cout<<e.what()<<endl;
    }


}

void test03()
{
    //  字符串拼接
    string s1 = "我";
    string s2 = "爱学习";
    s1 +=s2;
    cout<<s1<<endl;

    string s3 = "我";
    s3 +="爱学习";
    cout<<s3<<endl;

    s3.append("爱学习");
    cout<<s3<<endl;

    //  字符串查找  第二个参数默认查找起始坐标为0
     string s4 = "abcdefghide";

     cout<<s4.find("def")<<endl;
     cout<<s4.find("den")<<endl;  //  如果找不到返回-1  找到的话返回第一次出现的位置

     cout<<s4.rfind("de")<<endl;    //  rfind从右往左查找

     //  字符串替换
     s4.replace(1,3,"111111111");
     cout<<s4<<endl;

}


void test04()
{
    string s1 = "abcde";
    string s2 = "abcd";

    if(!s1.compare(s2))
    {
        cout<<"s1=s2"<<endl;
    }
    else if(s1.compare(s2)>0)
    {
        cout<<"s1>s2"<<endl;
    }
    else
    {
        cout<<"s1<s2"<<endl;
    }
}

void test05()
{
    string s1 = "abcde";
    cout<<s1.substr(1,3)<<endl;

    string s2 = "dangyanlin@sina.com";
    cout<<s2.substr(0,s2.find("@"))<<endl;
}

void test06()
{
    string s1 = "www.baidu.com.cn";
    vector<string>v;
    int i = 0;
    while(true)
    {
        if(s1.find(".",i)==-1)
        {
            v.push_back(s1.substr(i,s1.size()));
            break;
        }
        v.push_back(s1.substr(i, s1.find(".",i)-i));
        i = s1.find(".",i)+1;
    }
    for(vector<string>::iterator it = v.begin();it!=v.end();it++)
    {
        cout<<*it<<endl;
    }
}


//  string插入和删除操作

/*
    string& insert(int pos,const char *s);  //  插入字符串
    string& insert(int pos,const string &s);  //  插入字符串
    string& insert(int pos,int n,char c);  //  在指定位置插入n个字符
    string& erase(int pos,int n = npos);  //  删除从Pos开始的n个字符
*/
void test07()
{
    string s1 = "abcdefg";
    cout<<s1.insert(2,"112233")<<endl;

    cout<<s1.insert(2,5,'n')<<endl;

    cout<<s1.erase(2,11)<<endl;
}

//  string和C风格字符串转换
/*
 * string 转 char*
 * string str = "abcdef";
 * const char * cstr = str.c_str();
 * char* 转 string
 * char *s = "abcdef";
 * string str(s)
 * */

void test09()
{
    string s = "abcdefg";
    char &a = s[2];
    char &b = s[3];

    a = '1';
    b = '2';

    cout<<s<<endl;
    cout<<(int *)s.c_str()<<endl;

    s = "ppppppppppppppppp";
    a = '1';
    b = '2';
    cout<<s<<endl;
    cout<<(int *)s.c_str()<<endl;
}

void test10()
{
    string s = "abdEsad";
    for(int i=0;i<s.size();i++)
    {
        s[i] = toupper(s[i]);

        s[i] = tolower(s[i]);
    }

    cout<<s<<endl;
}

int main()
{
    //  test01();
    //  test02();
    //  test03();
    //  test04();
    //  test05();
    //  test06();
    //  test07();
    //  test09();
    test10();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

walkerrev_ll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值