一、string容器
1.1 string基本概念
本质:
-
string本质上是一个类
特点:
string类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete,替换replace,插入insert
string管理char*所分配的内存,不用担心赋值越界和取值越界等,由类内部进行负责
1.2 string构造函数
构造函数原型:
string();
//创建一个空的字符串 例如:string str
string(const char* s);
//使用字符串s初始化
string(const string& str);
//使用一个string对象初始化另一个string对象
string(int n,char c);
//使用n个字符c初始化
#include<iostream>
using namespace std;
void test1()
{
string s1;
cout << "s1 = " << s1 << endl;
const char* str = "hello world";
string s2(str);
cout << "s2 = " << s2 << endl;
string s3(s2);
cout << "s3 = " << s3 << endl;
string s4(10, 'a');
cout << "s4 = " << s4 << endl;
}
int main()
{
test1();
return 0;
}
1.3 string 赋值操作
功能描述:
给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赋给当前字符串
string的赋值方式很多,operator=这种方式是比较实用的
#include<iostream>
using namespace std;
#include<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赋给当前字符串
void test1()
{
string s1;
s1 = "hello world";
cout << "s1=" << s1 << endl;//s1=hello world
string s2;
s2 = s1;
cout << "s2=" << s2 << endl;//s2=hello world
string s3;
s3 = 'a';
cout << "s3=" << s3 << endl;//s3=a
string s4;
s4.assign("hello C++");
cout << "s4=" << s4 << endl;//s4=hello C++
string s5;
s5.assign("hello C++", 5);
cout << "s5=" << s5 << endl;//s5=hello
string s6;
s6.assign(s5);
cout << "s6=" << s6 << endl;//s6=hello
string s7;
s7.assign(10, 'a');
cout << "s7=" << s7 << endl;//s7=aaaaaaa
}
int main()
{
test1();
return 0;
}
1.4 string字符串拼接
string& operator+=(const char* str);
//重载+=操作符
string& operator+=(const char c);
//重载+=操作符
string& operator=(char string& str);
//重载+=操作符
string& append(const char* s);
//把字符串s连接到当前字符串结尾
string& append(const char *s,int n);
//把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s);
//同string& operator=(char string& str)
string& append(const string &s,int pos,int n);
//字符串s中从pos开始的n个字符连接到字符串结尾
第二个参数为:第几个数开始。第三个参数为:一共截多少个
#include<iostream>
using namespace std;
#include<string>
void test1()
{
string s1="我";
s1 += "爱玩游戏";
cout << "s1=" << s1 << endl;
s1 += ':';
cout << "s1=" << s1 << endl;
string s2 = "王者荣耀、刺激战场";
s1 += s2;
cout << "s1=" << s1 << endl;
string s3 = " I ";
s3.append(" LOVE ");
cout << "s3=" << s3 << endl;
s3.append("game uiufhbavlfya", 4);
cout << "s3=" << s3 << endl;
s3.append(s2, 0, 8);
cout << "s3=" << s3 << endl;
}
int main()
{
test1();
return 0;
}
1.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 char* 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 =e)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<iostream>
using namespace std;
#include<string>
void test1()
{
//查找(find)
string s1 = "abcdefghi";
int pos=s1.find("fg");
cout << "pos=" << pos << endl;//5
int pos1 = s1.find("fh");
cout << "pos1=" << pos1 << endl;//-1
//查找(rfind)
int pos2 = s1.rfind("fg");
cout << "pos2=" << pos2 << endl;//5
string s2 = "abcdefde";
int pos3 = s2.rfind("de");
cout << "pos3=" << pos3 << endl;//6
//替换
string s3 = "abcdefg";
s3.replace(1, 3, "1111");
cout << "s3=" << s3 << endl;//a1111efg
}
int main()
{
test1();
return 0;
}
-
find查找是从左往右,rfind从右往左
-
find找到字符串后返回查找的第一个字符位置,找不到返回-1
-
replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串
1.6 string字符串比较
功能描述:
-
字符串之间的比较
比较方式:
字符串比较是按照字符的ASCLL码进行比较的
=返回 0
>返回 1
<返回 -1
函数原型:
int compare(const string &s) const;
//与字符串s比较
int compare(const char *s) const;
//与字符串s比较
#include<iostream>
using namespace std;
#include<string>
void test1()
{
string s1 = "zello";
string s2 = "hello";
if (s1.compare(s2) == 0)
{
cout << "s1=s2" << endl;
}
else if (s1.compare(s2) > 0)
{
cout << "s1>s2" << endl;
}
else
{
cout << "s1<s2" << endl;
}
}
int main()
{
test1();
return 0;
}
1.7 string字符存取
string中单个字符存取方式有两种
-
char& operator[](int n);
//通过[]方式取 -
char& at(int n);
//通过at方法获取字符
#include<iostream>
using namespace std;
#include<string>
void test1()
{
string s1 = "zello";
for (int i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
for (int i = 0; i < s1.size(); i++)
{
cout << s1.at(i) << " ";
}
cout << endl;
//修改单个字符
s1[0] = 'h';
cout << "s1=" << s1 << endl;//hello
s1.at(1) = 'h';
cout << "s1=" << s1 << endl;//hhllo
}
int main()
{
test1();
return 0;
}
1.8 string插入和删除
功能描述:
-
对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个字符
#include<iostream>
using namespace std;
#include<string>
void test1()
{
//插入
string s1 = "hello";
s1.insert(1, "520");
cout << "s1=" << s1 << endl;//h520ello
//删除
s1.erase(1, 3);
cout << "s1=" << s1 << endl;//hello
}
int main()
{
test1();
return 0;
}
1.9 string子串
功能描述:
从字符串中获取想要的子串
函数原型:
string substr(int pos=0,int n= npos)const; //返回由pos开始的n个字符组成的字符串
#include<iostream>
using namespace std;
#include<string>
void test1()
{
string s1 = "abcdefg";
string s2 = s1.substr(1,3);
cout << "s2=" << s2 << endl;
string s3 = "zhangsan123456789@163.com";
int pos = s3.find("@");
string s4 = s3.substr(0, pos);
cout << "s4="<<s4 << endl;//zhangsan123456789
}
int main()
{
test1();
return 0;
}