使用C++中string类时必须包含
#include <string>
using namespace std;
string基本概念
本质:
string是C++风格的字符串,而string本质上是一个类
string和char*的区别:
char*是一个指针
string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器
特点:
string内部封装了很多成员方法
例如:查找find,删除delete,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责
string的属性
Ⅰ.容量
①capacity()
1.Visual Studio默认容量大小为:少于15个时,申请15个容量(至少申请15个),以后多余容量就增加16个大小。
2.VC6.0至少申请31个空间,以后每次增加32个。
举例在Visual Studio下:
string str;
cout<<str.capacity()<<endl; //输出15
②reserve();
修改容量,只能将容量往大了改,规律遵循capacity()。
string str;
cout<<str.reserve(17)<<endl; //输出31,即15+16。
Ⅱ.字符串长度
length();
Ⅲ.字符个数
size();
Ⅳ.重新设置字符个数
resize(); 调用之后容量不变,改变的是字符的个数
Ⅴ.判断字符串是否为空
empty(); 字符串为空返回1,否则返回0
string类的构造函数
①string();
无参构造函数
string str;
②string(size_type length,char ch);
将length个ch赋值给字符串
string str1(3,'h'); //将3个h赋值给字符串str1
③string(const char *s);
用字符串s初始化
string str2("abcdefg"); // str2=abcdefg
④string(const char *s,size_type length);
用字符串s的前length个字符初始化
string str3("abcdefg",3); //str3=abc
⑤string(string &s,size_type index,size_type length);
用字符串s中从下标为index的字符开始向后顺延length个长度的字符串初始化
string str2("abcdefg");
string str4(str2,2,5); //str4=cdefg
⑥string(const string &s);
拷贝构造
string str2("abcdefg");
string str5(str2); //str5=abcdefg
string类的赋值
功能:给string字符串进行赋值
①string &operator=(const string *s);
char* 类型的字符串赋值给当前的字符串
string s1="Hello C++";
②string &operator=(const string &s);
把字符串s赋值给当前字符串
string s1="Hello C++";
string s2=s1; //s2=Hello C++
③string &operator(char c);
字符赋值给当前的字符串
string s3="s";
④string &assign(const char *s);
char* 类型的字符串赋值
string s4;
s4.assign("Hello C++");
⑤string &assign(const string &s);
把字符串S赋值给当前字符串
string s1="Hello C++";
string s5;
s5.assign(s1);
⑥string &assign(const char *s,int n);
用char*字符串s的前n个字符赋值
string s6;
s6.assign("Hello C++",5); //s6=Hello
⑦string &assign(int n,char c);
用n个字符c赋值
string s7;
s7.assign(3,'j'); //s7=jjj
⑧string &assign(const string &s,int start,int n);
把字符串s中从start开始的n个字符赋值给当前字符串
string s1="Hello C++";
string s8;
s8.assign(s1,2,3); //s8=llo
string类的字符串拼接
功能:实现在字符串末尾拼接字符串
①string& operator+=(const char* s);
重载+=操作符
string s1="I";
s1+="love C++"; //s1=I love C++
②string& operator+=(const char c);
重载+=操作符
string s2="m";
s2+='e'; //s2=me
③string& operator+=(const string& s);
重载+=操作符
string s3="Tom and"
s3+=s2; //s3=Tom and me
④string& append(const char* s);
将字符串s拼接到当前字符串尾部
string s4="he";
s4.append("llo"); //s4=hello
⑤string& append(const char* s,int n);
将字符串s的前n个字符拼接到当前字符串尾部
string s5="Haha";
s5.append("and heiehi",3); //s5=Hahaand
⑥string& append(const string &s);
同operator+=(const string& s)
string s6="Je";
s6.append(s4); //s6=Jehello
⑦string& append(const string& s,int n,int m);
将字符串s中从下表为n的字符开始的m个字符连接到当前字符串尾部
string s7="Jerry";
s7.append(s3,4,6); //s7=Jerryand me
string类的字符串查找
功能:查找指定字符串是否存在
①int find(const char* s,int pos=0)const;
查找s第一次出现的位置,从pos位置开始查找,若找到返回下标,否则返回-1
string s1="abcdefgde";
int pos=s1.find("de"); //pos=3
②int find(const char* s,int pos,int n)const;
从pos位置查找s的前n个字符第一次出现位置,若找到返回下标,否则返回-1
pos=s1.find("dedede",5,2); //pos=7
③int rfind(const char* s,int pos=npos)const;
从pos位置开始查找s最后一次出现的位置,若找到返回下标,否则返回-1
pos=s1.rfind("de"); //pos=7
find和rfind的区别:find从左往右查找,rfind从右往左查找,未找到时返回-1
string类的字符串替换
功能:在指定的位置替换字符串
①string& replace(int pos,int n,const string& s);
替换从pos位置开始的n个字符为字符串s
string s1="game";
string s2="I love study!";
s2.replace(7,5,s1); //s2=I love game!
②string& replace(int pos,int n,const char* s);
替换从pos位置开始的n个字符为字符串s
string s3="abcdefg";
s3.replace(1,3,"1111"); //s3=a1111efg
string类的字符串比较
功能:字符串之间的比较
字符串比较是按字符ASCII码进行对比
= 返回 0
> 返回 1
< 返回 -1
①int compare(const char* s) const;
当前字符串与字符串s比较
string s1="abcdefg";
int bol=s1.compare("abc"); //bol=1
②int compare(const string& s) const;
当前字符串与字符串s比较
string s2="abdwda";
bol=s1.compare(s2); //bol=-1
string类的字符存取
①char& operator[](int n);
通过[]方式取字符
string s1="hello world";
for(int i=0;i<s1.size();i++){
cout<<s1[i]<<" ";
}
//输出:h e l l o w o r l d ,即将字符串s1中的字符逐个提取并输出
②char* at(int n);
通过at方法获取字符
string s1="hello world";
for(int i=0;i<s1.size();i++){
cout<<s1.at(i)<<" ";
}
//输出:h e l l o w o r l d
注:还可通过此方式修改单个字符
eg:s1[0]='x';
s1.at(1)='p';
string类的字符串插入
①string& insert(int pos,const char* s);
在当前字符串pos位置插入字符串s
string s1="hello";
s1.insert(1,"1111"); //s1=h1111ello
②string& insert(int pos,const string& s);
在当前字符串pos位置插入字符串s
string s2="ho";
string s3="ell";
s2.insert(1,s3); //s2=hello
③string& insert(int pos,int n,char c);
在当前字符串pos位置插入n个字符c
string s4="abcdefg";
s4.insert(2,3,'w'); //s4=abwwwcdefg
string类的字符串删除
string& erase(int pos,int n=n);
从pos位置开始删除n个字符
string s5=h1111ello;
s5.erase(1,3); //s5=hello
string类的字串获取
功能:从字符串中获取想要的字串
string substr(int pos=0,int n=npos) const;
返回从pos位置开始的n个字符组成的字符串
string s="sbcdef";
string sub=s.substr(1,3); //sub=bcd
string类的迭代器
相当于一个指向string对象元素的指针,本质相当于一个char*的指针
string str="abcdefg";
string::iterator it=str.begin(); //迭代器指向字符串str的起始位置
string::iterator ite=str.end(); //迭代器指向字符串str最后一个字符后面的位置
通过迭代器赋值:
*it='f';
it[i]='w';
string重新申请空间的时候,迭代器会失效,重新赋值。