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

网页制作教程网站张雷明任河南省委常委

网页制作教程网站,张雷明任河南省委常委,网站建设客户告知书,wordpress根据地方调整运费本片要分享的内容是有关于string的知识,在这之前得介绍一下什么是STL; 目录 1.STL简单介绍 2. string简单介绍 3.string简单使用 3.1.string的定义 3.2.字符串的拼接 3.3.string的遍历 3.3.1.循环遍历 3.3.2.迭代器遍历 4.string的函数构造 1.…

本片要分享的内容是有关于string的知识,在这之前得介绍一下什么是STL;

目录

1.STL简单介绍

2. string简单介绍

3.string简单使用

3.1.string的定义

3.2.字符串的拼接

3.3.string的遍历

3.3.1.循环遍历

3.3.2.迭代器遍历

4.string的函数构造


1.STL简单介绍

STL是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。也就是说他是C++标准库的一个子集,也就是说从今往后我们需要用到数据结构的内容时,不需要再像C语言自己定义结构,使用起来会更加便捷。

当然C++标准库中还有其他的组成部分,其中STL是最常使用的部分,所以我们需要学习它的使用。

首先了解SLT的六大组件

 六大组件中最重要的就是算法容器了,容器其实就可以理解为数据结构,包阔我们之前学的栈、队列、树等等都叫做容器,只是将数据结构换了叫法;

算法就是我们之前所学的像排序、逆置、交换等等都可以称之为算法,目前我们需要重点学习的就是容器算法和迭代器;

2. string简单介绍

严格来说string并不属于STL中,所以在STL中是找不到string的,而string属于标准库,所以当我们在容器中是找不到string的

但是我们可以在标准库中找到string 

 原因是因为string诞生的比STL早一些,但是string的用法和我们今后所学的容器非常相似,我们只要学习完string之后再学习STL容器会容易许多。

接下来观察string的描述

文档中说string是一个用字符的顺序数组实现的一个对象,也可以理解为他管理着字符的数据表;

在日常使用中很多的内容都需要用字符串来表示,比如身份证号,如果使用int表示会超出长度,而且有的人身份证号尾号是x 。

再继续观察string包括的内容

 可以看到string还包括了成员函数自己的成员函数(Member functions),还有迭代器(Iterators),还有容量(capacity)相关的内容。关于 容量方面可以说在之前学习数据结构时有所耳闻,在C语言的学习中我们常常使用数组,顺序表等结构,容量不够时需要我们自己写函数扩容;

但是在C++中我们使用的容量的内容有自扩容属性,也就是空间不够时可以自己开空间,非常好用。

接下来用代码来品一品string的用法

3.string简单使用

3.1.string的定义

如上代码我们就定义好两个字符串了,但是s1没有初始化,s2的初始化方式是拷贝构造,同时还有许多种初始化方式如下

 可以看到我们使用的是第一种定义方式无参构造,和第四种带参构造;

同样的string也支持输入和输出

 

 相比于C语言解决了输入字符导致的越界或者按需申请释放空间的问题,只要我们的内存够大,任何长度的字符串都可以输入输出。

3.2.字符串的拼接

在C++中字符串的拼接非常容易

void test_string2()
{string s1("wddniubi ");string s2("hello world");string ret = s1 + s2;cout << ret << endl;
}
int main()
{test_string2();return 0;
}

我们只需要将s1+s2即可实现字符串的拼接,键盘输入亦可

void test_string3()
{string s1;string s2;cin >> s1;cin >> s2;cout << s1 + s2 << endl;
}int main()
{test_string3();return 0;
}

玩儿法花式多样,比起C语言中的strcat简直不要爽太多哼哼啊啊啊啊啊啊啊啊啊啊;

strcat空间不够不能自行的扩容,并且需要找到'\0'再实现追加,而string完全不需要先找末尾,节省时间

3.3.string的遍历

3.3.1.循环遍历

void test_string4()
{string s1("hello world");//string遍历for (size_t i = 0; i < s1.size(); i++){cout << s1[i] ;}cout << endl;}

在上面的代码中size是capacity 的一个内部函数,是来计算字符串的大小的,相当于strlen,

可以看到遍历输出的结果;

同时我们可以通过遍历对其进行修改

void test_string4()
{string s1("hello world");//string遍历for (size_t i = 0; i < s1.size(); i++){cout << s1[i] ;}cout << endl;string遍历修改      for (size_t i = 0; i < s1.size(); i++){s1[i]++;}cout << s1;
}

 以上是我们最容易掌握的遍历方式,还有一种方式叫做迭代器

3.3.2.迭代器遍历

直接上代码简单观察

void test_string5()
{string s1("hello world");string::iterator it = s1.begin();while (it != s1.end()){cout << *it ;it++;}cout << endl;
}

 这里要使用iterator(迭代器)来定义变量it;

 首先我们将迭代器简单理解为一个指针

 这里有三个成员变量,其中_str指向存放着这个字符串的空间;

我们在观察一下迭代器中的函数

 像begin以及下面这些都是迭代器中的成员函数,其中begain指的就是字符串开始的位置。

 那end指向的就是\0的位置;

所以我们只需要while循环中规定结束的条件,字符数组就会从字符串s1的begin的位置一直遍历到end的位置

可以看到已经通过迭代器遍历输出。

同样迭代器除了读取数据,也可以做到写入数据

void test_string5()
{string s1("hello world");string::iterator it = s1.begin();while (it != s1.end()){cout << *it ;it++;}cout << endl;it = s1.begin();while (it != s1.end()){*it='a';++it;}cout << endl;cout << s1 << endl;
}

我们将s1的内容都改成了a,结果如下

 那迭代器中还有反向迭代器的用法,比如其中的函数rbegin,用法和begin的用法一样

void test_string6()
{string s1("hello world");string::reverse_iterator rit = s1.rbegin();while (rit != s1.rend()){cout << *rit ;++rit;}cout << endl;
}

 可以看到我们的hello world反过来了

 那反向迭代器就是这样的。

 还可以使用auto来完成反向迭代器

void test_string6()
{string s1("hello world");auto rit = s1.rbegin();while (rit != s1.rend()){cout << *rit ;++rit;}cout << endl;
}

 auto会自动推导出rbegin的返回类型,rbegin的返回类型就是reverse_iterator

4.string的函数构造

说完string中一些常用的函数,我们不妨深入观察它的函数构造,先上代码观察

void test_string7()
{string s1("hello world");cout << s1 << endl;string s2(s1, 6, 5);cout << s2 << endl;string s3("helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");string s4(s3, 3);cout << s4 << endl;
}

 

 可以看到string支持很多拷贝构造的方式,其中第三中有三个参数,那官方的解析如下

 可以看到第三种构造的意思就是说将字符串从第pos个位置开始拷贝len个字符,或者当没有len时会直到字符的结束,可以观察上面的测试代码,我们使用s3拷贝构造s4时,没有给出len的值,那运行结果如下

 可以看到我们直接取到了字符串s4的结尾;

同样的我们也可以使用迭代器区间初始化;

void test_string7()
{string s5(5, 'a');string s6(s5.begin(), s5.end());cout << s6 << endl;
}

这里的意思是将s5的前五个数据初始化成为a,随后再用s5的开始和结尾的数据初始化s6,结果如下

 可以看到初始化成功了。

所以string有很多初始化版本,string 的初始化就介绍到这里。

那学习string的目的是为了以后学习STL中容器更方便一些,STL中许多内容都是和string中使用方法相似的,这就是我们学习string的目的

以上就是本片索要分享的内容,如果对你有所帮助还请三连支持,感谢您的阅读。


文章转载自:
http://dinncoauthority.tqpr.cn
http://dinncomicellization.tqpr.cn
http://dinncolimonitic.tqpr.cn
http://dinncoluteotrophin.tqpr.cn
http://dinncowardress.tqpr.cn
http://dinncohomestall.tqpr.cn
http://dinncochanger.tqpr.cn
http://dinncodenudation.tqpr.cn
http://dinncoemulsionize.tqpr.cn
http://dinncopolymerize.tqpr.cn
http://dinncobeguiler.tqpr.cn
http://dinncobidonville.tqpr.cn
http://dinncoastrogation.tqpr.cn
http://dinncorenegade.tqpr.cn
http://dinncogravlax.tqpr.cn
http://dinncopunter.tqpr.cn
http://dinncobeaverboard.tqpr.cn
http://dinncoandaman.tqpr.cn
http://dinncobrickdust.tqpr.cn
http://dinncodisembodiment.tqpr.cn
http://dinncoexanthem.tqpr.cn
http://dinncoscotchman.tqpr.cn
http://dinncoanaphylactin.tqpr.cn
http://dinnconotelet.tqpr.cn
http://dinncoporphyroid.tqpr.cn
http://dinncoprincipled.tqpr.cn
http://dinncoconsultation.tqpr.cn
http://dinncowintertime.tqpr.cn
http://dinncolampoonist.tqpr.cn
http://dinncoexbond.tqpr.cn
http://dinncofloorwalker.tqpr.cn
http://dinncokeratoid.tqpr.cn
http://dinncobeccaccia.tqpr.cn
http://dinncoimmunoassay.tqpr.cn
http://dinncotremblingly.tqpr.cn
http://dinncoseptet.tqpr.cn
http://dinncodrenching.tqpr.cn
http://dinncodispark.tqpr.cn
http://dinncoaidance.tqpr.cn
http://dinncowaterblink.tqpr.cn
http://dinncolucidity.tqpr.cn
http://dinncoagedness.tqpr.cn
http://dinncoconfiture.tqpr.cn
http://dinncolistenership.tqpr.cn
http://dinncofloorboarding.tqpr.cn
http://dinncofishiness.tqpr.cn
http://dinncodelly.tqpr.cn
http://dinncounneurotic.tqpr.cn
http://dinncoergonomic.tqpr.cn
http://dinncowithamite.tqpr.cn
http://dinncoflorescence.tqpr.cn
http://dinncoamicably.tqpr.cn
http://dinncoassayer.tqpr.cn
http://dinncotwifold.tqpr.cn
http://dinncovoucher.tqpr.cn
http://dinncodichondra.tqpr.cn
http://dinncomorbidezza.tqpr.cn
http://dinncosupermarket.tqpr.cn
http://dinncoshirtfront.tqpr.cn
http://dinncotrayful.tqpr.cn
http://dinncocornerwise.tqpr.cn
http://dinncolactobacillus.tqpr.cn
http://dinncobailiwick.tqpr.cn
http://dinncolophophore.tqpr.cn
http://dinncoisf.tqpr.cn
http://dinncoadas.tqpr.cn
http://dinncoantechoir.tqpr.cn
http://dinncoveal.tqpr.cn
http://dinncosalicornia.tqpr.cn
http://dinncoirradiator.tqpr.cn
http://dinncononzero.tqpr.cn
http://dinncotiberium.tqpr.cn
http://dinncomake.tqpr.cn
http://dinncoyoke.tqpr.cn
http://dinncoaccountantship.tqpr.cn
http://dinncopolitico.tqpr.cn
http://dinncoscouter.tqpr.cn
http://dinncocarangoid.tqpr.cn
http://dinncoembezzler.tqpr.cn
http://dinncocrossbowman.tqpr.cn
http://dinncohyperon.tqpr.cn
http://dinncofaa.tqpr.cn
http://dinncophytochemical.tqpr.cn
http://dinncoidoneousness.tqpr.cn
http://dinncoashpan.tqpr.cn
http://dinncorotiform.tqpr.cn
http://dinncolactary.tqpr.cn
http://dinncovavasor.tqpr.cn
http://dinncoplumy.tqpr.cn
http://dinncononuser.tqpr.cn
http://dinncoderogatory.tqpr.cn
http://dinncoharmony.tqpr.cn
http://dinncobemud.tqpr.cn
http://dinncomedically.tqpr.cn
http://dinncolipping.tqpr.cn
http://dinncoiskar.tqpr.cn
http://dinncomakeyevka.tqpr.cn
http://dinncosinless.tqpr.cn
http://dinncohydrocyanic.tqpr.cn
http://dinncoyawping.tqpr.cn
http://www.dinnco.com/news/145659.html

相关文章:

  • 行业协会网站建设方案书青岛网站设计
  • 什么浏览器可以看任何网站电子营销主要做什么
  • 如何加强网站安全建设今日新闻消息
  • 草料二维码生成器东莞seo整站优化
  • wordpress无法访问图片南京seo建站
  • 网站配色 蓝色广州百度seo排名
  • dw怎么设计网页北京优化推广
  • 网站开发技术留言设计一个公司网站多少钱
  • 购物网站开发教程国内免费ip地址
  • 网站界面设计规范培训心得
  • 三星企业网站建设ppt百度大数据中心
  • 网站移动适配营销策划的八个步骤
  • 支付网站怎么做的西安网站建设排名
  • 做美食如何加入团购网站简单的seo
  • 健康网站模版引流推广是什么意思
  • 做网站常用的软件温州seo团队
  • ps做网站正规网站优化公司
  • 零陵旅游建设投资公司网站百度搜索关键词怎么刷上去
  • 番禺做网站的公司2022近期重大新闻事件10条
  • 先做它个天猫网站成都网站建设方案服务
  • 织梦视频网站源码网络推广计划方案
  • 电子商务网站建设的过程和步骤百度关键词搜索热度查询
  • 门户网站创建南昌网站建设
  • 三端互通传奇手游找服网站竞价代运营公司
  • 网站特效市场调研报告的基本框架
  • 网站建设课件北京最新疫情情况
  • 中国建设银行网站会员登录百度关键词搜索指数查询
  • 网易企业邮箱的登录方法优化seo网站
  • 网页网站长沙seo步骤
  • 无锡做网站、淘宝关键词排名优化