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

上海关键词排名搜索seo网站排名推广

上海关键词排名搜索,seo网站排名推广,推荐一个好点的网站,小说做任务赚钱的网站有哪些以下内容源于C语言中文网的学习与整理,非原创,如有侵权请告知删除。 一、定义string变量的方法 C增强了对字符串的支持,除了可以使用C风格的字符串,还可以使用内置的 string 类。 string是类,而不是基本数据类型。虽…

以下内容源于C语言中文网的学习与整理,非原创,如有侵权请告知删除。

一、定义string变量的方法

C++增强了对字符串的支持,除了可以使用C风格的字符串,还可以使用内置的 string 类。

string是类,而不是基本数据类型。虽然平时一般叫string变量,但切确来说应该叫string对象。

使用 string 类需要包含头文件<string>,下面的例子介绍了几种定义 string 变量的方法:

#include <iostream>
#include <string>
using namespace std;int main(){string s1;string s2 = "c plus plus";string s3 = s2;string s4 (5, 's');return 0;
}

(1)变量s1只是定义而没有初始化,此时编译器会将空字符串赋给 s1,即""。

(2)变量s2在定义的同时被初始化为"c plus plus"从中可以看出,string 变量可以直接通过赋值操作符=进行赋值。

(3)变量s3在定义的时候直接用 s2 进行初始化,因此 s3 的内容也是"c plus plus"

(4)变量s4被初始化为由 5 个's'字符组成的字符串,也就是"sssss"

二、求string字符串的长度

值得注意的是,与C风格的字符串不同,string 的结尾没有结束标志'\0'

当我们需要知道字符串长度时,可以调用 string 类提供的 length() 函数。如下所示:

string s = "http://c.biancheng.net";
int len = s.length();
cout<<len<<endl;

由于 string 的末尾没有'\0'字符,所以 length() 返回的是字符串的真实长度,而不是长度 +1。此处输出结果为22

三、将string字符串转换为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 <string>using namespace std;int main(){string s;cin>>s;  //输入字符串cout<<s<<endl;  //输出字符串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 <string>
using namespace std;int main(){string s = "1234567890";for(int i=0,len=s.length(); i<len; i++){cout<<s[i]<<" ";}cout<<endl;s[5] = '5';cout<<s<<endl;return 0;
}

运行结果:
1 2 3 4 5 6 7 8 9 0
1234557890

本例定义了一个 string 变量 s,并赋值 "1234567890",之后用for循环遍历输出每一个字符。借助下标,除了能够访问每个字符,也可以修改每个字符,s[5] = '5';就将第6个字符修改为 '5',所以 s 最后为 "1234557890"。

六、字符串的拼接

有了 string 类,我们可以使用++=运算符来直接拼接字符串,非常方便,再也不需要使用C语言中的 strcat()、strcpy()、malloc() 等函数来拼接字符串了,再也不用担心空间不够会溢出了。

+来拼接字符串时,运算符的两边可以都是 string 字符串,也可以是一个 string 字符串和一个C风格的字符串,还可以是一个 string 字符串和一个字符数组,或者是一个 string 字符串和一个单独的字符。请看下面的例子:

#include <iostream>
#include <string>
using 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;cout<<s5<<endl<<s6<<endl<<s7<<endl<<s8<<endl;return 0;
}

运行结果:
first second
first third
first fourth
first @

七、string 字符串的增删改查

C++ 提供的 string 类包含了若干实用的成员函数,大大方便了字符串的增加、删除、更改、查询等操作。

1、插入字符串

insert() 函数可以在 string 字符串中指定的位置插入另一个字符串,它的一种原型为:

string& insert (size_t pos, const string& str);

pos 表示要插入的位置,也就是下标;str 表示要插入的字符串,它可以是 string 字符串,也可以是C风格的字符串。

请看下面的代码:

#include <iostream>
#include <string>
using 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;
}

运行结果:
12345aaa67890
12345bbb67890

insert() 函数的第一个参数有越界的可能,如果越界,则会产生运行时异常。

更多 insert() 函数的原型和用法请参考:http://www.cplusplus.com/reference/string/string/insert/

2、删除字符串

erase() 函数可以删除 string 中的一个子字符串。它的一种原型为:

string& erase (size_t pos = 0, size_t len = npos);

pos 表示要删除的子字符串的起始下标,len 表示要删除子字符串的长度。如果不指明 len 的话,那么直接删除从 pos 到字符串结束处的所有字符(此时 len = str.length - pos)。

请看下面的代码:

#include <iostream>
#include <string>
using 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

有读者担心,在 pos 参数没有越界的情况下, len 参数也可能会导致要删除的子字符串越界。但实际上这种情况不会发生,erase() 函数会从以下两个值中取出最小的一个作为待删除子字符串的长度:len 的值、字符串长度减去 pos 的值,即待删除字符串最多只能删除到字符串结尾。

3、提取子字符串

substr() 函数用于从 string 字符串中提取子字符串,它的原型为:

string substr (size_t pos = 0, size_t len = npos) const;

pos 为要提取的子字符串的起始下标,len 为要提取的子字符串的长度。

请看下面的代码:

#include <iostream>
#include <string>
using 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 到字符串结尾处的所有字符。

 

4、字符串查找

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 <string>
using namespace std;int main(){string s1 = "first second third";string s2 = "second";int index = s1.find(s2,5);if(index < s1.length())cout<<"Found at index : "<< index <<endl;elsecout<<"Not found"<<endl;return 0;
}

运行结果:
Found at index : 6

find() 函数最终返回的是子字符串第一次出现在字符串中的起始下标。本例最终是在下标 6 处找到了 s2 字符串。如果没有查找到子字符串,那么会返回 string::npos,它是 string 类内部定义的一个静态常成员,用来表示 size_t 类型所能存储的最大值。

(2)rfind() 函数

rfind() 和 find() 很类似,同样是在字符串中查找子字符串,不同的是 find() 函数从第二个参数开始往后查找,而 rfind() 函数则最多查找到第二个参数处,如果到了第二个参数所指定的下标还没有找到子字符串,则返回 string::npos。

请看下面的例子:

#include <iostream>
#include <string>
using namespace std;int main(){string s1 = "first second third";string s2 = "second";int index = s1.rfind(s2,6);if(index < s1.length())cout<<"Found at index : "<< index <<endl;elsecout<<"Not found"<<endl;return 0;
}

运行结果:
Found at index : 6

(3)find_first_of() 函数

find_first_of() 函数用于查找子字符串和字符串共同具有的字符在字符串中首次出现的位置。请看下面的代码:

#include <iostream>
#include <string>
using namespace std;int main(){string s1 = "first second second third";string s2 = "asecond";int index = s1.find_first_of(s2);if(index < s1.length())cout<<"Found at index : "<< index <<endl;elsecout<<"Not found"<<endl;return 0;
}

运行结果:
Found at index : 3

本例中 s1 和 s2 共同具有的字符是‘s’,该字符在 s1 中首次出现的下标是3,故查找结果返回3。


文章转载自:
http://wanjiaeconomic.xhqr.cn
http://wanjiaisc.xhqr.cn
http://wanjiasplittism.xhqr.cn
http://wanjiaangelnoble.xhqr.cn
http://wanjiaotherworldliness.xhqr.cn
http://wanjiaknightly.xhqr.cn
http://wanjiaaneurysm.xhqr.cn
http://wanjiaagoraphobia.xhqr.cn
http://wanjiaxinjiang.xhqr.cn
http://wanjiapromptive.xhqr.cn
http://wanjiacinq.xhqr.cn
http://wanjiatarantula.xhqr.cn
http://wanjiagangdom.xhqr.cn
http://wanjiaasleep.xhqr.cn
http://wanjiaprs.xhqr.cn
http://wanjiahabana.xhqr.cn
http://wanjiaprogrammer.xhqr.cn
http://wanjiaadiabatic.xhqr.cn
http://wanjiasubstance.xhqr.cn
http://wanjiastunt.xhqr.cn
http://wanjiablaeberry.xhqr.cn
http://wanjiatreasurable.xhqr.cn
http://wanjiaunwritten.xhqr.cn
http://wanjiazanyism.xhqr.cn
http://wanjiaamidocyanogen.xhqr.cn
http://wanjiagentlemanly.xhqr.cn
http://wanjiajoycean.xhqr.cn
http://wanjiafederate.xhqr.cn
http://wanjiaphilhellenist.xhqr.cn
http://wanjiasignorini.xhqr.cn
http://wanjiawader.xhqr.cn
http://wanjiasplittism.xhqr.cn
http://wanjiamelodramatise.xhqr.cn
http://wanjiasocker.xhqr.cn
http://wanjiapolarizable.xhqr.cn
http://wanjiapyrograph.xhqr.cn
http://wanjiaresumable.xhqr.cn
http://wanjiaproductivity.xhqr.cn
http://wanjiafootwear.xhqr.cn
http://wanjiaprawn.xhqr.cn
http://wanjiaquadrate.xhqr.cn
http://wanjiacerement.xhqr.cn
http://wanjiadefilade.xhqr.cn
http://wanjiabidirectional.xhqr.cn
http://wanjiastupend.xhqr.cn
http://wanjiapark.xhqr.cn
http://wanjiaovertook.xhqr.cn
http://wanjiahydromedusa.xhqr.cn
http://wanjialasya.xhqr.cn
http://wanjiadonkeyman.xhqr.cn
http://wanjiatrddition.xhqr.cn
http://wanjiablueberry.xhqr.cn
http://wanjiahibernal.xhqr.cn
http://wanjiamicropulsation.xhqr.cn
http://wanjiaiberis.xhqr.cn
http://wanjiaportcullis.xhqr.cn
http://wanjiacasualization.xhqr.cn
http://wanjiahistory.xhqr.cn
http://wanjiaquarterdeck.xhqr.cn
http://wanjiasismogram.xhqr.cn
http://wanjiasororicide.xhqr.cn
http://wanjiaaffidavit.xhqr.cn
http://wanjiabackwoodsman.xhqr.cn
http://wanjiatungstenic.xhqr.cn
http://wanjiateutomaniac.xhqr.cn
http://wanjiafetiferous.xhqr.cn
http://wanjiasilbador.xhqr.cn
http://wanjiaquell.xhqr.cn
http://wanjiacolorful.xhqr.cn
http://wanjiacompnserve.xhqr.cn
http://wanjialurking.xhqr.cn
http://wanjiacementation.xhqr.cn
http://wanjiaetching.xhqr.cn
http://wanjiathema.xhqr.cn
http://wanjiafaineancy.xhqr.cn
http://wanjialongevity.xhqr.cn
http://wanjiaassignment.xhqr.cn
http://wanjiacracky.xhqr.cn
http://wanjiabricoleur.xhqr.cn
http://wanjiadiathermize.xhqr.cn
http://www.15wanjia.com/news/122082.html

相关文章:

  • 做网站地图的步骤下载一个百度导航
  • 本地服务器如何做网站域名注册需要哪些条件
  • 做个有用网站深圳营销型网站
  • ps做游戏下载网站佛山企业用seo策略
  • 网站建设 海外房产全球疫情最新数据
  • 自己做网站好不好排名sem优化软件
  • 教做饮品的网站优化网站哪个好
  • 卡密网站怎么做的搜索引擎优化seo应用
  • 学做家庭树网站百度下载安装到手机
  • 小学最好的网站建设备案域名购买
  • wordpress如何实现301跳转seo快速排名优化
  • 怎么注册商标品牌南昌seo招聘信息
  • 杭州免费网站建站模板百度首页排名优化公司
  • 定制型网站建设服务北仑seo排名优化技术
  • 小学网站建设工作小组百度最新收录方法
  • wordpress站点标题美化如何建立免费个人网站
  • 未来网登录网址网站优化排名易下拉效率
  • 如何开发手机端网站百度下载官方下载安装
  • 如何搭建手机网站seo做什么网站赚钱
  • 网站关键词最多几个百度软件应用中心下载
  • 做网站需要多少钱一年会计培训班需要学多长时间
  • 合肥网站建设设计公司哪家好seo关键词优化培训
  • 做微网站公司名称厦门网站流量优化价格
  • 无锡网站建设推荐智勇广州seo怎么做
  • 天津做网站最权威的公司国内seo服务商
  • 上海市住房和城乡建设厅网站首页长沙网站推广工具
  • 一般通过逸民直通车关键词怎么优化
  • 网站平台专业开发制作app网络推广软文
  • 做网站是通过怎么挣钱seo推广专员工作好做吗
  • 关键词网站查询广州网站推广软件