STL——string类的使用

1.string类(了解)

string类的文档介绍
在使用string类时,必须包含#include头文件以及using namespace std;

2.string类的接口函数

2.1 string类对象常见的构造函数

在这里插入图片描述

string s1;//空字符串
string s2("hello world");//用C-string来构造string类对象
string s3(s2);//拷贝构造

2.2 string类对象的容量操作

  • size(重点):返回字符串的有效长度
string s1("hello world");
cout << s1.size() << endl;//11

跟C语言的strlen函数一样,不包含字符串末尾的'\0'

string s1("hello world");
cout << s1.capacity() << endl;
  • empty(重点):检查字符串是否为空串,返回true不是返回false
string s1("hello world");
cout << s1.empty() << endl;

clear:清除有效字符

string s1("hello world");
cout << s1 << endl;//hello world
s1.clear();
cout << s1 << endl;//字符串为空
  • reserve(重点):为字符串预留空间
//该代码在VS2022编译器下的结果:
string s("hello world");
cout << s.size() << endl;//11
cout << s.capacity() << endl;//15

s.reserve(13);
cout << s.size() << endl;//11
cout << s.capacity() << endl;//15

s.reserve(5);
cout << s.size() << endl;//11
cout << s.capacity() << endl;//15

s.reserve(30);
cout << s.size() << endl;//11
cout << s.capacity() << endl;//31

在这里插入图片描述

reserve(size_t n=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小.

  • resize(重点):将有效字符串的个数改成n个,多出来的空间用字符c填补
    在这里插入图片描述
string s("hello world");
cout << s << endl;//hello world
s.resize(15, 'x');
cout << s << endl;//hello worldxxxx
s.resize(4);
cout << s << endl;//hell

注意:

resize(size_t n)resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变

2.3 string类对象的访问及遍历操作

  • operator[](重点):返回pos位置的字符,const string类对象调用
string s("hello world");
cout << s << endl;//hello world
s[0] = 'x';
cout << s << endl;//xello world
  • begin+end:begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
    在这里插入图片描述
  • 三种遍历方式

    • 下标+[ ]

      string s("hello world");
      for (size_t i = 0; i < s.size(); ++i)
      {
      	cout << s[i] << " ";
      }
      cout << endl;
      //h e l l o   w o r l d
      
    • 迭代器

      //迭代器
      string s("hello world");
      string::iterator it = s.begin();//it相当于指针,但本质上不是指针
      while (it != s.end())
      {
      	cout << *it << " ";
      	++it;
      }
      cout << endl;
      //h e l l o   w o r l d
      
    • 范围for:自动迭代,自动取数据,自动判断结束(底层实际就是迭代器)

      //范围for
      string s("hello world");
      for (auto e : s)
      {
      	cout << e << " ";
      }
      cout << endl;
      
      • auto关键词

      1). auto 作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期推导而得
      2). 用auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时则必须加&
      3). 当在同一行声明多个变量时,这些变量必须是相同的类型,否则编译器将会报错,因为编译器实际只对第一个类型进行推导,然后用推导出来的类型定义其他变量
      4). auto不能作为函数的参数,可以做返回值,但是建议谨慎使用
      5). auto不能直接用来声明数组

      // 编译报错:error C3538: 在声明符列表中,“auto”必须始终推导为同一类型
      auto cc = 3, dd = 4.0;
      // 编译报错:error C3318: “auto []”: 数组不能具有其中包含“auto”的元素类型
      auto array[] = { 4, 5, 6 };
      
      // 不能做参数
      void func2(auto a)
      {}
      // 可以做返回值,但是建议谨慎使用
      auto func3()
      {
      	return 3;
      } 
      

2.4 string类对象的修改

string s("hello world");
cout << s << endl;//hello world
s.push_back('#');
cout << s << endl;//hello world#
  • append:在一个字符串后面追加一个字符串
    在这里插入图片描述
string s("hello world");
cout << s << endl;
s.append("$$$");
cout << s << endl;
string s1("hhhhhh");
s.append(s1);
cout << s << endl;
  • operator+=(重点) :在字符串后面追加字符串str
    在这里插入图片描述
string s("hello world");
cout << s << endl;//hello world
s += "$$$";
cout << s << endl;//hello world$$$
s += 'x';
cout << s << endl;//hello world$$$x
string s1("@@@");
s += s1;
cout << s << endl;//hello world$$$x@@@
  • find :从字符串pos位置开始往后找字符c,返回该字符在字符串中的位
string s("test.cpp.zip");
size_t pos = s.find('.');
cout << pos << endl;//4
pos=s.rfind('.');
cout<<pos<<endl;//8

string s1("ahnbbieneofmncinem");
pos = s1.find("abc");//寻找字符串,并返回找到的第一个位置的下标
//整个字符串必须完全匹配才算找到
cout << pos << endl;

在这里插入图片描述

  • substr:在str中从pos位置开始,截取n个字符,然后将其返回
string s("test.cpp.zip");
size_t pos = s.find('.');
cout << pos << endl;//4
cout << s.substr(pos) << endl;//.cpp.zip

2.5 string类非成员函数

  • operator+:尽量少用,因为传值返回,导致效率低
string s1("hello ");
string s2("world");
string s3 = s1 + s2;
cout << s3 << endl;//hello world
  • getline :获取一行字符串,遇到空格不会停止,遇到’\n’才停止
//输入hello world
string s;
cin >> s;//cin遇到空格,'\n'就停止
cout << s << endl;//hello
string s1;
getline(cin, s1);
cout << s1 << endl;//hello world
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值