中国三安建设网站,安康鼎盛网站建设,用户登录界面设计,沈阳建站费用前言
本篇文章记录的是一些关于string的补充说明
string与const char*之间的相互转换
const char* 转换成string
在C中存在着从const char到string的隐式类型转换#xff0c;换句话说#xff0c;如果一个函数的参数类型是string类#xff0c;直接传入const char类型的参…前言
本篇文章记录的是一些关于string的补充说明
string与const char*之间的相互转换
const char* 转换成string
在C中存在着从const char到string的隐式类型转换换句话说如果一个函数的参数类型是string类直接传入const char类型的参数是没问题的
const char* cstr demo;
string str(cstr);string与const char*的转换
在传入参数时i有时我们传入string在编译时是会报错的所以我们就需要传入const char* 类型
我们可以使用成员函数c_str()来返回string对应的char数组
string str demo;
const char* cstr str.c_str();并且在进行文件读写的时候使用const char* 比使用string更安全 因为string是会自动分配内存的其内部的存储方式是不可见的 而const char*类型是整存整取的或者也可以自己手动存入每一部分
注意
为了修改string字符串的内容下标访问操作符[]和函数at都会返回字符串中某个字符的引用但当字符串的内存被重新分配之后可能会发生错误
此处给出一个例子string内部的内存分配机制方便理解 字符串str中有十个元素当我想往里面复制11个元素时先复制了10个元素在放第十一个元素的时候原字符串不够放那就会在堆区的另一个地方找一个更大的位置比如找了一个能存放20个元素的位置 首先会之前存储的10个元素拷贝到新的空间里之后再将第11个元素存储到后面最后将之前存储10个元素的空间释放掉
那么当我想通过之前的引用找到原来对应的字符时就会发生未知的错误
大小写转换
在C语言中我们只能通过循环来改变每个字符利用ASCII码值或者toupper/tolower函数将其转换成大写或小写
而使用STL的话一行就能解决这个问题 在STL算法库中已经将这种遍历修改的过程进行了封装 我们可以直接复用
transform(str.begin, str.end(), str.begin(), toupper);
//字符串转大写transform(str.begin(), str.end(), str.begin(), tolower);
//字符串转小写这里面的算法就是transform算法包含四个参数 前两个参数是源容器的首未迭代器 第三个参数是目标容器的首迭代器 第四个参数是函数指针也就是要执行的操作
之后会有对于迭代器的解释和学习在这里会用就行
字符串和数字之间的转换
实数转字符串
在C11新标准中提供了全局函数std::to_string 可以将类型转换成string类型
下面给出一些例子 将带符号 / 不带符号整数和实数转换成字符串
string to_string(int val);string to_string(long val);string to_string(long long val);string to_string(unsigned int val);string to_string(unsigned long val);string to_string(unsigned long long val);string to_string(float val);string to_string(double val);string to_string(long double val);字符串转实数
int stoi(const string str, size_t* idx 0; int base 10);long stol(const string str, size_t* idx 0, int base 10);long long stoll(const string str, size_t* idx 0, int base 10);补充说明
1.
idx返回字符串中第一个非数字的位置即数值部分的结束位置
2.
base为进制10就是10进制8就是8进制
3.
该组函数会自动保留符号和自动去除前导0
C风格字符数组转换成实数
在C语言中也提供了一些函数来帮助我们将字符数组转换成实数
int atoi(const char* str);long atol(const char* str);long longstoll(const char* str);double atof(const char* str);注意 atof函数中的f指的是浮点数包含float、double、long double类型atof函数的返回类型是double
结语
关于string的补充说明到这里就结束了 之后我会出一些刷题的笔记以及C相关知识的学习和介绍
我们下篇文章见~ 文章转载自: http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.plznfnh.cn.gov.cn.plznfnh.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.qcwck.cn.gov.cn.qcwck.cn http://www.morning.bsqth.cn.gov.cn.bsqth.cn http://www.morning.bqppr.cn.gov.cn.bqppr.cn http://www.morning.tfgkq.cn.gov.cn.tfgkq.cn http://www.morning.kzbpx.cn.gov.cn.kzbpx.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.ncfky.cn.gov.cn.ncfky.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.ltspm.cn.gov.cn.ltspm.cn http://www.morning.thbkc.cn.gov.cn.thbkc.cn http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn http://www.morning.sfwcx.cn.gov.cn.sfwcx.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.rgpsq.cn.gov.cn.rgpsq.cn http://www.morning.rsmtx.cn.gov.cn.rsmtx.cn http://www.morning.sxbgc.cn.gov.cn.sxbgc.cn http://www.morning.tkchg.cn.gov.cn.tkchg.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.syynx.cn.gov.cn.syynx.cn http://www.morning.qjxxc.cn.gov.cn.qjxxc.cn http://www.morning.lmknf.cn.gov.cn.lmknf.cn http://www.morning.rfwgg.cn.gov.cn.rfwgg.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.yfrlk.cn.gov.cn.yfrlk.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.ydrml.cn.gov.cn.ydrml.cn http://www.morning.ybshj.cn.gov.cn.ybshj.cn http://www.morning.syznh.cn.gov.cn.syznh.cn http://www.morning.btypn.cn.gov.cn.btypn.cn http://www.morning.lqffg.cn.gov.cn.lqffg.cn http://www.morning.zymgs.cn.gov.cn.zymgs.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.dangaw.com.gov.cn.dangaw.com http://www.morning.ztrht.cn.gov.cn.ztrht.cn http://www.morning.mdrnn.cn.gov.cn.mdrnn.cn http://www.morning.jyknk.cn.gov.cn.jyknk.cn http://www.morning.cwknc.cn.gov.cn.cwknc.cn http://www.morning.bxrlt.cn.gov.cn.bxrlt.cn http://www.morning.youngbase.cn.gov.cn.youngbase.cn http://www.morning.kybyf.cn.gov.cn.kybyf.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.lthtp.cn.gov.cn.lthtp.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.drcnn.cn.gov.cn.drcnn.cn http://www.morning.ftnhr.cn.gov.cn.ftnhr.cn http://www.morning.sfsjh.cn.gov.cn.sfsjh.cn http://www.morning.zlhcw.cn.gov.cn.zlhcw.cn http://www.morning.kpcky.cn.gov.cn.kpcky.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.ydrml.cn.gov.cn.ydrml.cn http://www.morning.kpcdc.cn.gov.cn.kpcdc.cn http://www.morning.wphzr.cn.gov.cn.wphzr.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.c7627.cn.gov.cn.c7627.cn http://www.morning.btsls.cn.gov.cn.btsls.cn http://www.morning.qtkfp.cn.gov.cn.qtkfp.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.kxqmh.cn.gov.cn.kxqmh.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.hmktd.cn.gov.cn.hmktd.cn http://www.morning.gwmny.cn.gov.cn.gwmny.cn http://www.morning.wmqxt.cn.gov.cn.wmqxt.cn http://www.morning.bmnm.cn.gov.cn.bmnm.cn http://www.morning.dcmnl.cn.gov.cn.dcmnl.cn http://www.morning.wkknm.cn.gov.cn.wkknm.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.rzmlc.cn.gov.cn.rzmlc.cn http://www.morning.rhph.cn.gov.cn.rhph.cn