你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

string容器详解

[复制链接]
gaosmile 发布时间:2020-10-6 21:33

一、string基本概念:

1、本质

string是c++风格的字符串,而string本质上是一个类。

2、string和char *的区别

-char *是一个char类型的指针类型

-string是一个类,类内部封装了char*,是一个char型的容器。string类内部封装了很多成员函数,比如:查找find,拷贝copy,删除delete,替换replace,插入insert;string管理char所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

二、string构造函数:

构造函数原型:

  • string();//创建一个空的字符串,例如string str

  • string(const char* s);//使用字符串s初始化

  • string(const string& str);//使用一个string对象初始化另外一个string对象,就是拷贝构造函数

  • string(int n, char c);//使用n个字符c初始化


代码应用举例:

#include <iostream>
#include <string>
using namespace std;

void test()
{
   string s1;

   const char *str = "txp";
   string s2(str);

   cout << "s2= "<<s2<<endl;

   string s3(str);
   cout <<"s3= "<<s3<<endl;

   string s4(10,'a');
   cout <<"s4= "<<s4<<endl;
}
int main()
{
    test();
}

输出结果:

root@txp-virtual-machine:/home/txp/test2# ./a.out
s2= txp
s3= txp
s4= aaaaaaaaaa

三、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);//把字符串的前n个字符赋给当前的字符串

  • string& assign(const string &s);//把字符串s赋给当前字符串

  • string& assign(int n, char c);//用n个字符c赋给当前字符串


代码应用:

#include <iostream>
#include <string>
using namespace std;

void test()
{
   string str1;
   str1 = "txp";
   cout<<"str1= "<<str1<<endl;

   string str2;
   str2=str1;
   cout<<"str2 = "<<str2<<endl;

   string str3;
   str3='a';
   cout<<"str3= "<<str3<<endl;

   string str4;
   str4.assign("txp like cpp");
   cout<<"str4= "<<str4<<endl;

   string str5;
   str5.assign("txp like cpp",5);
   cout<<"str5= "<<str5<<endl;

   string str6;
   str6.assign(str5);
   cout<<"str6= "<<str6<<endl;

   string str7;
   str7.assign(10,'w');
   cout<<"str7= "<<str7<<endl;

}

int main()
{
  test();
}

结果输出:

root@txp-virtual-machine:/home/txp/test2# ./a.out
str1= txp
str2 = txp
str3= a
str4= txp like cpp
str5= txp l
str6= txp l
str7= wwwwwwwwww

四、string字符串拼接:

实现字符串末尾拼接字符串函数原型:

  • string& operator+=(const char* str);//重载+=操作符

  • string& operator+=(const char c);//重载+=操作符

  • string& operator+=(const string& str);//重载+=操作符

  • string& append(const char *s);//把字符串s连接到当前字符串结尾

  • string& append(const char *s,int n);//把字符串s的前n个字符连接到当前字符串结尾

  • string& append(const string &s);//同operator+=(const string& str)

  • string& append(const string &s,int pos,int n);//字符串s从pos开发的n个字符连接到字符串结尾


代码应用:

#include <iostream>
#include <string>
using namespace std;

void test()
{
   string str1 = "txp";
   str1 += "xiaoping";
   cout <<"str1= "<<str1<<endl;

   str1+=":";
   cout<<"str1= "<<str1<<endl;

   string str2 = "cpp";
   str1+=str2;
   cout<<"str1= "<<str1<<endl;

   string str3 = "I";
   str3.append("love");
   cout<<"str3= "<<str3<<endl;

   str3.append("game abcde",4);
   cout<<"str3= "<<str3<<endl;

   str3.append(str2);
   cout<<"str3= "<<str3<<endl;

   str3.append(str2,0,3);
   cout<<"str3= "<<str3<<endl;
}

int main()
{
  test();
}


结果输出:

root@txp-virtual-machine:/home/txp/test2# ./a.out
str1= txpxiaoping
str1= txpxiaoping:
str1= txpxiaoping:cpp
str3= Ilove
str3= Ilovegame
str3= Ilovegamecpp
str3= Ilovegamecppcpp


五、string查找和替换:

实现字符串的查找和替换函数原型:

  • int find(const string& str,int pos = 0)const;//查找str第一次出现位置从pos开始查找

  • int find(const char* s,int pos =0);//查找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 = 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


代码应用:

#include <iostream>
#include <string>
using namespace std;

void test()
{
  string str1 = "abcd";
  int pos = str1.find("bc");
  if(pos)
  {
     cout<< " find the word"<<endl;
  }
  else
  {
    cout<<" not find the word"<<endl;
  }
  cout<<"pos= "<<pos<<endl;
//rfind和find的区别:rfind从右往左查找,find从左往右查找
  pos = str1.rfind("cd");
  cout<<"pos= "<<pos<<endl;
}
void test1()
{
   string str2 = "abcd";
   str2.replace(1,3,"1111");
   cout<<"str2= "<<str2<<endl;

}

int main()
{
  test();
  test1();
}

结果输出:

root@txp-virtual-machine:/home/txp/test2# ./a.out
find the word
pos= 1
pos= 2
str2= a1111

六、string字符串比较:

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

= : 返回 0

>

: 返回 1

< : 返回 -1

函数原型:

  • int copare(const string &s)const;//与字符串s比较

  • int compare(const char *s)const;//与字符串s比较


代码应用:

#include <iostream>
#include <string>
using namespace std;

void test()
{
  string str1 = "txp";
  string str2 = "linux";

  if(str1.compare(str2)==0)
  {
     cout<<"str1 = str2 "<<endl;
  }
  else if(str1.compare(str2)>0)
  {
     cout<<"str1 > str2 "<<endl;
  }
  else
  {
     cout<<" str1 < str2 "<<endl;
  }
}


int main()
{
  test();

}

结果输出:

root@txp-virtual-machine:/home/txp/test2# ./a.out
str1 > str2

七、string字符存取:

实现字符存取函数原型:

  • char& operator[](int n);//通过[]方式取字符

  • char& at(int n);//通过at方法获取字符


代码应用:

#include <iostream>
#include <string>
using namespace std;

void test()
{
  string str = "linux";
//通过[]访问单个字符
  for(int i = 0;i< str.size();i++)
  {
     cout<<str<<" ";
  }
  cout<<endl;
//通过at方式访问单个字符
  for(int i =0; i<str.size();i++)
  {
     cout<<str.at(i)<<" ";
  }
  cout<<endl;
   //修改单个字符的方式
  str[0] = 'x';
  cout<<"str = "<<str<<endl;

  str.at(1) = 'x';
  cout<<"str = "<<str<<endl;
}


int main()
{
  test();

}

输出结果:

root@txp-virtual-machine:/home/txp/test2# ./a.out
l i n u x
l i n u x
str = xinux
str = xxnux

八、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>
#include <string>
using namespace std;

void test()
{
  string str = "linux";

  str.insert(1,"111");
  cout<<"str = "<<str<<endl;

  str.erase(1,3);
  cout<<"str = "<<str<<endl;
}


int main()
{
  test();
  
}

结果输出:

root@txp-virtual-machine:/home/txp/test2# ./a.out
str = l111inux
str = linux

九:string子串

实现从string字符串中获得子串的函数原型:

  • string substr(int pos =0 , int n =npos)const;//返回由pos开始的n个字符组成的字符串


代码应用:

#include <iostream>
#include <string>
using namespace std;

void test()
{
  string str = "linux";

  string substr = str.substr(1,3);
  cout<<"substr =  "<<substr<<endl;

  int pos = str.find('u');
  string ur = str.substr(0,pos);
  cout<<ur<<endl;
}


int main()
{
  test();
  
}

结果输出:

root@txp-virtual-machine:/home/txp/test2# ./a.out
substr =  inu
lin

收藏 评论0 发布时间:2020-10-6 21:33

举报

0个回答

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版