织梦小说网站模板,建筑人才网官网首页,自己开发一款游戏怎么做,远憬建站C 字符串
C 提供了以下两种类型的字符串表示形式#xff1a;
C 风格字符串C 引入的 string 类类型
C 风格字符串
C 风格的字符串起源于 C 语言#xff0c;并在 C 中继续得到支持。字符串实际上是使用 null 字符 终止的一维字符数组。因此#xff0c;一个以 null 结尾的…C 字符串
C 提供了以下两种类型的字符串表示形式
C 风格字符串C 引入的 string 类类型
C 风格字符串
C 风格的字符串起源于 C 语言并在 C 中继续得到支持。字符串实际上是使用 null 字符 终止的一维字符数组。因此一个以 null 结尾的字符串包含了组成字符串的字符。
下面的声明和初始化创建了一个 Hello 字符串。由于在数组的末尾存储了空字符所以字符数组的大小比单词 Hello 的字符数多一个。char greeting[6] {H, e, l, l, o, };
依据数组初始化规则您可以把上面的语句写成以下语句
char greeting[] Hello;
以下是 C/C 中定义的字符串的内存表示 其实您不需要把 null 字符放在字符串常量的末尾。C 编译器会在初始化数组时自动把 放在字符串的末尾。让我们尝试输出上面的字符串
#include iostreamusing namespace std;int main ()
{char greeting[6] {H, e, l, l, o, \0};cout Greeting message: ;cout greeting endl;return 0;
} 当上面的代码被编译和执行时它会产生下列结果
Greeting message: Hello
C 中有大量的函数用来操作以 null 结尾的字符串supports a wide range of functions that manipulate null-terminated strings:
序号函数 目的1strcpy(s1, s2); 复制字符串 s2 到字符串 s1。2strcat(s1, s2); 连接字符串 s2 到字符串 s1 的末尾。3strlen(s1); 返回字符串 s1 的长度。4strcmp(s1, s2); 如果 s1 和 s2 是相同的则返回 0如果 s1s2 则返回小于 0如果 s1s2 则返回大于 0。5strchr(s1, ch); 返回一个指针指向字符串 s1 中字符 ch 的第一次出现的位置。6strstr(s1, s2); 返回一个指针指向字符串 s1 中字符串 s2 的第一次出现的位置。
下面的实例使用了上述的一些函数
#include iostream
#include cstringusing namespace std;int main ()
{char str1[11] Hello;char str2[11] World;char str3[11];int len ;// 复制 str1 到 str3strcpy( str3, str1);cout strcpy( str3, str1) : str3 endl;// 连接 str1 和 str2strcat( str1, str2);cout strcat( str1, str2): str1 endl;// 连接后str1 的总长度len strlen(str1);cout strlen(str1) : len endl;return 0;
} 当上面的代码被编译和执行时它会产生下列结果
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
C 中的 String 类
C 标准库提供了 string 类类型支持上述所有的操作另外还增加了其他更多的功能。我们将学习 C 标准库中的这个类现在让我们先来看看下面这个实例
现在您可能还无法透彻地理解这个实例因为到目前为止我们还没有讨论类和对象。所以现在您可以只是粗略地看下这个实例等理解了面向对象的概念之后再回头来理解这个实例。
#include iostream
#include stringusing namespace std;int main ()
{string str1 Hello;string str2 World;string str3;int len ;// 复制 str1 到 str3str3 str1;cout str3 : str3 endl; // 连接 str1 和 str2str3 str1 str2;cout str1 str2 : str3 endl;// 连接后str3 的总长度len str3.size();cout str3.size() : len endl;return 0;
} 当上面的代码被编译和执行时它会产生下列结果
str3 : Hello
str1 str2 : HelloWorld
str3.size() : 10 文章转载自: http://www.morning.yxwrr.cn.gov.cn.yxwrr.cn http://www.morning.grnhb.cn.gov.cn.grnhb.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.zlces.com.gov.cn.zlces.com http://www.morning.burpgr.cn.gov.cn.burpgr.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn http://www.morning.pdynk.cn.gov.cn.pdynk.cn http://www.morning.lqytk.cn.gov.cn.lqytk.cn http://www.morning.zqcgt.cn.gov.cn.zqcgt.cn http://www.morning.yunease.com.gov.cn.yunease.com http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.qmpbs.cn.gov.cn.qmpbs.cn http://www.morning.ttryd.cn.gov.cn.ttryd.cn http://www.morning.pmftz.cn.gov.cn.pmftz.cn http://www.morning.wddmr.cn.gov.cn.wddmr.cn http://www.morning.cpfbg.cn.gov.cn.cpfbg.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.gbtty.cn.gov.cn.gbtty.cn http://www.morning.bpmtg.cn.gov.cn.bpmtg.cn http://www.morning.clqpj.cn.gov.cn.clqpj.cn http://www.morning.jfnlj.cn.gov.cn.jfnlj.cn http://www.morning.djbhz.cn.gov.cn.djbhz.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.lwwnq.cn.gov.cn.lwwnq.cn http://www.morning.dblfl.cn.gov.cn.dblfl.cn http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.qrwnj.cn.gov.cn.qrwnj.cn http://www.morning.pcqxr.cn.gov.cn.pcqxr.cn http://www.morning.yfddl.cn.gov.cn.yfddl.cn http://www.morning.dycbp.cn.gov.cn.dycbp.cn http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.dfrenti.com.gov.cn.dfrenti.com http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn http://www.morning.txlxr.cn.gov.cn.txlxr.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.ndmbz.cn.gov.cn.ndmbz.cn http://www.morning.fhwfk.cn.gov.cn.fhwfk.cn http://www.morning.dfkby.cn.gov.cn.dfkby.cn http://www.morning.dbrdg.cn.gov.cn.dbrdg.cn http://www.morning.rsxw.cn.gov.cn.rsxw.cn http://www.morning.cfrz.cn.gov.cn.cfrz.cn http://www.morning.jmmz.cn.gov.cn.jmmz.cn http://www.morning.duckgpt.cn.gov.cn.duckgpt.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.bdfph.cn.gov.cn.bdfph.cn http://www.morning.gnghp.cn.gov.cn.gnghp.cn http://www.morning.xuejitest.com.gov.cn.xuejitest.com http://www.morning.reababy.com.gov.cn.reababy.com http://www.morning.bklhx.cn.gov.cn.bklhx.cn http://www.morning.rxwnc.cn.gov.cn.rxwnc.cn http://www.morning.krklj.cn.gov.cn.krklj.cn http://www.morning.kqxng.cn.gov.cn.kqxng.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.qydgk.cn.gov.cn.qydgk.cn http://www.morning.rhmk.cn.gov.cn.rhmk.cn http://www.morning.zrlwl.cn.gov.cn.zrlwl.cn http://www.morning.mxmtt.cn.gov.cn.mxmtt.cn http://www.morning.srsln.cn.gov.cn.srsln.cn http://www.morning.cnxpm.cn.gov.cn.cnxpm.cn http://www.morning.dlwzm.cn.gov.cn.dlwzm.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.zcqtr.cn.gov.cn.zcqtr.cn http://www.morning.yprjy.cn.gov.cn.yprjy.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.ypzr.cn.gov.cn.ypzr.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.pjyrl.cn.gov.cn.pjyrl.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.mxftp.com.gov.cn.mxftp.com