• STL常用容器——String容器的使用


    STL常用容器——String容器

    string类封装了很多成员方法,例如查找find,拷贝copy,删除delete,替换replace,插入insert。

    string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部负责

    1、string构造器

    string();创建一个空字符串

    string (const char *s);用字符串s 进行初始化

    string (const string& str);使用一个string对象,初始化另一个string对象

    string(int n,char c);使用n个字符初始化

    #include
    using namespace std;
    #include
    
    void test()
    {
    	string s;
    	cout << s << endl;
    
    	string s1("明天,你好");
    	cout << s1 << endl;
    
    	string s2(s1);
    	cout << s2 << endl;
    
    	string s3(10, 'w');
    	cout << s3 << endl;
    
    }
    int main() {
    
    	test();
    	system("pause");
    	return 0;
    }
    

    2、string的赋值操作

    sting& operator=(const chars); char字符串赋值给当前字符串

    sting& operator=(const string &s);把字符串s赋值给当前字符串

    sting& operator=(const char c); 字符c赋值给当前字符串

    sting& assign(const char*s); 把字符串s赋值给当前字符串

    sting& assign(const char*s,int n); 把字符串s的前n个字符赋值给当前字符串

    sting& assign(const string &s); 把字符串s赋值给当前字符串

    sting& assign(int n,char c); 把n个字符c赋值给当前字符串

    #include
    using namespace std;
    #include
    
    void test() {
    	const char* s = "你好呀";
    	string s1 = s;
    	cout << s1 << endl;
    
    	string s2 = s1;
    	cout << s2 << endl;
    
    	string s3;
    	s3 = 'a';
    	cout << s3 << endl;
    
    	string s4;
    	s4.assign("明天见");
    	cout << s4 << endl;
    
        //截取,[0,3)
    	string s5;
    	s5.assign("abcdefj", 3);
    	cout << s5 << endl;
    
    	string s6;
    	s6.assign(s1);
    	cout << s6 << endl;
    
    	string s7;
    	s7.assign(6, 'a');
    	cout << s7 << endl;
    }
    int main() {
    	test();
    	system("pause");
    	return 0;
    }
    

    3、拼接字符串

    运算符+=重载:

    • sting& operator+=(const char* str);

    • sting& operator+=(const string &s);

    • sting& operator+=(const char c);

    append方法:

    • sting& appendconst char*s); 把字符串s连接到当前字符串结尾

    • sting& append(const char*s,int n); 把字符串s的前n个字符连接到当前字符串结尾

    • sting& append(const string &s); 相当于operator+=(const string &s);

    • sting& append(const string &s,int pos,int n);字符串s中从pos开始的n个字符连接到当前字符串结尾

    #include
    using namespace std;
    #include
    
    void test01() {
    	string s = "确实";
    	string s1 = "我爱游戏";
    	cout << s1 << endl;
    	s1 += "?你在打游戏吗";
    	cout << s1 << endl;
    	s1 += '!';
    	cout << s1 << endl;
    	//中文占2个字符大小
    	string s2 = "今天天气真不错";
    	cout << s2 << endl;
    	s2.append(",hhh");
    	cout << s2 << endl;
    	s2.append(s);
    	cout << s2 << endl;
    	s2.append("快别打了,这都啥呀?", 4);
    	cout << s2 << endl;
    	s2.append(s1, 4, 5);
    	cout << s2 << endl;
    }
    int main() {
    	test01();
    	system("pause");
    	return 0;
    }
    

    4、字符串查找和替换

    查找

    int find(const string& str,int pos=0) const; 返回str第一次出现的位置下标,从pos开始查找,如果没有查找到则返回-1

    int find(const char* s,int pos=0) const; 查找s第一次出现的位置下标,从pos开始查找

    int find(const char* s,int pos=0,int n) const; 从pos开始查找s的前n个字符第一次出现的位置下标

    int find(const char c) const; 查找字符c第一次出现的位置下标

    int rfind(const string& str,int pos=npos) const; 查找str最后一次出现的位置下标,从pos开始查找

    int rfind(const char* s,int pos=npos) const; 查找s最后一次出现的位置下标,从pos开始查找

    int rfind(const char* s,int pos=npos,int n) const; 从pos开始查找s的前n个字符最后一次出现的位置下标

    int rfind(const char c) const; 查找字符c最后一次出现的位置下标

    替换

    string &replace(int pos,int n,const string &str);替换从pos开始的n个字符为字符串str

    string &replace(int pos,int n,const char* s);替换从pos开始的n个字符为字符串s

    #include
    using namespace std;
    #include
    //字符串的查找
    void test01() {
    	string s1 = "abcdefghijkeflmn";
    	int pos = s1.find("ef");
    	cout << pos << endl; //4 返回的是查找目标字符串的首个字符对应第一次的位置下标
    	int res = s1.find("wn");
    	cout << res << endl;//-1  没有查找到则返回-1
    
    	int pos1 = s1.rfind("ef");
    	cout << pos1 << endl;//11 rfind查找最后一次出现的位置
    
    }
    //字符串的替换
    void test02() {
    	string s1 = "abcdefghijkeflmn";
    	s1.replace(2, 4, "开始了");//ab开始了ghijkeflmn  替换掉[2,4]范围的字符串
    	cout << s1 << endl;
    }
    int main() {
    	test01();
    	test02();
    	system("pause");
    	return 0;
    }
    

    5、字符串比较

    字符串比较最大用途是比较是否相等,对于比较大小没有实在意义

    字符串比较规则:

    • 按字符的ASCII码进行对比
    • “=” 返回 0
    • “>” 返回 1
    • “<” 返回 -1
    #include
    using namespace std;
    #include
    
    void test01() {
    	string s1 = "cccc";
    	string s2 = "cccc";
    	string s3 = "dddd";
    	string s4 = "aaaa";
    
    	cout << s1.compare(s2) << endl;//0
    	cout << s1.compare(s3) << endl;//-1
    	cout << s1.compare(s4) << endl;//1
    
    }
    int main() {
    	test01();
    	system("pause");
    	return 0;
    }
    

    6、字符串存取

    char& operator[](int n) 通过[]取字符串

    char& at(int n) 通过at方式获取字符串

    #include
    using namespace std;
    #include
    //字符串存取
    void test01() {
    	//读取中文时输出的是双倍的“?”
    	string s = "hello,世界";
    	//通过[]获取
    	for (int i = 0; i < s.size(); i++) {
    		cout << s[i] << " ";
    	}
    	cout << endl; // h e l l o , ????
    	//通过at获取
    	for (int i = 0; i < s.size(); i++) {
    		cout << s.at(i) << " ";
    	}
    	cout << endl; // h e l l o , ????
    
    	//修改单个字符
    	s[0] = 'c';
    	cout << s << endl; //cello,世界
    	s.at(1) = 'c';
    	cout << s << endl;//ccllo,世界
    
    }
    int main() {
    	test01();
    	system("pause");
    	return 0;
    }
    

    7、字符串插入与删除

    string& insert(int pos,const char *s); 插入字符串

    string& insert(int pos,const string& str); 插入字符串

    string& insert(int pos,int n,char c);在指定位置pos插入n个字符c

    string& erase(int pos,int n =npos); 删除从npos开始的n个字符,范围为[0,5)

    #include
    using namespace std;
    #include
    //string的插入与删除
    //1. string & insert(int pos, const char* s);   //插入字符串
    //2. string & insert(int pos, const string & str); //插入字符串
    //3. string & insert(int pos, int n, char c);//在指定位置pos插入n个字符c
    //4. string & erase(int pos, int n = npos); //删除从pos开始的n个字符
    void test01() {
    	//string的插入
    	string s = "hello,世界";
    	const char* s1 = "521";
    	s.insert(4, s1);
    	cout << s << endl;//hell521o,世界 插入到第四个位置
    	s.insert(1, "新增字符串");
    	cout << s << endl;//h新增字符串ell512o,你好  插入到第一个位置
    	s.insert(0, 6, 'd');
    	cout << s << endl;//ddddddh新增字符串ell512o,你好   在s的指定位置插入n个字符
    	//string 的删除
    	s.erase(0, 5);
    	cout << s << endl;//dh我是新增的字符串ell512o,你好  删除[0,5)范围的字符
    }
    int main() {
    	test01();
    	system("pause");
    	return 0;
    }
    

    8、截取字符串

    str.substr(int pos=0,int n=npos) const; 截取str中由pos开始的n个字符组成的字符串

    #include
    using namespace std;
    #include
    //string获取子串
    
    void test01() {
    	string s = "hello,世界";
    	string subS = s.substr(2, 7);
    	cout << subS << endl;//llo,世  若字符串的截取个数恰好读到中文且只剩一个字符,那么中文不显示
    }
    void test02() {
    	string s1 = "wujingou@qq.com";
    	string sub = s1.substr(0, s1.find('@'));
    	cout << "截取到的用户名为:" << sub << endl;//截取到的用户名为:wujingou
    }
    int main() {
    	test01();
    
    	test02();
    	system("pause");
    	return 0;
    }
    
  • 相关阅读:
    【C++进阶:多态】多态的构成条件 | 虚函数的重写 | 抽象类 | 多态的原理 | 多继承的虚函数表
    【Python】Pyinstaller打包后程序运行报错configparser.NoSectionError: No section:XX问题解决
    Labs‘Codes review(AVR)(1)
    【MMDetection】bug记录
    比上不足比下有余,GeForce RTX 4060出色的性价比,让你“欲罢不能”,中端显卡非它莫属
    CentOS7日志文件及journalctl日志查看
    读取.nrrd和.dcm文件格式医学图片可视化与预处理
    码蹄集 - MT3252 - 子序列问题
    大一作业HTML网页作业 HTML校园篮球网页作业(12个页面)
    《通用源码阅读指导书-MyBatis源码详解》笔记一(MyBatis概述、使用、源码大体执行流程、源码大体结构)
  • 原文地址:https://blog.csdn.net/wpc2018/article/details/127119511