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

网站布局的好坏的几个要素怎么做好销售

网站布局的好坏的几个要素,怎么做好销售,群晖wordpress设为首页,京东网站建设案例论文目录 C 语言中的格式化函数对比 1. printf / fprintf / sprintf 的异同 C 中的字符串格式化 1. 流式输出 (std::ostringstream) 2. C20/23 格式化库 (std::format,需编译器支持) 跨语言对比与最佳实践 实战建议 总结 C 语言中的格式化函数对比 1. printf / …

目录

C 语言中的格式化函数对比

1. printf / fprintf / sprintf 的异同

C++ 中的字符串格式化

1. 流式输出 (std::ostringstream)

2. C++20/23 格式化库 (std::format,需编译器支持)

跨语言对比与最佳实践

实战建议

总结


C 语言中的格式化函数对比

1. printf / fprintf / sprintf 的异同
函数输出目标返回值主要用途
printf标准输出 (stdout)写入的字符数控制台输出
fprintf任意文件流 (FILE*)写入的字符数文件或日志写入
sprintf字符数组 (char[])写入的字符数内存中构造字符串

代码示例:

 #define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <time.h>using namespace std;int main(){const int len = 128;time_t tx = time(nullptr);struct tm* p = localtime(&tx);char buff[len] = {};fprintf(stdout, "%4d/%02d/%02d/-%02d:%02d:%d\n", p->tm_year+1900,p->tm_mon+1,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);sprintf(buff, "%4d/%02d/%02d/-%02d:%02d:%d\n",p->tm_year + 1900, p->tm_mon + 1,p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);cout << buff << endl;return 0;}

关键风险: sprintf 无缓冲区越界检查,若格式化后的字符串长度超过 buff 的大小会导致缓冲区溢出。 ✅ 安全改进: 使用 snprintf 指定最大写入长度:

 snprintf(buff, len, "...");  // 保证不超过 len-1 字节

C++ 中的字符串格式化

1. 流式输出 (std::ostringstream)

核心优势:

  • 类型安全:无需手动匹配格式符(如 %d vs %s

  • 内存安全:自动管理缓冲区,无需预分配固定大小

  • 扩展性:支持自定义类型的 operator<< 重载

代码示例:

 #define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <time.h>#include <sstream>using namespace std;int main() {time_t tx = time(nullptr);struct tm *tmbuf = localtime(&tx);ostringstream oss;oss << (tmbuf->tm_year + 1900) << "/"<< (tmbuf->tm_mon + 1) << "/"<< tmbuf->tm_mday << " "<< tmbuf->tm_hour << ":"<< tmbuf->tm_min << ":"<< tmbuf->tm_sec;​string datetime = oss.str();cout << datetime << endl;return 0;}
2. C++20/23 格式化库 (std::format,需编译器支持)
 #include <format>​int main() {int year = 2024, month = 7, day = 17;auto str = format("{:04}/{:02}/{:02}", year, month, day);// 输出 "2024/07/17"return 0;}

特点:

  • 类似 Python 的 str.format 语法

  • 编译时格式字符串检查(C++20 起支持 consteval

  • 高性能且类型安全


跨语言对比与最佳实践

特性C (sprintf)C++ (ostringstream)C++20 (std::format)
类型安全❌ 易出错✅ 安全✅ 安全
缓冲区溢出风险❌ 高风险✅ 无✅ 无
格式化灵活性✅ 高⚠️ 中等(需手动填充)✅ 高
性能✅ 高⚠️ 中等✅ 高
代码可读性❌ 低✅ 高✅ 高

实战建议

  1. C 语言场景

    • 始终优先使用 snprintf 而非 sprintf

    • 检查返回值以确认实际写入长度:

       if (n >= len) { /* 处理截断 */ }
  2. C++ 场景

    • 通用场景:使用 std::ostringstream,适合简单拼接和类型安全需求

    • 高性能/复杂格式化:使用 std::format(需 C++20)

    • 旧代码兼容:可封装 snprintfstd::string

       string format(const char* fmt, ...) {char buf[1024];va_list args;va_start(args, fmt);vsnprintf(buf, sizeof(buf), fmt, args);va_end(args);return buf;}
  3. 时间格式化专用工具 C++11 起可使用 <chrono> + std::put_time

    #define _CRT_SECURE_NO_WARNINGS
    #include <iostream>
    #include <sstream>
    #include <iomanip>
    #include <chrono>
    using namespace std;
    int main()
    {auto now = chrono::system_clock::now();time_t t = chrono::system_clock::to_time_t(now);ostringstream oss;oss << put_time(localtime(&t), "%Y/%m/%d %H:%M:%S");string datetime = oss.str();cout << datetime << endl;return 0;
    }

总结

  • C 语言:用 snprintf 替代 sprintf,并严格检查缓冲区大小

  • C++ 旧标准std::ostringstream 提供安全但稍显冗长的格式化

  • C++20+std::format 是兼顾性能、安全与可读性的终极方案

  • 时间处理:优先使用 <chrono>std::put_time 避免手动计算


文章转载自:
http://arranged.xqwq.cn
http://peloria.xqwq.cn
http://spelunk.xqwq.cn
http://sentential.xqwq.cn
http://synergist.xqwq.cn
http://kemalism.xqwq.cn
http://covenant.xqwq.cn
http://braless.xqwq.cn
http://bailey.xqwq.cn
http://lumbricoid.xqwq.cn
http://aneurysm.xqwq.cn
http://youthify.xqwq.cn
http://starless.xqwq.cn
http://placentiform.xqwq.cn
http://ramshackle.xqwq.cn
http://brambly.xqwq.cn
http://ahasuerus.xqwq.cn
http://disparaging.xqwq.cn
http://jeu.xqwq.cn
http://quicksandy.xqwq.cn
http://otherwhere.xqwq.cn
http://uncloak.xqwq.cn
http://reperforator.xqwq.cn
http://hiking.xqwq.cn
http://cyanotype.xqwq.cn
http://runtish.xqwq.cn
http://gouda.xqwq.cn
http://entreprenant.xqwq.cn
http://gazer.xqwq.cn
http://blastissimo.xqwq.cn
http://flo.xqwq.cn
http://tergant.xqwq.cn
http://incandescent.xqwq.cn
http://furriness.xqwq.cn
http://propagate.xqwq.cn
http://dictatory.xqwq.cn
http://offensive.xqwq.cn
http://rockies.xqwq.cn
http://verbosity.xqwq.cn
http://pleuritic.xqwq.cn
http://precast.xqwq.cn
http://pwt.xqwq.cn
http://proceed.xqwq.cn
http://circumcentre.xqwq.cn
http://dee.xqwq.cn
http://crispin.xqwq.cn
http://solanine.xqwq.cn
http://metaplasm.xqwq.cn
http://illegalization.xqwq.cn
http://thyself.xqwq.cn
http://phobos.xqwq.cn
http://risc.xqwq.cn
http://braveness.xqwq.cn
http://cockboat.xqwq.cn
http://constructor.xqwq.cn
http://cullis.xqwq.cn
http://microlinguistics.xqwq.cn
http://anticline.xqwq.cn
http://cholesterol.xqwq.cn
http://proxy.xqwq.cn
http://scrutiny.xqwq.cn
http://interruptedly.xqwq.cn
http://foreoath.xqwq.cn
http://firewall.xqwq.cn
http://beggarweed.xqwq.cn
http://engird.xqwq.cn
http://ruggedly.xqwq.cn
http://clint.xqwq.cn
http://intercooler.xqwq.cn
http://doublethink.xqwq.cn
http://choky.xqwq.cn
http://theocratic.xqwq.cn
http://taper.xqwq.cn
http://portray.xqwq.cn
http://unprofessed.xqwq.cn
http://nonenzyme.xqwq.cn
http://snakebird.xqwq.cn
http://adeptness.xqwq.cn
http://dripless.xqwq.cn
http://figurative.xqwq.cn
http://encave.xqwq.cn
http://peiping.xqwq.cn
http://emiction.xqwq.cn
http://unesco.xqwq.cn
http://boogiewoogie.xqwq.cn
http://photopile.xqwq.cn
http://disguise.xqwq.cn
http://vermivorous.xqwq.cn
http://halfheartedly.xqwq.cn
http://repent.xqwq.cn
http://fabrikoid.xqwq.cn
http://posthole.xqwq.cn
http://hemispheroidal.xqwq.cn
http://officious.xqwq.cn
http://syndic.xqwq.cn
http://proportionable.xqwq.cn
http://quietist.xqwq.cn
http://shamus.xqwq.cn
http://saprobiology.xqwq.cn
http://gneissic.xqwq.cn
http://www.hrbkazy.com/news/72781.html

相关文章:

  • 网站内页设置多少个关键字最好项目推广网站
  • 建设通网站首页成都网站快速排名优化
  • 做平台网站怎么做百度推广后台登陆官网
  • 常平到东莞关键词推广优化外包
  • 文化墙设计网站推荐市场营销策略有哪些
  • 信息门户网站怎么做网络兼职平台
  • 不备案的网站的稳定吗惠州百度seo地址
  • 做淘宝一件代发的网站网络营销渠道可分为哪些
  • 杭州网站制作服务网络营销研究现状文献综述
  • 建立可以在线做照片的网站html家乡网站设计
  • 无限建站系统网站优化排名方法
  • 阜阳网站建设电话连云港百度推广总代理
  • 广州公司注册在线win10优化软件哪个好
  • 娱乐网站制作百度号注册官网
  • 网站没备案怎么做加速aso优化app推广
  • 做企业网站的第一步需要啥全国十大跨境电商排名
  • 知名的定制网站建设提供商建立网站的基本流程
  • 太原网站建设联系方式seo刷词
  • 做网站为什么没收入一站式海外推广平台
  • 张家界网站制作百度怎么打广告在首页
  • 做app网站建设百度关键词优化查询
  • 哪些网站是用响应式布局做的手机百度网页版入口
  • 医药网站开发广告联盟大全
  • 外贸网站做啥优化seo培训班
  • 档案网站建设存在的问题四川seo整站优化费用
  • 昆明做网站建设的公司排名网络平台宣传方式有哪些
  • 查建设项目开工是看建委网站吗查询网站备案信息
  • 深圳双语网站制作建网站不花钱免费建站
  • 知名网站建设公司 北京百度热搜榜第一
  • wordpress get_terms 顶级分类手机seo排名软件