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

渝北集团网站建设如何给网站做排名

渝北集团网站建设,如何给网站做排名,做钓鱼网站盗游戏号会被判刑吗,360关键词竞价网站C 大大增强了对字符串的支持#xff0c;除了可以使用C风格的字符串#xff0c;还可以使用内置的 string 类。string 类处理起字符串来会方便很多#xff0c;完全可以代替C语言中的字符数组或字符串指针。 string 是 C 中常用的一个类#xff0c;它非常重要#xff0c;我们…C 大大增强了对字符串的支持除了可以使用C风格的字符串还可以使用内置的 string 类。string 类处理起字符串来会方便很多完全可以代替C语言中的字符数组或字符串指针。 string 是 C 中常用的一个类它非常重要我们有必要在此单独讲解一下。 使用 string 类需要包含头文件string下面的例子介绍了几种定义 string 变量对象的方法 #include iostream#include stringusing namespace std;int main(){string s1;string s2 c plus plus;string s3 s2;string s4 (5, s);return 0;} 变量 s1 只是定义但没有初始化编译器会将默认值赋给 s1默认值是也即空字符串。 变量 s2 在定义的同时被初始化为c plus plus。与C风格的字符串不同string 的结尾没有结束标志\0。 变量 s3 在定义的时候直接用 s2 进行初始化因此 s3 的内容也是c plus plus。 变量 s4 被初始化为由 5 个s字符组成的字符串也就是sssss。从上面的代码可以看出string 变量可以直接通过赋值操作符进行赋值。 string 变量也可以用C风格的字符串进行赋值例如s2 是用一个字符串常量进行初始化的而 s3 则是通过 s2 变量进行初始化的。与C风格的字符串不同当我们需要知道字符串长度时可以调用 string 类提供的 length() 函数。如下所示 string s http://c.biancheng.net;int len s.length();coutlenendl; 输出结果为22。由于 string 的末尾没有\0字符所以 length() 返回的是字符串的真实长度而不是长度 1。 转换为C风格的字符串 虽然 C 提供了 string 类来替代C语言中的字符串但是在实际编程中有时候必须要使用C风格的字符串例如打开文件时的路径为此string 类为我们提供了一个转换函数 c_str()该函数能够将 string 字符串转换为C风格的字符串并返回该字符串的 const 指针const char*。请看下面的代码 string path D:\\demo.txt;FILE *fp fopen(path.c_str(), rt); 为了使用C语言中的 fopen() 函数打开文件必须将 string 字符串转换为C风格的字符串。 string 字符串的输入输出 string 类重载了输入输出运算符可以像对待普通变量那样对待 string 变量也就是用进行输入用进行输出。请看下面的代码 #include iostream#include stringusing namespace std;int main(){string s;cins; //输入字符串coutsendl; //输出字符串return 0;} 运行结果http://c.biancheng.net  http://vip.biancheng.net↙http://c.biancheng.net 虽然我们输入了两个由空格隔开的网址但是只输出了一个这是因为输入运算符默认会忽略空格遇到空格就认为输入结束所以最后输入的http://vip.biancheng.net没有被存储到变量 s。 访问字符串中的字符 string 字符串也可以像C风格的字符串一样按照下标来访问其中的每一个字符。string 字符串的起始下标仍是从 0 开始。请看下面的代码 #include iostream#include stringusing namespace std;int main(){string s 1234567890;for(int i0,lens.length(); ilen; i){couts[i] ;}coutendl;s[5] 5;coutsendl;return 0;} 运行结果1 2 3 4 5 6 7 8 9 01234557890 本例定义了一个 string 变量 s并赋值 1234567890之后用 for 循环遍历输出每一个字符。借助下标除了能够访问每个字符也可以修改每个字符s[5] 5;就将第6个字符修改为 5所以 s 最后为 1234557890。 字符串的拼接 有了 string 类我们可以使用或运算符来直接拼接字符串非常方便再也不需要使用C语言中的 strcat()、strcpy()、malloc() 等函数来拼接字符串了再也不用担心空间不够会溢出了。 用来拼接字符串时运算符的两边可以都是 string 字符串也可以是一个 string 字符串和一个C风格的字符串还可以是一个 string 字符串和一个字符数组或者是一个 string 字符串和一个单独的字符。请看下面的例子 #include iostream#include stringusing namespace std;int main(){string s1 first ;string s2 second ;char *s3 third ;char s4[] fourth ;char ch ;string s5 s1 s2;string s6 s1 s3;string s7 s1 s4;string s8 s1 ch;couts5endls6endls7endls8endl;return 0;} 运行结果 first second first third first fourth first string 字符串的增删改查 C 提供的 string 类包含了若干实用的成员函数大大方便了字符串的增加、删除、更改、查询等操作。 一. 插入字符串 insert() 函数可以在 string 字符串中指定的位置插入另一个字符串它的一种原型为 string insert (size_t pos, const string str); pos 表示要插入的位置也就是下标str 表示要插入的字符串它可以是 string 字符串也可以是C风格的字符串。请看下面的代码 #include iostream#include stringusing namespace std;int main(){string s1, s2, s3;s1 s2 1234567890;s3 aaa;s1.insert(5, s3);cout s1 endl;s2.insert(5, bbb);cout s2 endl;return 0;} 运行结果12345aaa6789012345bbb67890 insert() 函数的第一个参数有越界的可能如果越界则会产生运行时异常我们将会在《C异常Exception》一章中详细讲解如何捕获这个异常。 更多 insert() 函数的原型和用法请参考http://www.cplusplus.com/reference/string/string/insert/ 二. 删除字符串 erase() 函数可以删除 string 中的一个子字符串。它的一种原型为 string erase (size_t pos 0, size_t len npos); pos 表示要删除的子字符串的起始下标len 表示要删除子字符串的长度。如果不指明 len 的话那么直接删除从 pos 到字符串结束处的所有字符此时 len str.length - pos。 请看下面的代码 #include iostream#include stringusing namespace std;int main(){string s1, s2, s3;s1 s2 s3 1234567890;s2.erase(5);s3.erase(5, 3);cout s1 endl;cout s2 endl;cout s3 endl;return 0;} 运行结果 1234567890 12345 1234590 三. 提取子字符串 substr() 函数用于从 string 字符串中提取子字符串它的原型为 string substr (size_t pos 0, size_t len npos) const; pos 为要提取的子字符串的起始下标len 为要提取的子字符串的长度。 请看下面的代码 #include iostream#include stringusing namespace std;int main(){string s1 first second third;string s2;s2 s1.substr(6, 6);cout s1 endl;cout s2 endl;return 0;} 运行结果 first second third second 系统对 substr() 参数的处理和 erase() 类似 如果 pos 越界会抛出异常 如果 len 越界会提取从 pos 到字符串结尾处的所有字符。 四. 字符串查找 string 类提供了几个与字符串查找有关的函数如下所示。 1) find() 函数 find() 函数用于在 string 字符串中查找子字符串出现的位置它其中的两种原型为 size_t find (const string str, size_t pos 0) const; size_t find (const char* s, size_t pos 0) const; 第一个参数为待查找的子字符串它可以是 string 字符串也可以是C风格的字符串。第二个参数为开始查找的位置下标如果不指明则从第0个字符开始查找。请看下面的代码 #include iostream#include stringusing namespace std;int main(){string s1 first second third;string s2 second;int index s1.find(s2,5);if(index s1.length())coutFound at index : index endl;elsecoutNot foundendl;return 0;} find() 函数最终返回的是子字符串第一次出现在字符串中的起始下标。本例最终是在下标 6 处找到了 s2 字符串。如果没有查找到子字符串那么会返回 string::npos它是 string 类内部定义的一个静态常成员用来表示 size_t 类型所能存储的最大值。 2) rfind() 函数 rfind() 和 find() 很类似同样是在字符串中查找子字符串不同的是 find() 函数从第二个参数开始往后查找而 rfind() 函数则最多查找到第二个参数处如果到了第二个参数所指定的下标还没有找到子字符串则返回 string::npos。 请看下面的例子 #include iostream#include stringusing namespace std;int main(){string s1 first second third;string s2 second;int index s1.rfind(s2,6);if(index s1.length())coutFound at index : index endl;elsecoutNot foundendl;return 0;} 3) find_first_of() 函数 find_first_of() 函数用于查找子字符串和字符串共同具有的字符在字符串中首次出现的位置。请看下面的代码 #include iostream#include stringusing namespace std;int main(){string s1 first second second third;string s2 asecond;int index s1.find_first_of(s2);if(index s1.length())coutFound at index : index endl;elsecoutNot foundendl;return 0;} 本例中 s1 和 s2 共同具有的字符是‘s’该字符在 s1 中首次出现的下标是3故查找结果返回3。
http://www.hkea.cn/news/14318318/

相关文章:

  • 传智播客php网站开发实例教程深圳网深圳网站开发公司
  • c2c网站有哪些?免费注册com域名
  • 莱芜网站优化加徽信xiala5效果好加强网站技术建设
  • 最权威的网站推广公司山东正元建设网站
  • 个人做网站的流程新版wordpress文章编辑界面
  • 网站怎么注册怎样做金融理财网站
  • phpcms建站流程怀化网站优化推荐
  • 杭州网站建设规划外贸单在哪些网站做
  • 上海市安全建设监理协会网站小城镇建设期刊网站
  • 普陀网站开发培训学校临沂市建设工程监理公司网站
  • 公司网站建设论文自己设计图片的软件
  • 网站建设实验报告手写增加网站关键词
  • 网站如何接广告赚钱厦门seo排名外包
  • 网页设计及网站建设的相关概念成都企业网站备案流程
  • 金华金东区建设局网站网站建设 设计
  • 百度站长工具seoseo优化实训总结
  • 不会编码可以做网站优化吗网站关键词百度指数
  • 黄冈便宜的网站推广怎么做网站点击量设计
  • 网站有免费的域名和空间么做美容仪器的网站
  • 建筑行业招聘网站推荐一级a做爰片_相关网站
  • 做网站网站加载内容慢怎么解决查找做影评的工作网站
  • 辽宁省建设工程信息网官网新网站入口建站之家官网
  • 注册网站公司企业官网的意义
  • 鹤岗住房和城乡建设局网站wordpress 图集功能
  • 网站建设与实现毕业答辩ppt甘肃机械化建设工程有限公司网站
  • 服饰 视频 网站建设装修公司手机网站模板
  • 浙江省建设职业技术学院网站好建设网站
  • 做基因表达热图的网站wordpress 视频显示
  • 做谷歌推广一定要网站吗广安专业网站建设报价
  • 网站的花费wordpress用不了了