关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

STL-string容器基本操作

发布时间:2023-06-30 15:00:37

string基本概念

本质:string是C++风格的字符串,本质上是一个类

stringchar*的区别:

  • char*是一个指针
  • string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器

特点:

  • string类内部封装了很多成员方法,如:查找find,拷贝copy,删除delete,替换replace,插入insert
  • string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

string构造函数

构造函数原型:

  • string();创建一个空的字符串,如:string str;
  • string(const char* s);使用字符串s初始化
  • string(const char& str);使用一个string对象初始化另一个string对象
  • string(int n,char c);使用n个字符c初始化

string的多种构造方式没有可比性,灵活使用即可

//默认构造 string a; //使用字符串初始化 const char* b = "hello"; string c(b); cout << c << endl; //使用一个string对象初始化另一个string对象 string d(c); cout << d << endl; //使用n个字符c初始化 string e(5, 'a'); cout << e << endl;

   


string赋值操作

string赋值操作有很多,operator=这种方式是比较实用的:

  • string& operator=(const char* s); 字符串赋值给当前的字符串
  • 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 a; //字符串赋值给当前的字符串 a = "hello"; cout << a << endl;//hello //把字符串s赋给当前的字符串 string b; b = a; cout << b << endl;//hello //字符赋值给当前的字符串 b = 'h'; cout << b << endl;//h //assign方式 b.assign("world"); cout << b << endl;//world b.assign("world", 1); cout << b << endl;//w b.assign(a); cout << b << endl;//hello b.assign(5, 'a'); cout << b << endl;//aaaaa

   


string字符串拼接

实现在字符串末尾拼接字符串

两种方法的函数原型:

  • 重载+=操作符
    • string& operator+=(const char* str);
    • string& operator+=(const char c);
    • string& operator+=(const string& str);
  • 成员函数append()
    • string& append(const char* s);
    • string& append(const char* s, int n);字符串的前n个字符
    • string& append(const string& s);
    • string& append(const string& s, int pos, int n);从第pos个位置开始,截取n个字符
string a = "hello"; a.append("world", 2, 2); cout << a << endl;//hellorl

   


string查找和替换

  • 查找:查找自定字符串是否存在
  • 替换:在指定的位置替换字符串

find是从左往右查找:

  • size_t find(const string& str, int pos = 0) const;//查找str第一次出现位置,从pos开始查找
  • size_t find(const char* s, int pos = 0) const;//查找s第一次出现位置,从pos开始查找
  • size_t find(const char* s, int pos, int n) const;//从pos位置查找s的前n个字符第一次位置
  • size_t find(const char c, int pos = 0) const;//查找字符c第一次出现位置

rfind是从右往左查找:

  • size_t rfind(const string& str, int pos = npos) const;//查找str最后一次位置,从pos开始查找
  • size_t rfind(const char* s, int pos = npos) const;//查找s最后一次出现位置,从pos开始查找
  • size_t rfind(const char* s, int pos, int n) const;//从pos查找s的前n个字符最后一次位置
  • size_t 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
string a = "hello"; cout << a.find("el") << endl;//1 a.replace(1, 2, "被替换"); cout << a << endl;//h被替换lo cout << a.find("world") << endl;//18446744073709551615 cout << (int)a.rfind("world") << endl;//-1

   

find()返回值-118446744073709551615

  • find()返回的是size_t,无符号整型,-1在内存中的储存以无符号视角看就是一个很大的数
  • 如果要显示为-1,需要强制类型转换为int类型

string字符串比较

字符串的比较是按字符串的ASCII码进行对比

返回值类型是int,不需要做强制类型转换

  • =返回0

返回1

  • <返回-1

字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大

string a = "hello"; cout << a.compare("hell") << endl;//1 cout << a.compare("hello") << endl;//0 cout << a.compare("hellz") << endl;//-1

   


string字符存取

string中单个字符存取有[]at两种方式:

  • char& operator[](int n);
  • char& at(int n);
  • 使用size()获取字符串长度
string a = "hello"; //使用size()获取字符串长度 for (int n=0; n < a.size(); n++) {  cout << a[n] << endl; }

     

//修改单个字符 a[0] = 'w'; a.at(1) = 'w'; for (int n = 0; n < a.size(); n++) {  cout << a[n] << " "; }

   


string插入和删除

插入字符串:

  • string& insert(int pos, const char* s);
  • string& insert(int pos, const string* str);

在指定位置插入n个字符c:

  • string& insert(int pos, int n, char c);

删除从pos开始的n个字符:

  • string& erase(int pos, int n =npos);
string a = "hello"; a.erase(2); cout << a << endl;//he a = "hello"; a.erase(2, 2); cout << a << endl;//heo

   


string子串获取

string substr(int pos = 0, int n = npos)const;

string a = "hello"; cout << a.substr(1, 2) << endl;//el cout << a.substr(1) << endl;//ello

   

从邮箱中提取信息:

string a = "123456@qq.com"; int pos = a.find('@'); cout << "用户名:" << a.substr(0, pos) << endl;//123456 cout << "服务器:" << a.substr(pos + 1) << endl;//qq.com

/template/Home/leiyu/PC/Static