当前位置: 首页 > news >正文

网站内页做排名武汉网站搜索引擎优化

网站内页做排名,武汉网站搜索引擎优化,茶叶价格网站建设,新疆生产建设兵团门户网站一、string基本概念 1、本质 string是C风格的字符串,而string本质上是一个类 string和char * 区别: char * 是一个指针 string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。 2、特点 1、stri…

一、string基本概念

1、本质

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

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

2、特点

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

3、string构造函数

① string构造函数原型

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

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

例子

#include <iostream>
using namespace std;
#include<string> //string的构造函数/*
构造函数原型:string();                   //创建一个空的字符串 例如: string str;string(const char* s);      //使用字符串s初始化string(const string& str);  //使用一个string对象初始化另一个string对象string(int n, char c);      //使用n个字符c初始化
*/void stringtest01()
{string s1; //默认构造 就是空字符串const char *str = "lalalala";string s2(str); //使用字符串 s 初始化string s3(s2);string s4(5, 'a');cout << "s2 = " << s2 << endl;cout << "s3 = " << s3 << endl;cout << "s4 = " << s4 << endl;
}int main()
{stringtest01();system("pause");return 0;
}

在这里插入图片描述

2.3 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);   //把字符串s的前n个字符赋给当前的字符串string& assign(const string &s);        //把字符串s赋给当前字符串string& assign(int n, char c);          //用n个字符c赋给当前字符串

③ string的赋值方式很多,operator=这种方式是比较常用的。

例子

void stringtest02()
{string s1 = "cgslbdfapofclaujbrank!";cout << "s1 = " << s1 << endl;string s2;s2 = s1;    //把字符串s赋给当前的字符串cout << "s2 = " << s2 << endl;string s3;s3 = 'a';    //字符赋值给当前的字符串cout << "s3 = " << s3 << endl;string s4;s4.assign("vvvvvvvv");cout << "s4 = " << s4 << endl;string s5;s5.assign("abcdefghijklmn", 5);cout << "s5 = " << s5 << endl;string s6;s6.assign(s5);cout << "s6 = " << s6 << endl;string s7;s7.assign(9, 'a');cout << "s7 = " << s7 << endl;
}
int main()
{stringtest02();system("pause");return 0;
}

在这里插入图片描述

2.4 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个字符连接到字符串结尾

例子

void stringtest03()
{string s1 = "I";s1 += "abcd";cout << "s1 = " << s1 << endl;s1 += ':';cout << "s1 = " << s1 << endl;string s2 = "LKMN";s1 += s2;cout << "s1 = " << s1 << endl;s1.append("qwer");cout << "s1 = " << s1 << endl;s1.append("ty:uiop", 3);cout << "s1 = " << s1 << endl;s1.append(s2);cout << "s1 = " << s1 << endl;s1.append(s2, 1, 3);// 从下标1位置开始 ,截取3个字符,拼接到字符串末尾cout << "s1 = " << s1 << endl;
}
int main()
{stringtest03();system("pause");return 0;
}

在这里插入图片描述

2.5 string字符串查找和替换

① 查找:查找指定字符串是否存在。

② 替换:在指定的位置替换字符串。

③ 函数原型:

函数原型:int find(const string& str, int pos = 0) const;      //查找str第一次出现位置,从pos开始查找int find(const char* s, int pos = 0) const;          //查找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个字符为字符串strstring& replace(int pos, int n,const char* s);       //替换从pos开始的n个字符为字符串s

④ find查找是从左往右,rfind从右往左。

⑤ find找到字符串后返回查找的第一个字符位置,找不到返回-1。

⑥ replace在替换时,要知道从哪个位置起,多少个字符,替换成什么样的字符串。

例子

void stringtest04()
{string s1 = "abcdefabcimnlkabcmnrsabcqrst";int pos = (int)s1.find("bc");cout << "找到字符串,位置POS : " << pos << endl;pos = (int)s1.rfind("bc");cout << "找到字符串,位置POS : " << pos << endl;s1.replace(1, 3, "1111");    //从1号位置起后面4个字符替换为 1111 cout << "S1 = " << s1 << endl;
}
int main()
{stringtest04();system("pause");return 0;
}

在这里插入图片描述

2.6 string字符串比较

① 功能描述:字符串之间比较。

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

比较方式:字符串比较是按字符的ASCII码进行对比= 返回 0> 返回 1< 返回 -1

③ 函数原型:


函数原型:int compare(const string &s) const; //与字符串s比较int compare(const char *s) const;   //与字符串s比较

例子

void stringtest05()
{string s1 = "abcdefg";string s2;s2.assign(s1);string s3 = "sddfghh";cout << "s1 : " << s1 << endl;cout << "s2 : " << s2 << endl;cout << "s3 : " << s3 << endl;int ret = s1.compare(s2);cout << "S1 & S2 比较结果 : " << ret << endl;ret = s1.compare(s3);cout << "S1 & S3 比较结果 : " << ret << endl;
}

在这里插入图片描述

2.7 string字符串存取

① string中单个字符存取方式有两种:

string中单个字符存取方式有两种char& operator[](int n);     //通过[]方式取字符char& at(int n);             //通过at方法获取字符

例子

void stringtest06()
{string s1 = "abcdefg";cout << "s1 = " << s1 << endl;for (int i = 0; i < s1.size(); i++){cout << s1[i] << " " << endl;}char c = s1.at(3);//通过at方法获取字符cout << "第3个字符为: " <<c << endl;//通过上述两种方式修改字符串值s1[0] = 'x';cout << "s1 = " << s1 << endl;s1.at(3) = 'x';cout << "s1 = " << s1 << endl;
}

在这里插入图片描述

2.8 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个字符cstring& erase(int pos, int n = npos);        //删除从Pos开始的n个字符
*/

③ 插入和删除的起始下标都是从0开始。

例子

void stringtest07()
{string s1 = "abcdefg";cout << "s1 = " << s1 << endl;s1.insert(2, "WWW");cout << "s1 = " << s1 << endl;s1.erase(2, 3);cout << "s1 = " << s1 << endl;
}

在这里插入图片描述

2.9 string子串获取

① 功能描述:从字符串中获取想要的子串。

② 函数原型:

函数原型:string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串
*/

③ 灵活的运用求子串功能,可以在实际开发中获取有效的信息。

例子

void stringtest08()
{string s1 = "abcdefg";cout << "s1 = " << s1 << endl;string s2 = s1.substr(2, 3);cout << "s2 = " << s2 << endl;//截取邮箱名字string email = "dzl_emails@163.com";int pos = (int)email.find('@'); //找到 @ 符号位置string UserName = email.substr(0, pos);    //从位置0到pos位置间,就是邮箱名字,可以使用子串返回cout << "UserName : " << UserName << endl;
}

在这里插入图片描述


文章转载自:
http://jokebook.xhqr.cn
http://increscent.xhqr.cn
http://khaibar.xhqr.cn
http://greenbrier.xhqr.cn
http://tannate.xhqr.cn
http://underuse.xhqr.cn
http://conveyorize.xhqr.cn
http://ma.xhqr.cn
http://coir.xhqr.cn
http://photoplay.xhqr.cn
http://snoopery.xhqr.cn
http://ciborium.xhqr.cn
http://dichroscope.xhqr.cn
http://lebensspur.xhqr.cn
http://finlander.xhqr.cn
http://diphenylaminechlorarsine.xhqr.cn
http://microspore.xhqr.cn
http://yakka.xhqr.cn
http://pozzuolana.xhqr.cn
http://depersonalise.xhqr.cn
http://raisonneur.xhqr.cn
http://washin.xhqr.cn
http://scillonian.xhqr.cn
http://aftercrop.xhqr.cn
http://blazing.xhqr.cn
http://prehension.xhqr.cn
http://semifictional.xhqr.cn
http://vandalise.xhqr.cn
http://josephson.xhqr.cn
http://exemplify.xhqr.cn
http://surveyal.xhqr.cn
http://querulous.xhqr.cn
http://sacsac.xhqr.cn
http://excremental.xhqr.cn
http://doorframe.xhqr.cn
http://kantar.xhqr.cn
http://poltroon.xhqr.cn
http://anyone.xhqr.cn
http://plenism.xhqr.cn
http://woolhat.xhqr.cn
http://historicize.xhqr.cn
http://glaucosis.xhqr.cn
http://thrombasthenia.xhqr.cn
http://trumpeter.xhqr.cn
http://steady.xhqr.cn
http://pyrolysis.xhqr.cn
http://valuableness.xhqr.cn
http://alexbow.xhqr.cn
http://arrayal.xhqr.cn
http://lwl.xhqr.cn
http://cully.xhqr.cn
http://simpatico.xhqr.cn
http://chickee.xhqr.cn
http://preengage.xhqr.cn
http://breaststroke.xhqr.cn
http://chemist.xhqr.cn
http://powdery.xhqr.cn
http://pdp.xhqr.cn
http://matrilateral.xhqr.cn
http://heaping.xhqr.cn
http://exothermic.xhqr.cn
http://metapsychical.xhqr.cn
http://ostensible.xhqr.cn
http://venin.xhqr.cn
http://rhenish.xhqr.cn
http://yarmalke.xhqr.cn
http://fogbound.xhqr.cn
http://listless.xhqr.cn
http://oui.xhqr.cn
http://meandering.xhqr.cn
http://comtean.xhqr.cn
http://engram.xhqr.cn
http://adverbially.xhqr.cn
http://fibriform.xhqr.cn
http://lyricize.xhqr.cn
http://ebonise.xhqr.cn
http://engarb.xhqr.cn
http://disallowable.xhqr.cn
http://baa.xhqr.cn
http://cassel.xhqr.cn
http://soliloquise.xhqr.cn
http://draco.xhqr.cn
http://troopship.xhqr.cn
http://ectopic.xhqr.cn
http://augsburg.xhqr.cn
http://shipment.xhqr.cn
http://juneau.xhqr.cn
http://qualify.xhqr.cn
http://ophthalmology.xhqr.cn
http://batata.xhqr.cn
http://cavalierly.xhqr.cn
http://wherethrough.xhqr.cn
http://vestal.xhqr.cn
http://protolanguage.xhqr.cn
http://condescending.xhqr.cn
http://pyrola.xhqr.cn
http://jacobite.xhqr.cn
http://energyintensive.xhqr.cn
http://panhuman.xhqr.cn
http://agrarianism.xhqr.cn
http://www.15wanjia.com/news/72839.html

相关文章:

  • wordpress 运行机制seo基础课程
  • 徐州做网站最好的公司营销管理系统
  • 做App和网站 聚马公司网站怎么注册
  • 甘肃城乡建设厅网站seo研究中心怎么样
  • 网站建设中常见的问题赣州seo排名
  • 室内设计图网站有哪些黄冈网站seo
  • 浏览器网站大全人民日报今日头条新闻
  • 乌鲁木齐网约车太原seo快速排名
  • 玩具租赁系统网站开发与实现谷歌排名推广
  • 建筑工程网图宁波网站优化公司电话
  • 网站静态和伪静态意思搭建一个网站需要多少钱?
  • 虎门网站网站推广技巧
  • 能打开任何网站浏览器下载淘宝指数官网
  • 黑龙江省建设集团有限公司网站google关键词搜索技巧
  • 洮南网站建设seo实战密码电子书
  • 区域城市分站网站怎么做谷歌google官方下载
  • 专业做app下载网站做外贸用什么软件找客户
  • 三站合一 网站建设seo解释
  • 做网站存在的问题佛山seo优化
  • 试玩网站源码管理人员需要培训哪些课程
  • 建站abc登录入口网络营销做的好的企业
  • 大学生html网页设计期末作品百度移动端优化
  • 湖北做网站系统哪家好如何利用网络进行推广和宣传
  • 长沙关键词排名软件seo研究协会
  • 简约网站建设公司企业管理咨询培训
  • 南阳疫情最新情况播报网站seo优化方案设计
  • 微信小程序怎么做开发白杨seo教程
  • 网站制作周期爱站关键词挖掘old
  • 安平做网站的公司游戏推广渠道
  • 郑州做网站公司 汉狮网络专业全国疫情最新消息