STL常用容器之 string
文章目录
1. string容器
1. string的基本概念
本质:string是C++风格的字符串,而string本质上是一个类
string 和char 区别*:
- char* 是一个指针
- string是一个类,类内部封装了
char *
,管理这个字符串,是char *
型的容器
特点:string类内封装了很多成员的方法
例如: 查找find
,拷贝copy
,删除delete
,替换replace
,插入insert
steing管理char *
所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责
2. string构造函数
构造函数原型:
string();//创建一个空的字符串 例:string str;
string(const char* s); //使用字符串初始化
string(const string & str);//使用一个string对象初始化另一个string对象
string(int n, char c);//使用n个字符c初始化
例:
#include <iostream>
#include <string>
using namespace std;
void test()
{
// string();
string s1;//默认构造
const char* str = "hello world";
// string(const char* s);
string s2(str);
cout << s2 << endl;
//string(const string & str);
string s3(s2);
cout << s3 << endl;
//* string(int n, char c);
string s4(10,'a');
cout << s4 << endl;
}
int main()
{
test();
system("pause");
return 0;
}
3. 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);//把字符串s的前n个字符赋给当前字符串
-
string& assign(const string &s);//把字符串s赋给当前字符串
-
string& assign(int n, char c);//把n个字符c赋给当前字符串
例:
#include <iostream>
#include <string>
using namespace std;
void test()
{
//string& operator=(const char* s);
string s1;
s1 = "hello world";
cout << s1 << endl;
//string& operator=(const string &s);
string s2;
s2 = s1;
cout << s2 << endl;
//string& operator=(char c);
string s3;
s3 = 'aaa';
cout << s3 << endl;
//string& assign(const char* s);
string s4;
s4.assign("hello C++");
cout << s4 << endl;
//string& assign(const char *s,int n);
string s5;
s5.assign("hello C++",5);
cout << s5 << endl;
//string& assign(const string &s);
string s6;
s6.assign(s4);
cout << s6 << endl;
//string& assign(int n, char c);
string s7;
s7.assign(3,'a');
cout << s7 << endl;
}
int main()
{
test();
system("pause");
return 0;
}
4. string字符串拼接
函数原型:
string& operator+=(const char* str);//重载+=
string& operator+=(const char c);//重载+=
string& operator+=(const string& str);//重载+=
string& append(const char *s);//把字符串s连接到当前字符串结尾
string& append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾
string& append(const string& s);//同operator+=(const string& str)
string& append(const string &s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾
例:
#include <iostream>
#include <string>
using namespace std;
/*
1. `string& operator+=(const char* str);//重载+=`
2. `string& operator+=(const char c);//重载+=`
3. `string& operator+=(const string& str);//重载+=`
4. `string& append(const char *s);//把字符串s连接到当前字符串结尾`
5. `string& append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾`
6. `string& append(const string& s);//同operator+=(const string& str)`
7. `string& append(const string &s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾`
*/
void test()
{
//string& operator+=(const char* str);
string s1 = "我";
s1 += "爱玩游戏";
cout << s1 << endl;
//string& operator+=(const char c);
s1 += ':';
cout << s1 << endl;
//string& operator+=(const string& str);
string s2 = "LOL游戏";
s1 += s2;
cout << s1 << endl;
// string& append(const char *s);
string s3 = "I";
s3.append(" Love ");
cout << s3 << endl;
//string& append(const char *s, int n);
s3.append("you sa",3);
cout << s3 << endl;
// string& append(const string& s);
string s4;
s4.append(s3);
cout << s4 << endl;
//string& append(const string &s, int pos, int n);
s4.append(s4,1,5);
cout << s4 << endl;
}
int main()
{
test();
system("pause");
return 0;
}
5. string查找和替换
查找:查找指定字符串是否存在
int find(const string& str, int pos = 0) const;//查找str第一次出现位置,从pos开始查找
int find(const char* s, int pos = 0) const;//查找s第一次出现的位置,从pos开始查找
int find(const char* s, int pos, int n) const;//从pos位置查找s的前n个字符第一次位置
int find(const char c, int pos = 0) const;//查找字符c第一次出现位置
int rfind(const string& str, int pos = npos) const;//查找str最后依次位置,从pos开始查找
int rfind(const* s,int pos = npos) const;//查找s最后一次出现位置,从pos开始
int rfind(const char* s, int pos, int n) const;//从pos查找s的前n个字符最后一次位置
int rfind(const char c, int pos = 0) 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
6. string字符串比较
函数原型:
int compare(const string &s) const;//与字符串s比较
int compare(const char* s) const;//与字符串s比较
比较方式:按字符的ASCII进行比较
- 相等 返回 0
- 大于 返回 1
- 小于 返回 -1
7. string字符存取
string中单个字符获取的方式:
char& operator[](int n);//通过[]方式获取字符
char& at(int n);//通过at方法获取字符
例:
#include <iostream>
#include <string>
using namespace std;
void test()
{
string s = "hello";
//1. 通过[]访问单个字符
for(int i = 0; i < s.size(); i++)
{
cout << s[i] << " ";
}
cout << endl;
//2. 通过at访问
for(int i = 0; i < s.size(); i++)
{
cout << s.at(i) << " ";
}
cout << endl;
//修改
s[0] = 'x';
s.at(1) = 'x';
//变为xxllo
cout << s << endl;
}
int main()
{
test();
system("pause");
return 0;
}
8. 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 test()
{
string s = "hello";
//插入
s.insert(1,"111");
cout << s << endl;
//删除
s.erase(1,3);
cout << s << endl;
}
9. string子串
函数原型:
string substr(int pos = 0, int n = npos) const;//返回由pos开始的n个字符组成的字符串
例:
void test()
{
string s = "abcde";
//取子串
string subStr = s.substr(1,3);
cout << subStr << endl;
}