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

雪白丰腴做美妇网站百度seo搜搜

雪白丰腴做美妇网站,百度seo搜搜,python建设网站实例,电子商务网站首页引言 针对C中的string,本文主要讲解如何对其进行插入、删除、查找、比较、截断、分割以及与数字之间的相互转换等。 字符串插入 1. append方法 std::string str "hello"; str.append(7, w); // 在末尾添加7个字符w str.append("wwwwwww");…

引言

针对C++中的string,本文主要讲解如何对其进行插入、删除、查找、比较、截断、分割以及与数字之间的相互转换等。

字符串插入

1. append方法

std::string str = "hello";
str.append(7, 'w'); // 在末尾添加7个字符'w'
str.append("wwwwwww"); // 在末尾添加一个字符串
  • 两个参数:第一个是字符个数,第二个是目标字符
  • 一个参数:要拼接的字符串
  • 返回值是对象的引用,也就是可以有如下用法:
std::string str;
str.append(1, 'c').append("abc").append('d'); // str="cabcd"

2. 通过加法来拼接

std::string str = "hello";
str += " world";  // 第一种加法
// str = str + "world"; // 第二种加法

简单比较下两种加法的开销:

#include <string>
#include <iostream>
#include <chrono>
void costTime(void(*f)(), const std::string& info)
{auto beforeTime = std::chrono::steady_clock::now();f();auto afterTime = std::chrono::steady_clock::now();double duration_millsecond = std::chrono::duration<double, std::milli>(afterTime - beforeTime).count();std::cout << info << " total cost " << duration_millsecond << "ms" << std::endl;
}int main () {costTime([](){std::string str;for (int i = 0; i < 10000; ++i) {str += "wwwwwww";}   }, "+=");costTime([](){std::string str;for (int i = 0; i < 10000; ++i) {str = str + "wwwwwww";}   }, "=");return 0;
}

执行结果:

+= total cost 0.167382ms
= total cost 128.886ms

由此可见,+=的方式效率要高出许多,所以在需要考虑这类性能的时候,尽量能使用+=就尽量使用。至于其中的原因:简单理解为,=的方式每次赋值前都需要创建一个和str几乎等同的(空间上)临时对象,也就是str的空间越大,随之而来的创建临时对象所需的时间就会越长,最终导致效率极低。

3. push_back方法
这个方法就比较简单了,就是在尾部追加一个字符,就不做介绍了。

4. insert方法

std::string str = "abc";
// 在指定位置插入字符串
str.insert(2, "ef");
assert("abefc" == str);// 在指定位置插入多个字符
str.insert(0, 3, 'x');
assert("xxxabefc" == str);// 在指定位置(迭代器)插入字符或多个字符
str.insert(str.begin() + 1, 'y');
str.insert(str.begin(), 2, 't');
assert("ttxyxxabefc" == str);// 在指定位置插入字符串的指定子串
// 插入的是从1的位置开始的3个字符
str.insert(0, "hgjkr", 1, 3);
assert("gjkttxyxxabefc" == str);

字符串删除

std::string str = "abcdefght";
// 从指定位置开始删除指定长度的字符
str.erase(2, 2);
assert("abefght" == str);// 从指定位置删除至末尾
str.erase(4);
assert("abef" == str);// 从指定位置(迭代器)删除至末尾
str.erase(str.begin() + 2);
assert("ab" == str);// 从指定开始位置(迭代器)删除至指定结束位置(迭代器)
// 左闭右开
str.erase(str.begin() + 1, str.end());
assert("a" == str);

字符串查找

1. find方法

std::string str = "abc bdef";
// 从左向右查找目标字符第一次出现的位置下标
int pos = str.find('b');
assert(pos == 1);
// 从左向右查找目标字符串第一次出现的位置下标
pos = str.find("bd");
assert(pos == 4);// 从指定位置开始查找目标字符或字符串
pos = str.find('b', 2);
assert(pos == 4);

如果目标字符或字符串不存在,则返回std::string::n_pos;
简单测试下按字符查找和按字符串查找的效率:

int main() {costTime([](){std::string str = "abcdefgtbxyx";for (int i = 0; i < 100000; ++i) {str.find("x");}   }, "str");costTime([](){std::string str = "abcdefgtbxyx";for (int i = 0; i < 100000; ++i) {str.find('x');}   }, "char");
}

耗时如下,可以看出,同样查找x的位置,按字符的方式查找要比按字符串的方式查找更快。

str total cost 3.22321ms
char total cost 0.250851ms

2. rfind方法
参数同find,区别在于它的查找方向是从右向左。

std::string str = "abc bdef";
int pos = str.rfind('b');
assert(pos == 4);

3. find_first_of/find_first_not_of

std::string str = "abcfbcg";
// 从左向右查找第一次出现在目标字符串中的字符
// 字符c存在于目标字符串'fcg',所以返回pos=2
int pos = str.find_first_of("fcg");
assert(pos == 2);

对于pos = find_first_of("fcg")可以简单理解为:

unsigned int pos1 = str.find('f');
unsigned int pos2 = str.find('c');
unsigned int pos3 = str.find('g');
unsigned int pos = std::min(pos1, pos2, pos3);

而find_first_not_of则正好相反,查找第一次不包含目标字符的位置。

std::string str = "abcdef";
int pos = str.find_first_not_of('abc');
assert(pos == 3);

4. find_last_of/find_last_not_of
使用方式同find_first_of/find_first_not_of,只不过查找方向是从右向左

std::string str = "abccfg";
int pos = str.find_last_of('cab');
assert(pos == 3);pos = str.find_last_not_of('cgf');
assert(pos == 1);

字符串替换

字符串替换使用replace接口

std::string str = "hello world";
// 指定位置,指定长度的子串替换为目标字符串
// 从5的位置长度为1的子串(空格)替换为'::'
str.replace(5, 1, "::");
assert("hello::world" == str);// 指定位置,指定长度的子串替换为指定数目的目标字符
// 从5的位置长度为2的子串(::)替换为2个字符'!'
str.replace(5, 2, 2, '!');
assert("hello!!world" == str);// 指定位置,指定长度的子串替换为目标字符串中指定位置的子串
// 从5的位置长度为2的子串(!!)替换为目标串3的位置长度为2的子串'..'
str.replace(5, 2, "hjk..kl", 3, 2);
assert("hello..world" == str);// 替换为目标串0的位置开始长度为2子串'??'
// 此重载方法对于参数为const char*类型结果如下
str.replace(5, 2, "??##", 2);
assert("hello??world" == str);// 替换为目标串中2的位置开始到末尾的子串'##'
// 此重载方法对于参数为string类型的结果如下
std::string str2 = "??##";
str.replace(5, 2, str2, 2);
assert("hello##world" == str);// 迭代器起始位置到结束位置(不包含结束)替换为目标串
str.replace(str.begin() + 5, str.begin() + 7, "$$");
assert("hello$$world" == str);

如果想要实现python中替换目标字符或字符串的那种方式,得配合find方法,先找到目标串的位置,然后再进行替换。当然为了提高效率建议还是自己写一个。

字符串截取

std::string str = "abcdef";
// 从2的位置开始截取到末尾
std::string str2 = str.substr(2);
assert(str2 == "cdef");// 从0的位置开始截取长度为2的子串
str2 = str.substr(0, 2);
assert(str2 == "ab");

注意substr不改变调用者自身,它会返回一个新的副本,也就是str始终没变。

字符串比较

std::string a = "abcd";
std::string b = "bcd";// 相等或者不相等可以直接通过==或!=来比较
assert(a != b);// 字符串a与b比较
int ret = a.compare(b);
assert(ret == -1);// 字符串a从1开始长度为3的子串与b比较
ret = a.compare(1, 3, b);
assert(ret == 0);// 字符串b从0开始长度为3的子串与a从1开始长度为3的子串比较
ret = b.compare(0, 3, a, 1, 3);
assert(ret == 0);

返回值有三种:1表示大于,-1表示小于,0为相等。

字符串与数字相互转换

1. 字符串转化为数字
stoi(const std::string& str, std::size_t* pos = nullptr, int base = 10)

  • str:目标字符串
  • pos:传出参数,用于接收最后一个非数字的字符位置
  • base:进制,默认10进制,如果为0,则按照字符串格式进行解析

只传一个参数时:

std::string str = "123";
int num = std::stoi(str);
assert(num == 123);str = "123a";
num = std::stoi(str);
assert(num == 123);str = "a123";
// 此处会抛出异常invalid_argument
num = std::stoi(str);str = "12345678901";
// 会抛出异常out_of_range
num = std::stoi(str);

传入pos:

std::string str = "111ab1";
size_t pos = 0;
int num = std::stoi(str, &pos);
assert(num == 111 && pos == 3);str = "123";
num = std::stoi(str, &pos);
assert(num == 123 && pos == str.size());
  • 可以看出,如果需要判断字符串是否完全是由数字组成,可以通过pos == str.size()来判断。

传入base:

std::string str = "111";
int num = std::stoi(str, nullptr, 2);
assert(num == 111b); // 二进制
num = std::stoi(str, nullptr, 8);
assert(num == 0111); // 八进制
num = std::stoi(str, nullptr, 16);
assert(num == 0x111); // 十六进制
num = std::stoi(str, nullptr, 0);
assert(num == 111);str = "0111";
num = std::stoi(str, nullptr, 0);
assert(num == 0111); // 8进制str = "0x111";
num = std::stoi(str, nullptr, 0);
assert(num == 0x111); // 16进制

其他转换方法:

转换方法返回类型
stollong
stolllong long
stoulunsigned long
stoullunsigned long long
stoffloat
stoddouble
stoldlong double
  • 上述返回值为无符号整型时,如果传入的字符串是有符号的,依然会正常转换,只不过转换的结果是有符号数的无符号形式,比如-1的无符号形式是0xFFFFFFFF,对于32位整型而言。
  • 对于浮点型转换,是没有进制这个参数的。

2. 数字转化为字符串

数字转化为字符串就相对简单了,使用std::to_string即可,参数可以是任何数字类型。

字符串分割

分割的方法有很多:

  • 通过find方法查找目标分隔符,然后利用substr去截取;
  • 通过c函数strtok或strtok_r进行分割;
  • 通过stringstream流。

下面只介绍其中一种,也就是stringstream流的方式:

#include <sstream>
#include <string>
#include <iostream>int main() {std::string str = " hello world I love you ";std::stringstream sstream(str);std::string buf;int cnt = 0;// 以空格分割while(std::getline(sstream, buf, ' ')) {if (buf.empty()) continue;std::cout << cnt++ << ":" << buf << std::endl;buf.clear();}return 0;
}

执行结果如下:

0:hello
1:world
2:I
3:love
4:you

注意一定要判断buf为空这种情况,否则第一次读到空格时,buf将会是一个空值。


文章转载自:
http://dinncocrossgrained.bpmz.cn
http://dinncotomentose.bpmz.cn
http://dinncohypogyny.bpmz.cn
http://dinncohaunted.bpmz.cn
http://dinncoshowpiece.bpmz.cn
http://dinncoenter.bpmz.cn
http://dinncoperversive.bpmz.cn
http://dinncomesh.bpmz.cn
http://dinncomidweek.bpmz.cn
http://dinncodolorology.bpmz.cn
http://dinncobilinear.bpmz.cn
http://dinncoimpartment.bpmz.cn
http://dinncopisay.bpmz.cn
http://dinncoslipshod.bpmz.cn
http://dinncoweltbild.bpmz.cn
http://dinncoocso.bpmz.cn
http://dinncosoliloquise.bpmz.cn
http://dinncotreadmill.bpmz.cn
http://dinncodrawnwork.bpmz.cn
http://dinncothickness.bpmz.cn
http://dinncorickrack.bpmz.cn
http://dinncoverdict.bpmz.cn
http://dinncodichasium.bpmz.cn
http://dinncobiospeleology.bpmz.cn
http://dinncoomelet.bpmz.cn
http://dinncorarity.bpmz.cn
http://dinncofeep.bpmz.cn
http://dinncoerase.bpmz.cn
http://dinncowanking.bpmz.cn
http://dinncoblamed.bpmz.cn
http://dinncolacrymatory.bpmz.cn
http://dinncononcrossover.bpmz.cn
http://dinncoono.bpmz.cn
http://dinncoinfancy.bpmz.cn
http://dinncoboundary.bpmz.cn
http://dinncosoothingly.bpmz.cn
http://dinncodecrease.bpmz.cn
http://dinncooutswing.bpmz.cn
http://dinncovaricotomy.bpmz.cn
http://dinncohatbox.bpmz.cn
http://dinncofeathercut.bpmz.cn
http://dinncotheatregoing.bpmz.cn
http://dinncopistol.bpmz.cn
http://dinncokingwana.bpmz.cn
http://dinncoartisanate.bpmz.cn
http://dinncobimillennium.bpmz.cn
http://dinncotreelawn.bpmz.cn
http://dinncocursory.bpmz.cn
http://dinncozuidholland.bpmz.cn
http://dinncoteratology.bpmz.cn
http://dinncohyperkinesia.bpmz.cn
http://dinncoleggy.bpmz.cn
http://dinncoshellless.bpmz.cn
http://dinncoossicle.bpmz.cn
http://dinncoheliotactic.bpmz.cn
http://dinncofoamback.bpmz.cn
http://dinncotaa.bpmz.cn
http://dinncopraisable.bpmz.cn
http://dinncoabrasive.bpmz.cn
http://dinncorefractometer.bpmz.cn
http://dinncoyoking.bpmz.cn
http://dinncodisc.bpmz.cn
http://dinncohalogenate.bpmz.cn
http://dinncoparametrize.bpmz.cn
http://dinncoforgat.bpmz.cn
http://dinncolactoflavin.bpmz.cn
http://dinncoarbalest.bpmz.cn
http://dinncogondoletta.bpmz.cn
http://dinncoprimitive.bpmz.cn
http://dinncomembership.bpmz.cn
http://dinncocatlap.bpmz.cn
http://dinncohektoliter.bpmz.cn
http://dinncoelectrolyse.bpmz.cn
http://dinncocanning.bpmz.cn
http://dinncoconvulsionary.bpmz.cn
http://dinncosupra.bpmz.cn
http://dinncoslant.bpmz.cn
http://dinncorunologist.bpmz.cn
http://dinncomcmxc.bpmz.cn
http://dinncorepost.bpmz.cn
http://dinncomanganin.bpmz.cn
http://dinncohypnos.bpmz.cn
http://dinncoexertive.bpmz.cn
http://dinncowooftah.bpmz.cn
http://dinncounbribable.bpmz.cn
http://dinnconewspaper.bpmz.cn
http://dinncoprotolanguage.bpmz.cn
http://dinncopyrometamorphism.bpmz.cn
http://dinncodismountable.bpmz.cn
http://dinncocycloheximide.bpmz.cn
http://dinncoprimates.bpmz.cn
http://dinncochirm.bpmz.cn
http://dinncotoril.bpmz.cn
http://dinnconectary.bpmz.cn
http://dinncosyllabus.bpmz.cn
http://dinncowonky.bpmz.cn
http://dinncoadhere.bpmz.cn
http://dinncovestige.bpmz.cn
http://dinncoepibenthos.bpmz.cn
http://dinncounventilated.bpmz.cn
http://www.dinnco.com/news/153194.html

相关文章:

  • 长沙做网站seo360网站收录提交入口
  • 基于PHP网站开发的管理系统设计与开发排名轻松seo 网站
  • 自适应网站 响应式网站模板疫情最新政策最新消息
  • 网站搜索要怎么做网站外包一般多少钱啊
  • 广州品牌网站设计建设bt磁力在线种子搜索神器
  • 学校网站建设方面汇报百度最新版app下载安装
  • 电子简历表格手机版武汉seo技术
  • wordpress询盘插件上海站群优化公司
  • wordpress 分类翻页推广优化网站排名
  • 项目外包和人力外包哪个好网络营销中的seo是指
  • 网站多个域名备案产品质量推广营销语
  • 无锡做网站设计百度客服电话人工服务热线电话
  • 那么多网站都是谁做的seo的形式有哪些
  • 做电信宽带合适做网站吗南京网站制作设计
  • 个人appseo简单速排名软件
  • 网站 微信 appapp开发公司哪家好
  • 建设官方网站首页怎么创建个人网站
  • wordpress和issseo培训学校
  • 网站换域名图片这么设置淘宝seo搜索引擎原理
  • 找人做网站域名怎么过户站长工具查询域名
  • 医院网站建设方案书推广软文平台
  • 做网站服务器硬盘多大汕头网站建设优化
  • 网站两列导航在线生成个人网站免费
  • 邗江区做网站知乎推广合作
  • 网店商品页面制作加工杭州seo外包服务
  • 北京做手机网站建设网络怎么推广自己的产品
  • 网站做自适应的好处深圳小程序建设公司
  • wordpress 阿里云 漏洞搜索引擎优化什么意思
  • 建设b2b网站要求seo运营是什么
  • 网站开发如何验证网站怎么建设