资讯动态

第一章 C++03标准的改进(代码说明)

发布时间:2026/10/16 14:16:59 来源:尧图企业网站定制
// cpp03_demo.cpp// 编译: g -stdc03 cpp03_demo.cpp -o cpp03_demo// 运行: ./cpp03_demo// 注意: auto_ptr 在 C17 起被移除必须用 C03/C11/C14 标准编译#include iostream // 提供 std::cout / std::cin 输入输出#include vector // 提供 std::vector 动态数组#include string // 提供 std::string 字符串类#include map // 提供 std::map 有序映射#include algorithm // 提供 std::sort 等算法#include memory // 提供 std::auto_ptr#include cstddef // 提供 std::size_t#include sstream // 提供 std::ostringstream / istringstream#include stdexcept // 提供 std::runtime_error 等异常类// // 1. 值初始化// void demo_value_init() { // 演示值初始化std::cout 值初始化 \n; // 打印分组标题int x int(); // 对 int 做值初始化得到 0double d double(); // 对 double 做值初始化得到 0.0std::cout int() x , double() d \n; // 输出结果std::vectorint v(10); // 创建 10 个元素值初始化为 0std::cout vectorint(10): ; // 提示for (std::size_t i 0; i v.size(); i) std::cout v[i] ; // 逐个输出std::cout \n; // 换行}// // 2. 聚合初始化// struct Point { int x; int y; }; // 聚合体只有公有数据成员struct Rect { Point topLeft; Point bottomRight; }; // 嵌套聚合体void demo_aggregate() { // 演示聚合初始化std::cout \n 聚合初始化 \n; // 分组标题Point p {1, 2}; // 用花括号按顺序初始化成员Rect r { {0, 0}, {100, 50} }; // 嵌套花括号初始化std::cout Point: p.x , p.y \n; // 输出 Pointstd::cout Rect: ( r.topLeft.x , r.topLeft.y ) - ( r.bottomRight.x , r.bottomRight.y )\n; // 输出 Rect}// // 3. 空基类优化 (EBO)// struct Empty {}; // 空类无任何成员struct WithInt : Empty { int value; }; // 继承空类并加一个 intvoid demo_ebo() { // 演示空基类优化std::cout \n 空基类优化 \n; // 分组标题std::cout sizeof(Empty) sizeof(Empty) \n; // 空类大小通常为 1std::cout sizeof(WithInt) sizeof(WithInt) \n; // EBO 生效时等于 intstd::cout sizeof(int) sizeof(int) \n; // 对照}// // 4. 异常规范// void no_throw_func() throw() { // throw() 表示不抛任何异常std::cout no_throw_func 正常执行\n; // 输出提示}void demo_exception() { // 演示异常规范std::cout \n 异常规范 \n; // 分组标题no_throw_func(); // 调用不抛异常的函数try { // 开始 try 块throw std::runtime_error(故意抛出的异常); // 主动抛出异常} catch (const std::exception e) { // 捕获标准异常基类std::cout 捕获异常: e.what() \n; // 输出异常信息}}// // 5. auto_ptrC03 智能指针// void demo_auto_ptr() { // 演示 auto_ptrstd::cout \n auto_ptr \n; // 分组标题std::auto_ptrint p1(new int(42)); // 创建 auto_ptr接管堆内存std::cout *p1 *p1 \n; // 解引用输出 42std::auto_ptrint p2 p1; // 拷贝时所有权转移p1 变为空std::cout *p2 *p2 \n; // p2 现在持有资源std::cout p1 是否为空: (p1.get() 0 ? 是 : 否) \n; // 检查 p1}// // 6. STL 与函数对象// struct CompareDesc { // 自定义比较函数对象bool operator()(int a, int b) const { // 重载调用运算符return a b; // 降序a b}};void demo_stl() { // 演示 STL 与函数对象std::cout \n STL 与函数对象 \n; // 分组标题std::vectorint v; // 创建空 vectorv.push_back(3); v.push_back(1); v.push_back(4); v.push_back(1); v.push_back(5); // 添加元素std::sort(v.begin(), v.end(), CompareDesc()); // 用函数对象降序排序std::cout 降序: ; // 提示for (std::size_t i 0; i v.size(); i) std::cout v[i] ; // 输出std::cout \n; // 换行std::mapstd::string, int m; // 创建 mapm[apple] 3; m[banana] 5; // 插入键值对std::mapstd::string, int::iterator it; // 声明迭代器for (it m.begin(); it ! m.end(); it) // 遍历 mapstd::cout it-first it-second ; // 输出键值std::cout \n; // 换行}// // 7. RAII// class Resource { // 演示 RAII 的资源类public:Resource(int id) : id_(id) { // 构造打印提示std::cout Resource id_ 构造\n;}~Resource() { // 析构打印提示std::cout Resource id_ 析构\n;}int id() const { return id_; } // 访问器private:Resource(const Resource); // 私有声明拷贝构造禁用拷贝Resource operator(const Resource); // 私有声明拷贝赋值禁用拷贝int id_; // 资源 ID};void demo_raii() { // 演示 RAIIstd::cout \n RAII \n; // 分组标题Resource r(1); // 创建资源离开作用域自动析构std::cout r.id() r.id() \n; // 使用资源}// // 8. 模板// template typename T // 函数模板T my_max(const T a, const T b) { // 返回较大者return a b ? a : b; // 三元表达式}template typename T, int N // 类模板带非类型参数 Nclass FixedArray { // 固定大小数组public:T operator[](int i) { return data_[i]; } // 非 const 下标int size() const { return N; } // 返回大小private:T data_[N]; // 内嵌数组};void demo_template() { // 演示模板std::cout \n 模板 \n; // 分组标题std::cout my_max(3,7) my_max(3, 7) \n; // 对 int 调用FixedArrayint, 5 arr; // 创建固定数组for (int i 0; i arr.size(); i) arr[i] i * i; // 填充平方std::cout FixedArray: ; // 提示for (int i 0; i arr.size(); i) std::cout arr[i] ; // 输出std::cout \n; // 换行}// // 9. 命名空间// namespace MyLib { // 自定义命名空间int value 100; // 命名空间内的变量void print() { // 命名空间内的函数std::cout MyLib::print, value value \n; // 输出}}void demo_namespace() { // 演示命名空间std::cout \n 命名空间 \n; // 分组标题MyLib::print(); // 用限定名调用using MyLib::value; // 用 using 引入单个名字std::cout value value \n; // 直接使用}// // 10. 字符串流// void demo_stringstream() { // 演示字符串流std::cout \n 字符串流 \n; // 分组标题std::ostringstream oss; // 输出字符串流oss x 42 , y 3.14; // 拼接内容std::cout oss.str() \n; // 取出字符串并输出std::istringstream iss(123 456); // 输入字符串流int a, b; // 两个整数iss a b; // 从流中解析std::cout a a , b b \n; // 输出}// // main// int main() { // 程序入口std::cout C03 特性演示 \n\n; // 标题demo_value_init(); // 演示值初始化demo_aggregate(); // 演示聚合初始化demo_ebo(); // 演示空基类优化demo_exception(); // 演示异常规范demo_auto_ptr(); // 演示 auto_ptrdemo_stl(); // 演示 STL 与函数对象demo_raii(); // 演示 RAIIdemo_template(); // 演示模板demo_namespace(); // 演示命名空间demo_stringstream(); // 演示字符串流std::cout \n 演示结束 \n; // 结束提示return 0; // 正常返回}

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑