string类

string类
一、string类的字符操作
1、string定义:可变长的字符串
2、string字符串初始化,示例:
(1) string s = “hello world”;
(2) sting s1(“hello world2”);
(3) string *ps = new string(“hello world3”);
(4) string s4 = “”; //空字符串
3、string字符访问方式:使用[]或at函数
(1) cout << s1[3] << endl; //输出字符l
(2) cout << s1.at(4) << endl; //输出字符o
二者区别:越界情况[]不会报出异常,at函数会产生异常。
二、string类的特性(属性)
1、特性(*重点)
(1) int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素数) *
(2) int max_size()const; //返回string对象中可存放的最大字符串的长度 *
(3) int size()const; //返回当前字符串的大小 *
(4) int length()const; //返回当前字符串的长度
(5) bool empty()const; //当前字符串是否为空 *
(6) void resize(int len,char c); //把字符串当前大小置为len,并用字符c填充不足的部分
2、长度length、大小size(占有内存空间)此处相等,均不包含”\0”,常用size。
注:在C语言中strlen不包含”\0”而sizeof包含”\0”。
3、capacity规则:返回值总比它的size要大。
4、使用示例
(1) cout << s1.size() << endl; //前四种
(2) if(s4.empty() == true) {cout <<“s4 is null\n”;} //第五种
三、string类的输入/输出
1、cin >>用于输入,cout<<用于输出操作。
2、函数getline(istream &in,string &s);
用于从输入流in中读取字符串到s中,以换行符’\n’分开。
四、string类的赋值
1、赋值函数(会查会用即可)
(1) string &operator=(const string &s);//把字符串s赋给当前字符串
(2) string &assign(const char *s);//用c类型字符串s赋值
(3) string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
(4) string &assign(const string &s);//把字符串s赋给当前字符串
(5) string &assign(int n,char c);//用n个字符c赋值给当前字符串
(6) string &assign(const string &s,int start,int n);
//把字符串s中从start开始的n个字符赋给当前字符串
(7) string &assign(const_iterator first,const_itertor last);
//把first和last迭代器之间的部分赋给字符串
2、赋值具体使用示例:
(1) sting s5 =s1;
(2) string s5; s5 = s1;
(3) sting s5; s5.assign(s1); //此处使用的是第一种方法
(4) sting s5; s5.assign(s1,2,6); //给s5赋值s1在第2个字符开始的6个字符
五、string类的连接
1、连接函数
(1) string &operator+=(const string &s);
//把字符串s连接到当前字符串的结尾
(2) string &append(const char *s);
//把c类型字符串s连接到当前字符串结尾
(3) string &append(const char *s,int n);
//把c类型字符串s的前n个字符连接到当前字符串结尾
(4) string &append(const string &s);
//同operator+=()
(5) string &append(const string &s,int pos,int n);
//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
(6) string &append(int n,char c);
//在当前字符串结尾添加n个字符c
(7) string &append(const_iterator first,const_iterator last);
//把迭代器first和last之间的部分连接到当前字符串的结尾
2、连接具体使用示例:(将s1\abc\s2连接在一起赋给s5)
(1) string s5 = s1 + “abc” + s2;
(2) string s5; s5.append(s1); s5.append(“abc”); s5.append(s2);
六、string类的比较
1、运算符">","<",">=","<=","!="均被重载用于字符串的比较。
使用示例:
if(s == s1) { cout <<“s == s1”<< endl; }
2、string自带重载函数:(compare函数在>时返回1,<时返回-1,==时返回0)
(1) int compare(const string &s) const;
//比较当前字符串和s的大小
(2) int compare(int pos, int n,const string &s)const;
//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
(3) int compare(int pos, int n,const string &s,int pos2,int n2)const;
//比较当前字符串从pos开始的n个字符组成的字符串与s中
//pos2开始的n2个字符组成的字符串的大小
(4) int compare(const char *s) const;
//以下为使用指针修饰的字符串
(5) int compare(int pos, int n,const char *s) const;
(6) int compare(int pos, int n,const char *s, int pos2) const;
七、string类的查找函数
1、find 函数
补充:pos:position的缩写,表示一个位置。
(1) int find(char c, int pos = 0) const;
//从pos开始查找字符c在当前字符串的位置
(2) int find(const char *s, int pos = 0) const;
//从pos开始查找字符串s在当前串中的位置
(3) int find(const char *s, int pos, int n) const;
//从pos开始查找字符串s中前n个字符在当前串中的位置
(4) int find(const string &s, int pos = 0) const;
//从pos开始查找字符串s在当前串中的位置
2、find_first_not_of函数
从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
(1) int find_first_not_of(char c, int pos = 0) const;
//从pos开始查找在当前串中第一个没有指定字符c出现的位置
(2) int find_first_not_of(const char *s, int pos = 0) const;
//从pos开始查找在当前串中第一个没有指定子串s出现的位置
(3) int find_first_not_of(const char *s, int pos,int n) const;
//从pos开始查找在当前串前n个字符中第一个没有指定子串s出现的位置
(4) int find_first_not_of(const string &s,int pos = 0) const;
//从pos开始查找在当前串中第一个没有指定子串s出现的位置
4、 find_last_of函数
补充:string::npos静态成员常量:是对类型为size_t的元素具有最大可能的值。当这个值在字符串成员函数中的长度或者子长度被使用时,该值表示“直到字符串结尾”。作为返回值他通常被用作表明没有匹配。
(1) int find_last_of(char c, int pos = npos) const;
(2) int find_last_of(const char *s, int pos = npos) const;
(3) int find_last_of(const char *s, int pos, int n = npos) const;
(4) int find_last_of(const string &s,int pos = npos) const;
4、find_last_not_of函数
(1) int find_last_not_of(char c, int pos = npos) const;
(2) int find_last_not_of(const char *s, int pos = npos) const;
(3) int find_last_not_of(const char *s, int pos, int n) const;
(4) int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似
//只不过是从后向前查找
4、 使用示例
int pos = s1.find(“hello”); //设 s1 为“hi hello world2”
cout << pos << endl; //输出匹配到的第一个hello字串位置:3
八、string类的迭代器处理
1、迭代器作用:遍历(一个一个访问)的容器(string\vector),也可理解为指针的泛化。
2、迭代器声明:
(1)用string::iterator或string::const_iterator声明迭代器变量。
(const_iterator不允许改变迭代的内容)
(2)如需从后向前迭代遍历(如rbegin、rend),则通过设置迭代器string::reverse_interator或string::const_reverse_interator来实现。
3、迭代器函数(以下均以无const修饰类型)
(1) iterator begin(); //返回string的起始位置
(2) iterator end(); //返回string的最后一个字符后面的位置
(3) iterator rbegin();//返回string的最后一个字符的位置
(4) iterator rend(); //返回string第一个字符位置的前面
3、使用示例
string :: iterator it; // 定义一个迭代器it
//begin()指向第一个字符,end()指向最后一个有效字符(数据)的下一个
for(it = s5.begin(); it != s5.end(); it++)//设s5 = “hello world”
{
*it = ‘1’; //it代表指针指向的位置,此处使用可使s5全部变为’1’
cout << *it << endl; //每次换行输出一个字符,此处输出11行单个字符’1’
}
cout << s5 << endl; //此处输出11111111111
九、string类的删除函数
1、删除函数
(1) iterator erase(iterator first, iterator last);
//删除[first,last)之间的所有字符,返回删除后迭代器的位置
(2) iterator erase(iterator it);
//删除it指向的字符,返回删除后迭代器的位置
(3) string &erase(int pos = 0, int n = npos);
//删除pos开始的n个字符,返回修改后的字符串
2、使用示例
(1) s5.erase(s5.begin() + 2,s5.begin()+6);
//设 s5 = “01234567890”,此处删除[2,6)即’2345’
cout << s5 << endl; //此处输出’0167890’
(2) for(it = s5.begin(); it != s5.end()😉 //设s5 = “hello worldl”
{
if(*it == ‘l’)
it = s5.erase(it);
//删除s5中所有的l,返回删除后迭代器的位置
elseit ++;
}
cout << s5 << endl; //输出’heo word’

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值