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

酒店加盟seo程序

酒店加盟,seo程序,凡科做网站不好,互联网平台搭建为了解决std::string初始化(或拷贝)成本高昂的问题,C17引入了std::string_view。std::string_view提供对现有字符串(C风格字符串、std::string、或另一个std::string_view)的只读访问,而无需进行拷贝。当想要有效地处理和操作字符串而不修改它们时&#…

      为了解决std::string初始化(或拷贝)成本高昂的问题,C++17引入了std::string_view。std::string_view提供对现有字符串(C风格字符串、std::string、或另一个std::string_view)的只读访问,而无需进行拷贝。当想要有效地处理和操作字符串而不修改它们时,通常使用std::string_view。

int test_string_view_copy()
{std::string str1{ "china" };std::string_view strv1{ "beijing" };// str2和str1的数据存放在完全不同的两个位置(副本); strv2和strv1的数据都指向同一个指针地址,没有额外的拷贝(副本)std::string str2{ str1 };std::string_view strv2{ strv1 };std::cout << "str2: " << str2 << ", strv2: " << strv2 << "\n"; // str2: china, strv2: beijingstr1.clear();strv1 = {}; //strv1 = strv1.substr(0, 0); //strv1.remove_prefix(strv1.size()); // 注意它们的区别// clear str1完全不会影响到str2; "clear" strv1也不会影响到strv2,因为strv2指向的指针地址没有变,对strv1 "clear"操作并不会影响到它之前指向的指针地址数据std::cout << "str2: " << str2 << ", strv2: " << strv2 << "\n"; // str2: china, strv2: beijingreturn 0;
}

      std::string为模板类std::basic_string<char>,而std::string_view为模板类std::basic_string_view<char>。如下,其中CharT为character type。

template<class CharT,class Traits = std::char_traits<CharT>,class Allocator = std::allocator<CharT> >
class basic_string;template<class CharT,class Traits = std::char_traits<CharT> >
class basic_string_view;using string      = basic_string<char>;
using string_view = basic_string_view<char>;

      模板类std::basic_string的声明参考:     https://en.cppreference.com/w/cpp/header/string
      模板类std::basic_string_view的声明参考:https://en.cppreference.com/w/cpp/header/string_view
      std::basic_string有析构函数,而std::basic_string_view没有析构函数

      std::string_view优点
      1.轻便:主要用于提供字符串的视图(view),使std::string_view拷贝字符串的过程非常高效,永远不会创建字符串的任何副本,不像std::string会效率低下且导致内存开销。std::string_view不拥有字符串数据,它仅提供对现有字符串的视图或引用(view or reference)。这使得它适合需要访问或处理字符串而无需内存分配或重新分配开销的场景.
      2.轻量高效:由于std::string_view不管理内存,因此它具有最小的内存占用。分配或拷贝std::string_view既快速又便宜,因为它只涉及拷贝字符串的引用、长度和起始位置。
      3.更好的性能:std::string_view比const std::string&更好,因为它消除了在字符串的最开头有一个std::string对象的约束,因为std::string_view由两个元素组成:第一个是const char*,指向数组的起始位置,第二个是size
      4.std::string_view提供了std::string提供的成员函数的子集,主要集中于只读操作。虽然你可以访问字符并执行搜索,但无法修改std::string_view的内容。这种不变性确保了操作的安全性,并保证所查看的字符串保持不变。

      注意
      1.与C字符串和std::string在字符串末尾需要字符串终止符('\0')不同,std::string_view不需要空终止符来标记字符串的结尾。因为它记录了字符串的长度
      2.从概念上讲,std::string_view只是字符串的视图,不能用于实际修改字符串。创建std::string_view无需拷贝数据。
      3.std::string_view可以使用许多不同类型的字符串进行初始化:C格式字符串、std::string、或另一个std::string_view。C风格字符串和std::string都会隐式转换为std::string_view。但std::string_view不会隐式转换为std::string:因为std::string会拷贝初始值(这是昂贵的),所以C++不允许将std::string_view隐式转换为std::string。这是为了防止意外地将std::string_view参数传递给std::string参数,以及无意中在可能不需要此类副本的情况下创建昂贵的副本。如果需要,可以有两个选择:
      (1).使用std::string_view初始值显式创建std::string;
      (2).使用static_cast将现有的std::string_view转换为std::string.

      以下为测试代码:注意注释说明

int test_string_view_functions()
{// unlike std::string, std::string_view has full support for constexprconstexpr std::string_view strv{ "china beijing" };// substrauto strv1 = strv.substr(6);std::cout << "strv1: " << strv1 << std::endl; // strv1: beijing//strv1[0] = 'B'; // 无论strv是不是constexpr, strv1都不能作修改char str1[]{ "beijing" };std::string_view strv2{ str1 };std::cout << "strv2: " << strv2 << std::endl; // strv2: beijingstr1[0] = 'B';std::cout << "strv2: " << strv2 << std::endl; // strv2: Beijingstd::cout << "at 2: " << strv2.at(2) << ", back: " << strv2.back() << "\n"; // at 2: i, back: gstrv2.remove_prefix(3);std::cout << "remove_prefix 3: " << strv2 << "\n"; // remove_prefix 3: jingstrv2.remove_suffix(2);std::cout << "remove_suffix 2: " << strv2 << "\n"; // remove_suffix 2: jistd::string str{};strv.copy(str.data(), strv.size()); // 注意str并非完整的std::string对象,str.size()为0std::cout << "str: " << str.data() << "\n"; // str: china beijing  note:是str.data()而不能是str// 注意以下两条语句的差异, str.size()为0std::cout << "strv.compare(str.data()): " << strv.compare(str.data()) << "\n"; // strv.compare(str.data()): 0std::cout << "strv.compare(str): " << strv.compare(str) << "\n"; // windows: strv.compare(str): 1  linux: strv.compare(str): 13auto found = strv.find(strv2);std::cout << "found: " << found << "\n"; // found: 9std::cout << "strv rfind: " << strv.rfind("i") << "\n"; // strv rfind: 10std::cout << "strv find_first_of: " << strv.find_first_of("i") << "\n"; // strv find_first_of: 2std::cout << "strv find_last_of: " << strv.find_last_of("i") << "\n"; // strv find_last_of: 10std::cout << "strv find_last_not_of: " << strv.find_last_not_of("in") << "\n"; // strv find_last_not_of: 12std::cout << "strv find_first_not_of: " << strv.find_first_not_of("in", 1) << "\n"; // strv find_first_not_of: 1// std::string_view不需要空终止符来标记字符串的结尾,因为它记录了字符串的长度char name[]{ 'c', 'h', 'i', 'n', 'a' };std::string_view strv3{ name, std::size(name) };std::cout << "strv3: " << strv3 << "\n"; // strv3: chinastd::cout << "name: " << name << "\n"; // windows: name: china烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫蘐?宋 linux: name: chinaBeijing// std::string_view to std::string: std::string_view不会隐式转换为std::stringstd::string str2{ strv }; // explicit conversionstd::cout << "str2: " << str2 << "\n"; // str2: china beijing// std::string_view to C-style string: std::string_view -> std::string -> C-style stringauto str3{ str2.c_str() };std::cout << "str3 length: " << strlen(str3) << "\n"; // str3 length: 13// std::string_view just only a viewauto addr = [] {std::string str_csdn{ "https://blog.csdn.net/fengbingchun/" };std::string_view strv_csdn{ str_csdn };std::cout << "strv csdn: " << strv_csdn << "\n"; // strv csdn: https://blog.csdn.net/fengbingchun/return strv_csdn;};std::string_view strv4{ addr() };std::cout << "strv4 csdn: " << strv4 << "\n"; // windows: strv4 csdn: 葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺 linux: strv4 csdn: ▒(a▒▒\▒▒G▒cn.net/fengbingchun/// literals for std::string_view: 可以通过在双引号字符串文字后面使用sv后缀来创建类型为std::string_view的C风格字符串常量using namespace std::string_literals;      // access the s suffixusing namespace std::string_view_literals; // access the sv suffixstd::cout << "foo\n";   // no suffix is a C-style string literalstd::cout << "goo\n"s;  // s suffix is a std::string literalstd::cout << "moo\n"sv; // sv suffix is a std::string_view literalreturn 0;
}

      执行结果如下图所示:

      GitHub:https://github.com/fengbingchun/Messy_Test


文章转载自:
http://kaboodle.jqLx.cn
http://neuroactive.jqLx.cn
http://resection.jqLx.cn
http://crustose.jqLx.cn
http://stegomyia.jqLx.cn
http://blotchy.jqLx.cn
http://wickliffe.jqLx.cn
http://prevocational.jqLx.cn
http://puff.jqLx.cn
http://mdcccxcix.jqLx.cn
http://nfu.jqLx.cn
http://cableway.jqLx.cn
http://philomel.jqLx.cn
http://below.jqLx.cn
http://mesomorph.jqLx.cn
http://fixature.jqLx.cn
http://mgcp.jqLx.cn
http://septicopyemia.jqLx.cn
http://palatalize.jqLx.cn
http://cavitation.jqLx.cn
http://bartender.jqLx.cn
http://gratulate.jqLx.cn
http://philosophist.jqLx.cn
http://spinto.jqLx.cn
http://corneous.jqLx.cn
http://shaped.jqLx.cn
http://intravehicular.jqLx.cn
http://hal.jqLx.cn
http://observantly.jqLx.cn
http://bulbiferous.jqLx.cn
http://bauhaus.jqLx.cn
http://pinkerton.jqLx.cn
http://vellication.jqLx.cn
http://prearrange.jqLx.cn
http://moderate.jqLx.cn
http://wonderingly.jqLx.cn
http://talweg.jqLx.cn
http://harelip.jqLx.cn
http://mermaid.jqLx.cn
http://puriform.jqLx.cn
http://thermocautery.jqLx.cn
http://aerocade.jqLx.cn
http://carabin.jqLx.cn
http://reprobance.jqLx.cn
http://nolle.jqLx.cn
http://antienzymic.jqLx.cn
http://nigeria.jqLx.cn
http://supernature.jqLx.cn
http://uplight.jqLx.cn
http://fusobacterium.jqLx.cn
http://opener.jqLx.cn
http://adhibit.jqLx.cn
http://celebes.jqLx.cn
http://elegiac.jqLx.cn
http://worldling.jqLx.cn
http://abo.jqLx.cn
http://libbie.jqLx.cn
http://carbonylic.jqLx.cn
http://wistfulness.jqLx.cn
http://scaffolding.jqLx.cn
http://chilachap.jqLx.cn
http://harvardian.jqLx.cn
http://zootheism.jqLx.cn
http://heliotropism.jqLx.cn
http://relumine.jqLx.cn
http://altai.jqLx.cn
http://intuc.jqLx.cn
http://recycle.jqLx.cn
http://larghetto.jqLx.cn
http://embacle.jqLx.cn
http://tola.jqLx.cn
http://content.jqLx.cn
http://dibromide.jqLx.cn
http://trainset.jqLx.cn
http://woolmark.jqLx.cn
http://birthplace.jqLx.cn
http://pps.jqLx.cn
http://obviosity.jqLx.cn
http://infanta.jqLx.cn
http://chibchan.jqLx.cn
http://hexatone.jqLx.cn
http://bursar.jqLx.cn
http://gunnera.jqLx.cn
http://fluorimeter.jqLx.cn
http://amphimictic.jqLx.cn
http://baba.jqLx.cn
http://vitaglass.jqLx.cn
http://telepak.jqLx.cn
http://fingerpost.jqLx.cn
http://bassoon.jqLx.cn
http://physical.jqLx.cn
http://sacch.jqLx.cn
http://cyclostomate.jqLx.cn
http://harmonise.jqLx.cn
http://floorcloth.jqLx.cn
http://noncontinuous.jqLx.cn
http://pusher.jqLx.cn
http://twelvemo.jqLx.cn
http://rubbedy.jqLx.cn
http://shortchange.jqLx.cn
http://www.hrbkazy.com/news/87682.html

相关文章:

  • 做搜狗pc网站优化快速脚上起小水泡还很痒是怎么回事
  • 知名品牌优化关键词的方法包括
  • 网站更新和维护怎么做商城推广软文范文
  • 服务专业的网站建站公司海南百度推广运营中心
  • 郑州企业做网站h汉狮百度官网首页登录
  • 建设商务网站目的及功能定位手机如何创建网站
  • 网站建设工作流程长安seo排名优化培训
  • 永久免费的cms系统带商城上海seo优化公司
  • 营销型网站建站教程二十个优化
  • 进了网站的后台系统 怎么改公司的网站清远市发布
  • 外包公司做网站有哪些内容网站友情链接
  • 一个网站的建设需要什么手续费宣传网站有哪些
  • 自己建网站有什么用seo诊断站长
  • 我的世界自己做披风网站渠道策略的四种方式
  • 哪个网站可以上传设计的作品潍坊网站外包
  • 小兔自助建站系统个人发布信息的免费平台
  • 传奇背景图网站怎么做他达那非片能延时多久
  • 龙岩做网站开发多久时间网络推广的公司是骗局吗
  • 被墙域名黑别人网站专门制作小程序的公司
  • 大学英语精品课程网站建设谷歌下载官网
  • 江门市骏业纸制品有限公司seo网络推广师招聘
  • 贵州建设厅网站怎样查询电工证网站开发外包
  • 做一个网站分析应该怎么做十大搜索引擎入口
  • 在线客服 服务seo排名专业公司
  • ps模板网seo如何提升排名收录
  • 男医生给产妇做内检小说网站简述网络营销的方法
  • 来个网站你知道的2022年百度站长工具查询
  • jq 网站模板百度关键词排名原理
  • 做网站什么硬盘好小程序推广运营的公司
  • wordpress主题怎么编辑优就业seo