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

福建省建设注册执业管理中心网站网络免费推广网站

福建省建设注册执业管理中心网站,网络免费推广网站,工业互联网平台排名,wordpress头部导航栏前言 在前一章种我们介绍了C中的模板的使用#xff0c;这是一种泛型编程#xff0c;模板的使用能让我们减少大量的相似代码#xff0c;减少我们的代码量与工作量#xff0c;写出更加高效简洁的代码#xff0c;模板如此好用#xff0c;但还是要我们先出写一个泛型类或函数…前言 在前一章种我们介绍了C中的模板的使用这是一种泛型编程模板的使用能让我们减少大量的相似代码减少我们的代码量与工作量写出更加高效简洁的代码模板如此好用但还是要我们先出写一个泛型类或函数才能使用。有没有办法直接不写泛型类或函数就能使用一些常见的数据结构与算法的代码呢答案是有的C给我们提供了标准模板库STL这是一个相当重要的库它就完美解决了上面的问题相信学完以后你会对它爱不释手 C标准模板库STL 一一、STL的基础介绍1. 什么是STL2. STL的版本3. STL的六大组件二、string类的介绍1. 为什么学习string类2. 标准库中的string类三、string类的常用接口说明1. string类对象的常见构造函数2. string类对象的容量操作3. string类对象的单个元素访问4. string类对象的元素的遍历5.string类对象的修改操作6.string类对象的一些其他操作7.getline函数四、结语一、STL的基础介绍 1. 什么是STL STL(standard template libaray-标准模板库)是C标准库的重要组成部分不仅是一个可复用的组件库而且是一个包罗数据结构与算法的软件框架。 2. STL的版本 原始版本 Alexander Stepanov、Meng Lee 在惠普实验室完成的原始版本本着开源精神他们声明允许任何人任意运用、拷贝、修改、传播、商业使用这些代码无需付费。唯一的条件就是也需要向原始版本一样做开源使用。 HP 版本–所有STL实现版本的始祖。P. J. 版本 由P. J. Plauger开发继承自HP版本被Windows Visual C采用不能公开或修改缺陷可读性比较低符号命名比较怪异。RW版本 由Rouge Wage公司开发继承自HP版本被C Builder 采用不能公开或修改可读性一般。SGI版本 由Silicon Graphics Computer SystemsInc公司开发继承自HP版 本。被GCC(Linux)采用可移植性好可公开、修改甚至贩卖从命名风格和编程风格上看阅读性非常高。如果你要深入学习STL想要阅读部分源代码可以主要参考这个版本。 3. STL的六大组件 二、string类的介绍 string类是STL中的容器也是我们学习STL的开始。 1. 为什么学习string类 C语言中的字符串 C语言中基本类型有char类型没有字符串类型字符串是以\0结尾的一些字符的集合为了操作方便C标准库中提供了一些str系列的库函数(如strlen, strcmp,strcat,strcpy 等)但是这些库函数与字符串是分离开的不太符合OOP(面向对象)的思想而且底层空间需要用户自己管理稍不留神可能还会越界访问。 因此为了更加高效的使用和管理字符串C提出了string类的概念关于string类的学习我们先学习string类的使用后面我们进一步深入时我们再去动手模拟实现string类在这里我们首先知道string类的底层其实就是char类型的顺序表就行了。 2. 标准库中的string类 string类的文档介绍 从文档中我们能看到 string类是basic_string模板类的一个实例它使用char来实例化basic_string模板类。string在底层实际是basic_string模板类的别名typedef basic_stringcharstring;不能操作多字节或者变长字符的序列。(如英文使用ASCII编码使用一个字节表示一个字母汉字如果采用GBK编码占用两个字节表示一个汉字)处理多字节或者变长字符的序列要用到以下三个类 在使用string类时必须包含 #includestring以及using namespace std; 三、string类的常用接口说明 在上面的string类的文档介绍中我们可以看到string类接近有140个不同的接口函数在这么多的成员函数中很多都是冗余不必要的因此下面我只讲解最常用的接口。 在讲解这些函数接口之前我们先来介绍nposnpos是string类里面的一个静态成员变量,因此用string类创建的所有对象访问的都是同一个npos,其次我们来看pos的定义 首先npos的值是被const修饰了其值不能更改然后size_t是无符号整形unsigned的typedef定义将-1的值赋值给了npos,但npos是无符号整形因此此时nops的值就是无符号整形的最大值65535。 1. string类对象的常见构造函数 缺省构造函数用空字符串初始化string对象拷贝构造函数用string类的对象另一个对象用string类的对象中的字符串的子串初始化一个对象,子串的起始位置是pos,结束位置是pos向后的len个字符,如果len没有给缺省值默认为npos即从pos位置后面有多少拿多少。用C风格的字符串初始化string类的对象用C风格的字符串前n个字符初始化string类的对象用n个字符初始化string类的对象 #includeiostream #includestring using namespace std; int main() {string s0(hello world!);string s1; //default (1) 缺省构造函数 //用空字符串初始化s1对象 string s2(s0); //copy (2) 拷贝构造函数 //用string类的对象s0初始化s2对象string s3(s0, 6, 5); //substring (3) 用string类的对象s0的字符串的子串world初始化s3 //这里pos参数表示位置len表示的是个数string s4(hello string); //from c-string (4) //C风格的字符串初始化string类的对象s4string s5(hello world,5); //from sequence(5) //用C风格的字符串前五个字符hello初始化string类的对象s5string s6(5, x); //fill (6) //用5个x初始化string类的对象s6cout s0 endl;cout s1 endl;cout s2 endl;cout s3 endl;cout s4 endl;cout s5 endl;cout s6 endl; }2. string类对象的容量操作 我们先来看看不涉及扩容的函数 #includeiostream #includestring using namespace std; int main() {string s1(hello world);cout s1.size() endl;cout s1.length() endl;cout s1.capacity() endl;cout s1.max_size() endl;s1.clear();cout s1.empty() endl;return 0; }我们再来看看涉及扩容的函数 std::string::reserve()函数与std::string::shrink_to_fit()函数 //std::string::reserve()函数 #includeiostream #includestring using namespace std; int main() {string s1(hello world);cout s1.size() endl;cout s1.capacity() endl;s1.reserve(200); //提前把空间扩容到200字节但不初始化//由于string类内部的一些原因如对齐等原因虽然向系统申请的的200字节//但不一定就是200字节总之系统申请的空间会 你给的指定值cout s1.size() endl;cout s1.capacity() endl;s1.shrink_to_fit(); //缩减容量让 size capacitycout s1.size() endl;cout s1.capacity() endl;return 0; }观察会发现经过reserve()函数的处理后size(字符个数)没有变但是capacity(容量)改变了。 经过shrink_to_fit()函数处理后size(字符个数)没有变但是capacity(容量)改变了。虽然shrink_to_fit()函数能够减少空间的浪费但是我们还是一般不缩容因为缩容的消耗是很大的 我们再来看另一个扩容有关的函数std::string::resize()函数 此函数有两个版本这两个版本构成函数重载。 第一个参数是:调整后容量的大小第二个参数是用什么字符来初始化新申请的空间中多余的没有被初始化部分空间如果不给此参数就默认用’\0’来初始化。 如果第一个参数给的没有原来的大那就是缩容,里面的字符串就变成了只保留原先字符串从0位置开始到n位置的字符串。 //std::string::resize()函数 #includeiostream #includestring using namespace std; int main() {string s1(hello world);cout s1.size() endl;cout s1.capacity() endl;s1.resize(30); //提前把空间与字符数量扩容到50字节但不初始化//由于string类内部的一些原因如对齐等原因虽然向系统申请的的50字节//但不一定就是50字节总之系统申请的空间会 你给的指定值cout s1.size() endl;cout s1.capacity() endl;return 0; }注意 size()与length()方法底层实现原理完全相同引入size()的原因是为了与其他容器的接口保持一致一般情况下基本都是用size()。clear()只是将string中有效字符清空不改变底层空间大小。resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个不同的是当字符个数增多时resize(n)用’\0’来填充多出的元素空间resize(size_t n, char c)用字符c来填充多出的元素空间。注意resize在改变元素个数时如果是将元素个数增多可能会改变底层容量的大小如果是将元素个数减少底层空间总大小不变。reserve(size_t n0)为string预留空间不改变有效元素个数当reserve的参数小于string的底层空间总大小时reserver不会改变容量大小。 3. string类对象的单个元素访问 //string 里面关于单个元素的访问 #includeiostream #includestring using namespace std; int main() {string s1(hello world!);char ch1 s1[1]; //operator[]的运算符重载char ch2 s1[2]; //operator[]的运算符重载char ch3 s1.at(6);cout ch1 endl;cout ch2 endl;cout ch3 endl;cout --------------- endl;cout s1.front() endl;cout s1.back() endl;cout --------------- endl;//捕获异常try{s1.at(100);}catch (const std::exception e){//显示异常原因cout e.what() endl;}return 0; }4. string类对象的元素的遍历 对于string类对象的遍历有以下四种方式 第一种operator运算符重载我们已经学会了我们可以利用如下代码进行遍历字符串 #includeiostream #includestring using namespace std; int main() {string s1(hello world);for (int i 0; i s1.size(); i){cout s1[i];}cout endl;return 0; }第二种begin end就需要借助STL里面的迭代器了iterator是迭代器类型是一种自定义类型现在你可以暂时把它理解为类似于指针的类型。 想要遍历字符串我们就应该这样遍历 #includeiostream #includestring using namespace std; int main() {string s1(hello world);string::iterator it s1.begin();while (it ! s1.end()){cout *it;it;}cout endl;return 0; }第三种是rbegin rend 它们其实也是迭代器不同的是它们是反向迭代器所以我们应该这样写才能遍历string对象。 #includeiostream #includestring using namespace std; int main() {string s1(hello world);string::reverse_iterator it s1.rbegin();while (it ! s1.rend()){cout *it;it;}cout endl;return 0; }第三种是范围for在前面的文章中我们也谈论过范围for,利用范围for,也可以遍历string类范围for的底层实现就是迭代器。 #includeiostream #includestring using namespace std; int main() {string s1(hello world);for (auto e : s1){cout e;}cout endl;return 0; }在前面已经简单的介绍了两种迭代器正向迭代器与反向迭代器下面我们就把另外两种迭代器进行简单介绍补充 const_iterator类型的迭代器 此类型的迭代器不能通过迭代器来改变指向的对象里的内容。 #includeiostream #includestring using namespace std; int main() {const string s1(hello world);string::const_iterator it s1.begin();while (it ! s1.end()){(*it); //此行为非法it; //此行为合法}return 0; }const_reverse_iterator类型的迭代器 此类型的迭代器是反向迭代器并且不能通过迭代器来改变指向的对象里的内容。 #includeiostream #includestring using namespace std; int main() {const string s1(hello world);string::const_reverse_iterator it s1.rbegin();while (it ! s1.rend()){(*it); //此行为非法it; //此行为合法}return 0; }5.string类对象的修改操作 #includeiostream #includestring using namespace std; int main() {string s0(hello );string s1(world);s0 s1;cout s0 endl;s0 string;cout s0 endl;s0 !;cout s0 endl;return 0; }#includeiostream #includestring using namespace std; int main() {string s0(hello );string s1(world);//substring (2)s0.append(s1, 0, 3);cout s0 endl;//buffer (4) s0.append(string, 3);cout s0 endl;//fill (5)s0.append(5, !);cout s0 endl;return 0; }#includeiostream #includestring using namespace std; int main() {string s0(hello );s0.push_back(!);cout s0 endl; }代码与append()函数代码类似不再演示 注意 在string尾部追加字符时s.push_back( c ) / s.append(1, c) / s c’三种的实现方式差不多一般 情况下string类的操作用的比较多操作不仅可以连接单个字符还可以连接字符串。对string操作时如果能够大概预估到放多少字符可以先通过reserve把空间预留好。 #includeiostream #includestring using namespace std; int main() {string s0(hello );string s1(world);//string (1) s0.insert(0, s1);cout s0 endl;//c-string (3) s0.insert(3, !!!!!!);cout s0 endl; }#includeiostream #includestring using namespace std; int main() {string s0(hello );s0.erase(2, 2);cout s0 endl; }#includeiostream #includestring using namespace std; int main() {string s0(hello );//c-string (3)s0.replace(2, 2,!!!!!);cout s0 endl; }#includeiostream #includestring using namespace std; int main() {string s0(hello );string s1(world);s0.swap(s1);cout s0 endl;cout s1 endl;return 0; }#includeiostream #includestring using namespace std; int main() {string s0(hello);s0.pop_back();cout s0 endl;return 0; }6.string类对象的一些其他操作 #includeiostream #includestring using namespace std; int main() {string s1(hello world);const char* str1 s1.c_str();cout str1 endl;return 0; }#includeiostream #includestring using namespace std; int main() {string s1(hello world);/*const char* str1 s1.c_str();cout str1 endl;*/char ch a;char* str1 ch;s1.copy(str1, 3, 2); // 注意此函数不会拷贝完后追加字符串打印字符串时要小心越界。return 0; }#includeiostream #includestring using namespace std; int main() {//buffer (3) string s1(hello world);const char* str world;int index s1.find(str, 0, 3);cout index endl;return 0; }#includeiostream #includestring using namespace std; int main() {//c-string (2) string s1(hello world);const char* str world;int index s1.rfind(str);cout index endl;return 0; }#includeiostream #includestring using namespace std; int main() {string s1(hello world);int index s1.find_first_of(wor);cout index endl;return 0; }#includeiostream #includestring using namespace std; int main() {string s1(hello world);string s2 s1.substr(2, 3);cout s2 endl;return 0; }#includeiostream #includestring using namespace std; int main() {//string (1) string s1(hello world);string s2(iello);cout s1.compare(s2) endl;return 0; }7.getline函数 getline的作用是读取一整行直到遇到换行符才停止读取期间能读取像空格、Tab等的空白符。 getline函数和cin一样也会返回它的流参数也就是cin所以可以用getline循环读取带空格的字符串。 注意 getline 是非成员函数 #includeiostream #includestring using namespace std; int main() {string s1;//这里我们输入hello world!getline(cin, s1);cout s1 endl;return 0; }四、结语 本章的内容都不是很难但是琐碎的函数与细节特别多想要掌握好它们还需要多加练习。多去使用它们相信你会越用越熟悉的。
http://www.dnsts.com.cn/news/87548.html

相关文章:

  • 蜗牛星际做网站服务器适合美工的设计网站
  • 网站托管做的好的公司高端品牌羽绒服
  • 谷歌关键词挖掘工具随州抖音seo收费标准
  • 泉州优化怎么做搜索职场seo是什么意思
  • 国外网站开发怎么开网店找货源
  • 手机html5 网站导航代码用什么软件开发手机app
  • 手机如何制作网站和网页网店营销与推广策划方案
  • 房地产建设网站的意义分类目录搜索引擎
  • 网站标头图片切换祥云户网站
  • 登封网站设计游戏公司网站模板
  • 手机网站注册页面wordpress轮播图修改
  • 网站seo源码网页制作相关的工具软件
  • 昆明网站制作方案华为云免费云服务器
  • 丝绸之路网站建设报告vuejs做视频网站
  • 虚拟主机可以做视频视频网站吗如何提高网站的安全性
  • 在线A视频网站(级做爰片)免费获取ppt模板的网站
  • 什么电脑做网站前段用电商平台怎么搭建
  • idc网站模板源码下载WordPress文章生成不是HTML
  • 拼多多电商网站建设公司和个人均不能备案论坛类网站
  • 公司网站维护流程花店网站推广方案
  • 临沧网站制作温州专业建站
  • 网站备案 通知番禺网站制作费用
  • 免费一键生成logo网站焦作市网站建设
  • 邢台移动网站设计wordpress地区分站
  • 在线购物商城网站建设如何建立网络销售平台
  • 入门做外贸是先建网站还是先参展驾校网站制作
  • 公主岭网站建设规划如何在网上做销售推广
  • 企业网站的制作及维护网站要做手机版怎么做
  • 金科网站建设前端开发工程师是干什么的
  • 聊城做网站的公司价位自媒体视频剪辑教学视频