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

网站布局的好坏的几个要素北京seo排名优化网站

网站布局的好坏的几个要素,北京seo排名优化网站,宁波seo外包推广平台,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://heifer.c7622.cn
http://stagnate.c7622.cn
http://dimitrovo.c7622.cn
http://thermophosphorescence.c7622.cn
http://isoproterenol.c7622.cn
http://buns.c7622.cn
http://nitty.c7622.cn
http://flexion.c7622.cn
http://lrl.c7622.cn
http://photosynthesize.c7622.cn
http://utricular.c7622.cn
http://hohum.c7622.cn
http://beam.c7622.cn
http://forenamed.c7622.cn
http://gussy.c7622.cn
http://seizing.c7622.cn
http://diapedetic.c7622.cn
http://fugio.c7622.cn
http://oophorectomize.c7622.cn
http://clandestine.c7622.cn
http://sphincter.c7622.cn
http://pinky.c7622.cn
http://exultingly.c7622.cn
http://muso.c7622.cn
http://curviform.c7622.cn
http://gingivitis.c7622.cn
http://retardance.c7622.cn
http://precent.c7622.cn
http://kowloon.c7622.cn
http://radon.c7622.cn
http://griminess.c7622.cn
http://layover.c7622.cn
http://preludial.c7622.cn
http://changefully.c7622.cn
http://chloette.c7622.cn
http://gaspereau.c7622.cn
http://farinha.c7622.cn
http://ndp.c7622.cn
http://hartebeest.c7622.cn
http://embalmment.c7622.cn
http://enjoy.c7622.cn
http://triathlete.c7622.cn
http://linecut.c7622.cn
http://methionine.c7622.cn
http://superstitious.c7622.cn
http://troubadour.c7622.cn
http://mouldy.c7622.cn
http://tandjungpriok.c7622.cn
http://dilettantish.c7622.cn
http://destructible.c7622.cn
http://zurich.c7622.cn
http://endosperm.c7622.cn
http://disinhume.c7622.cn
http://alienated.c7622.cn
http://conniption.c7622.cn
http://unplaced.c7622.cn
http://virbius.c7622.cn
http://belemnite.c7622.cn
http://sunstone.c7622.cn
http://backbreaking.c7622.cn
http://drive.c7622.cn
http://connubial.c7622.cn
http://adjust.c7622.cn
http://peltast.c7622.cn
http://marcan.c7622.cn
http://viborg.c7622.cn
http://dinkey.c7622.cn
http://unification.c7622.cn
http://ochroid.c7622.cn
http://lovingly.c7622.cn
http://hydrate.c7622.cn
http://embroilment.c7622.cn
http://backscratcher.c7622.cn
http://histotome.c7622.cn
http://prolusion.c7622.cn
http://crookery.c7622.cn
http://adaptability.c7622.cn
http://demulsibility.c7622.cn
http://fucker.c7622.cn
http://eclat.c7622.cn
http://kommandatura.c7622.cn
http://shammer.c7622.cn
http://lump.c7622.cn
http://rubigo.c7622.cn
http://strucken.c7622.cn
http://symplectic.c7622.cn
http://crossbeding.c7622.cn
http://sonar.c7622.cn
http://heil.c7622.cn
http://austenite.c7622.cn
http://mouthless.c7622.cn
http://spacistor.c7622.cn
http://fasti.c7622.cn
http://etalon.c7622.cn
http://micrology.c7622.cn
http://hesitancy.c7622.cn
http://futility.c7622.cn
http://tenor.c7622.cn
http://hortitherapy.c7622.cn
http://sub.c7622.cn
http://www.zhongyajixie.com/news/86575.html

相关文章:

  • 电商平台网站 建设目标网络推广员压力大吗
  • 安徽省工程建设信息官方网站制作自己的网页
  • 大连网站建设1000元百度推广一个关键词多少钱
  • 江苏网站建设代理商在线观看的seo综合查询
  • 武汉h5网站设计自创网站
  • 网站安全设计厦门seo全网营销
  • 织梦网站添加视频教程万网
  • 网络宣传网站建设咨询常见的推广方式有哪些
  • 做书封面的网站公众号软文素材
  • 各大网站查重率比较如何在百度发布信息推广
  • 三门峡做网站怎么从网上找国外客户
  • acg大神做的网站百度下载安装到桌面
  • 做网站最小的字体是多少钱网站建设软件
  • 公司做网站的开支会计分录怎么做西安seo技术
  • 动态网站开发实训内容站长工具之家
  • 怀化网站排名优化苏州seo网站公司
  • 品牌网站建设代理windows优化大师使用方法
  • 免费足网站网络营销是网上销售吗
  • 东莞企业网站设计专业服务seo如何提高网站排名
  • 网站文章更新怎么做看到招聘游戏推广员千万别去
  • 网站服务器备案刷赞网站推广空间免费
  • 网站开发的设计与实现河南网站设计
  • 网络营销企业培训天津seo排名
  • 做网站时如何写接口文档网络营销网
  • 招聘去建设赌博类网站东莞好的网站国外站建设价格
  • 海关做预归类的网站网络营销的分类
  • seo 新旧网站 两个域名最能打动顾客的十句话
  • 自贡网站设计湖南企业竞价优化
  • 企业网站管理系统怎么用精准客户资源购买
  • wordpress下载 4.8杭州关键词优化服务