第一章 快速入门


C++ Primer 中文版,第4版

/*

第一章 快速入门
第二章 变量和基本类型
第三章 标准库类型
第四章 数组和指针
第五章 表达式
第六章 语句
第七章 函数
第八章 标准IO库
第九章 顺序容器
第十章 关联容器
第11章 泛型算法
第12章 类
第13章 复制控制
第14章 重载操作符与转换
第15章 面向对象编程
第16章 模板和泛型编程
第17章 用于大型程序的工具
第18章 特殊工具与技术

*/

/*

第1章 快速入门. 1
1.1 编写简单的C++程序 2
1.2 初窥输入/输出 5
1.2.1 标准输入与输出对象 5
1.2.2 一个使用IO库的程序 5
1.3 关于注释 8
1.4 控制结构 10
1.4.1 while语句 10
1.4.2 for语句 12
1.4.3 if语句 14
1.4.4 读入未知数目的输入 15
1.5 类的简介 17
1.5.1 Sales_item类 17
1.5.2 初窥成员函数 19
1.6 C++程序 21
小结 22
术语 22
第一部分 基本语言
第2章 变量和基本类型 29
2.1 基本内置类型 30
2.1.1 整型 30
2.1.2 浮点型 32
2.2 字面值常量 34
2.3 变量 38
2.3.1 什么是变量 39
2.3.2 变量名 40
2.3.3 定义对象 42
2.3.4 变量初始化规则 44
2.3.5 声明和定义 45
2.3.6 名字的作用域 46
2.3.7 在变量使用处定义变量 48
2.4 const限定符 49
2.5 引用 50
2.6 typedef名字 53
2.7 枚举 53
2.8 类类型 54
2.9 编写自己的头文件 57
2.9.1 设计自己的头文件 58
2.9.2 预处理器的简单介绍 60
小结 62
术语 62
第3章 标准库类型 67
3.1 命名空间的using声明 68
3.2 标准库string类型 70
3.2.1 string对象的定义和初始化 70
3.2.2 String对象的读写 71
3.2.3 string对象的操作 72
3.2.4 string对象中字符的处理 76
3.3 标准库vector类型 78
3.3.1 vector对象的定义和初始化 79
3.3.2 vector对象的操作 81
3.4 迭代器简介 83
3.5 标准库bitset类型 88
3.5.1 bitset对象的定义和初始化 88
3.5.2 bitset对象上的操作 90
小结 92
术语 92
第4章 数组和指针 95
4.1 数组 96
4.1.1 数组的定义和初始化 96
4.1.2 数组操作 99
4.2 指针的引入 100
4.2.1 什么是指针 100
4.2.2 指针的定义和初始化 101
4.2.3 指针操作 104
4.2.4 使用指针访问数组元素 106
4.2.5 指针和const限定符 110
4.3 C风格字符串 113
4.3.1 创建动态数组 117
4.3.2 新旧代码的兼容 120
4.4 多维数组 122
小结 124
术语 125
第5章 表达式 127
5.1 算术操作符 129
5.2 关系操作符和逻辑操作符 131
5.3 位操作符 134
5.3.1 bitset对象或整型值的使用 135
5.3.2 将移位操作符用于IO 137
5.4 赋值操作符 137
5.4.1 赋值操作的右结合性 138
5.4.2 赋值操作具有低优先级 138
5.4.3 复合赋值操作符 139
5.5 自增和自减操作符 140
5.6 箭头操作符 142
5.7 条件操作符 143
5.8 sizeof操作符 144
5.9 逗号操作符 145
5.10 复合表达式的求值 145
5.10.1 优先级 145
5.10.2 结合性 146
5.10.3 求值顺序 148
5.11 new和delete表达式 150
5.12 类型转换 154
5.12.1 何时发生隐式类型转换 154
5.12.2 算术转换 155
5.12.3 其他隐式转换 156
5.12.4 显式转换 158
5.12.5 何时需要强制类型转换 158
5.12.6 命名的强制类型转换 158
5.12.7 旧式强制类型转换 160
小结 161
术语 162
第6章 语句 165
6.1 简单语句 166
6.2 声明语句 167
6.3 复合语句(块) 167
6.4 语句作用域 168
6.5 if语句 169
6.6 switch语句 172
6.6.1 使用switch 173
6.6.2 switch中的控制流 173
6.6.3 default标号 175
6.6.4 switch表达式与case标号 176
6.6.5 switch内部的变量定义 176
6.7 while语句 177
6.8 for循环语句 179
6.8.1 省略for语句头的某些部分 180
6.8.2 for语句头中的多个定义 181
6.9 do while语句 182
6.10 break语句 183
6.11 continue语句 184
6.12 goto语句 185
6.13 try块和异常处理 186
6.13.1 throw表达式 186
6.13.2 try块 187
6.13.3 标准异常 189
6.14 使用预处理器进行调试 190
小结 192
术语 192
第7章 函数 195
7.1 函数的定义 196
7.1.1 函数返回类型 197
7.1.2 函数形参表 198
7.2 参数传递 199
7.2.1 非引用形参 199
7.2.2 引用形参 201
7.2.3 vector和其他容器类型的形参 206
7.2.4 数组形参 206
7.2.5 传递给函数的数组的处理 209
7.2.6 main:处理命令行选项 210
7.2.7 含有可变形参的函数 211
7.3 return语句 211
7.3.1 没有返回值的函数 212
7.3.2 具有返回值的函数 212
7.3.3 递归 216
7.4 函数声明 217
7.5 局部对象 220
7.5.1 自动对象 220
7.5.2 静态局部对象 220
7.6 内联函数 221
7.7 类的成员函数 222
7.7.1 定义成员函数的函数体 223
7.7.2 在类外定义成员函数 225
7.7.3 编写Sales_item类的构造
函数 225
7.7.4 类代码文件的组织 227
7.8 重载函数 228
7.8.1 重载与作用域 230
7.8.2 函数匹配与实参转换 231
7.8.3 重载确定的三个步骤 232
7.8.4 实参类型转换 234
7.9 指向函数的指针 237
小结 239
术语 240
第8章 标准IO库 243
8.1 面向对象的标准库 244
8.2 条件状态 247
8.3 输出缓冲区的管理 249
8.4 文件的输入和输出 251
8.4.1 文件流对象的使用 251
8.4.2 文件模式 254
8.4.3 一个打开并检查输入文件的
程序 256
8.5 字符串流 257
小结 259
术语 259
第二部分 容器和算法
第9章 顺序容器 263
9.1 顺序容器的定义 264
9.1.1 容器元素的初始化 265
9.1.2 容器内元素的类型约束 267
9.2 迭代器和迭代器范围 268
9.2.1 迭代器范围 270
9.2.2 使迭代器失效的容器操作 271
9.3 顺序容器的操作 272
9.3.1 容器定义的类型别名 272
9.3.2 begin和end成员 273
9.3.3 在顺序容器中添加元素 273
9.3.4 关系操作符 277
9.3.5 容器大小的操作 278
9.3.6 访问元素 279
9.3.7 删除元素 280
9.3.8 赋值与swap 282
9.4 vector容器的自增长 284
9.5 容器的选用 287
9.6 再谈string类型 289
9.6.1 构造string对象的其他方法 290
9.6.2 修改string对象的其他方法 292
9.6.3 只适用于string类型的操作 293
9.6.4 string类型的查找操作 295
9.6.5 string对象的比较 298
9.7 容器适配器 300
9.7.1 栈适配器 301
9.7.2 队列和优先级队列 302
小结 303
术语 303
第10章 关联容器 305
10.1 引言:pair类型 306
10.2 关联容器 308
10.3 map类型 309
10.3.1 map对象的定义 309
10.3.2 map定义的类型 310
10.3.3 给map添加元素 311
10.3.4 使用下标访问map对象 311
10.3.5 map::insert的使用 313
10.3.6 查找并读取map中的元素 315
10.3.7 从map对象中删除元素 316
10.3.8 map对象的迭代遍历 316
10.3.9 “单词转换”map对象 317
10.4 set类型 319
10.4.1 set容器的定义和使用 319
10.4.2 创建“单词排除”集 321
10.5 multimap和multiset类型 322
10.5.1 元素的添加和删除 322
10.5.2 在multimap和multiset
中查找元素 323
10.6 容器的综合应用:文本查询程序 325
10.6.1 查询程序的设计 326
10.6.2 TextQuery类 327
10.6.3 TextQuery类的使用 328
10.6.4 编写成员函数 330
小结 332
术语 332
第11章 泛型算法 335
11.1 概述 336
11.2 初窥算法 339
11.2.1 只读算法 339
11.2.2 写容器元素的算法 341
11.2.3 对容器元素重新排序的算法 343
11.3 再谈迭代器 347
11.3.1 插入迭代器 348
11.3.2 iostream迭代器 349
11.3.3 反向迭代器 353
11.3.4 const迭代器 355
11.3.5 五种迭代器 356
11.4 泛型算法的结构 358
11.4.1 算法的形参模式 359
11.4.2 算法的命名规范 359
11.5 容器特有的算法 361
小结 362
术语 363
第三部分 类和数据抽象
第12章 类 367
12.1 类的定义和声明 368
12.1.1 类定义:扼要重述 368
12.1.2 数据抽象和封装 369
12.1.3 关于类定义的更多内容 372
12.1.4 类声明与类定义 374
12.1.5 类对象 375
12.2 隐含的this指针 376
12.3 类作用域 380
类作用域中的名字查找 382
12.4 构造函数 385
12.4.1 构造函数初始化式 387
12.4.2 默认实参与构造函数 391
12.4.3 默认构造函数 392
12.4.4 隐式类类型转换 393
12.4.5 类成员的显式初始化 396
12.5 友元 396
12.6 static类成员 398
12.6.1 static成员函数 400
12.6.2 static数据成员 400
小结 403
术语 403
第13章 复制控制 405
13.1 复制构造函数 406
13.1.1 合成的复制构造函数 409
13.1.2 定义自己的复制构造函数 409
13.1.3 禁止复制 410
13.2 赋值操作符 411
13.3 析构函数 412
13.4 消息处理示例 415
13.5 管理指针成员 419
13.5.1 定义智能指针类 421
13.5.2 定义值型类 425
小结 427
术语 427
第14章 重载操作符与转换 429
14.1 重载操作符的定义 430
14.2 输入和输出操作符 435
14.2.1 输出操作符<<的重载 435
14.2.2 输入操作符>>的重载 437
14.3 算术操作符和关系操作符 439
14.3.1 相等操作符 440
14.3.2 关系操作符 441
14.4 赋值操作符 441
14.5 下标操作符 442
14.6 成员访问操作符 443
14.7 自增操作符和自减操作符 446
14.8 调用操作符和函数对象 449
14.8.1 将函数对象用于标准库算法 450
14.8.2 标准库定义的函数对象 451
14.8.3 函数对象的函数适配器 453
14.9 转换与类类型 454
14.9.1 转换为什么有用 454
14.9.2 转换操作符 455
14.9.3 实参匹配和转换 458
14.9.4 重载确定和类的实参 461
14.9.5 重载、转换和操作符 464
小结 466
术语 467
第四部分 面向对象编程与泛型编程
第15章 面向对象编程 471
15.1 面向对象编程:概述 472
15.2 定义基类和派生类 473
15.2.1 定义基类 474
15.2.2 protected成员 475
15.2.3 派生类 476
15.2.4 virtual与其他成员函数 479
15.2.5 公用、私有和受保护的继承 482
15.2.6 友元关系与继承 486
15.2.7 继承与静态成员 486
15.3 转换与继承 487
15.3.1 派生类到基类的转换 487
15.3.2 基类到派生类的转换 489
15.4 构造函数和复制控制 490
15.4.1 基类构造函数和复制控制 490
15.4.2 派生类构造函数 490
15.4.3 复制控制和继承 494
15.4.4 虚析构函数 495
15.4.5 构造函数和析构函数中的虚函数 497
15.5 继承情况下的类作用域 497
15.5.1 名字查找在编译时发生 498
15.5.2 名字冲突与继承 498
15.5.3 作用域与成员函数 499
15.5.4 虚函数与作用域 500
15.6 纯虚函数 502
15.7 容器与继承 503
15.8 句柄类与继承 504
15.8.1 指针型句柄 505
15.8.2 复制未知类型 507
15.8.3 句柄的使用 508
15.9 再谈文本查询示例 511
15.9.1 面向对象的解决方案 513
15.9.2 值型句柄 514
15.9.3 Query_base类 515
15.9.4 Query句柄类 516
15.9.5 派生类 518
15.9.6 eval函数 520
小结 522
术语 523
第16章 模板与泛型编程 525
16.1 模板定义 526
16.1.1 定义函数模板 526
16.1.2 定义类模板 528
16.1.3 模板形参 529
16.1.4 模板类型形参 531
16.1.5 非类型模板形参 533
16.1.6 编写泛型程序 534
16.2 实例化 535
16.2.1 模板实参推断 537
16.2.2 函数模板的显式实参 540
16.3 模板编译模型 542
16.4 类模板成员 545
16.4.1 类模板成员函数 548
16.4.2 非类型形参的模板实参 551
16.4.3 类模板中的友元声明 552
16.4.4 Queue和QueueItem的友元
声明 554
16.4.5 成员模板 556
16.4.6 完整的Queue类 558
16.4.7 类模板的static成员 559
16.5 一个泛型句柄类 560
16.5.1 定义句柄类 561
16.5.2 使用句柄 562
16.6 模板特化 564
16.6.1 函数模板的特化 565
16.6.2 类模板的特化 567
16.6.3 特化成员而不特化类 569
16.6.4 类模板的部分特化 570
16.7 重载与函数模板 570
小结 573
术语 574
第五部分 高级主题
第17章 用于大型程序的工具 579
17.1 异常处理 580
17.1.1 抛出类类型的异常 581
17.1.2 栈展开 582
17.1.3 捕获异常 583
17.1.4 重新抛出 585
17.1.5 捕获所有异常的处理代码 586
17.1.6 函数测试块与构造函数 586
17.1.7 异常类层次 587
17.1.8 自动资源释放 589
17.1.9 auto_ptr类 591
17.1.10 异常说明 595
17.1.11 函数指针的异常说明 598
17.2 命名空间 599
17.2.1 命名空间的定义 599
17.2.2 嵌套命名空间 603
17.2.3 未命名的命名空间 604
17.2.4 命名空间成员的使用 606
17.2.5 类、命名空间和作用域 609
17.2.6 重载与命名空间 612
17.2.7 命名空间与模板 614
17.3 多重继承与虚继承 614
17.3.1 多重继承 615
17.3.2 转换与多个基类 617
17.3.3 多重继承派生类的复制控制 619
17.3.4 多重继承下的类作用域 620
17.3.5 虚继承 622
17.3.6 虚基类的声明 624
17.3.7 特殊的初始化语义 625
小结 628
术语 628
第18章 特殊工具与技术 631
18.1 优化内存分配 632
18.1.1 C++中的内存分配 632
18.1.2 allocator类 633
18.1.3 operator new函数和
operator delete函数 636
18.1.4 定位new表达式 638
18.1.5 显式析构函数的调用 639
18.1.6 类特定的new和delete 639
18.1.7 一个内存分配器基类 641
18.2 运行时类型识别 646
18.2.1 dynamic_cast操作符 647
18.2.2 typeid操作符 649
18.2.3 RTTI的使用 650
18.2.4 type_info类.. 652
18.3 类成员的指针 653
18.3.1 声明成员指针 653
18.3.2 使用类成员的指针 655
18.4 嵌套类 658
18.4.1 嵌套类的实现 658
18.4.2 嵌套类作用域中的名字查找 661
18.5 联合:节省空间的类 662
18.6 局部类 665
18.7 固有的不可移植的特征 666
18.7.1 位域 666
18.7.2 volatile限定符 668
18.7.3 链接指示:extern "C" 669
小结 672
术语 673
附录 标准库 675
索引... 703

*/
详细目录


/*

第一章 快速入门

第一部分:基本语言-------------------------------------------
第二章 变量和基本类型
第三章 标准库类型
第四章 数组和指针
第五章 表达式
第六章 语句
第七章 函数
第八章 标准IO库

第二部分:容器和算法----------------------------------------
第九章 顺序容器
第十章 关联容器
第11章 泛型算法

第三部分:类和数据抽象--------------------------------------
第12章 类
第13章 复制控制
第14章 重载操作符与转换

第四部分:面向对象编程与泛型编程--------------------------
第15章 面向对象编程
第16章 模板和泛型编程

第五部分:高级主题------------------------------------------
第17章 用于大型程序的工具
第18章 特殊工具与技术

*/

第一章  快速入门

// Sales_item.h

/*
 * This file contains code from "C++ Primer, Fourth Edition", by Stanley B.
 * Lippman, Jose Lajoie, and Barbara E. Moo, and is covered under the
 * copyright and warranty notices given in that book:
 * 
 * "Copyright (c) 2005 by Objectwrite, Inc., Jose Lajoie, and Barbara E. Moo."
 * 
 * 
 * "The authors and publisher have taken care in the preparation of this book,
 * but make no expressed or implied warranty of any kind and assume no
 * responsibility for errors or omissions. No liability is assumed for
 * incidental or consequential damages in connection with or arising out of the
 * use of the information or programs contained herein."
 * 
 * Permission is granted for this code to be used for educational purposes in
 * association with the book, given proper citation if and when posted or
 * reproduced.Any commercial use of this code requires the explicit written
 * permission of the publisher, Addison-Wesley Professional, a division of
 * Pearson Education, Inc. Send your request for permission, stating clearly
 * what code you would like to use, and in what specific way, to the following
 * address: 
 * 
 *     Pearson Education, Inc.
 *     Rights and Contracts Department
 *     75 Arlington Street, Suite 300
 *     Boston, MA 02216
 *     Fax: (617) 848-7047
*/ 

#ifndef SALESITEM_H
#define SALESITEM_H

// Definition of Sales_item class and related functions goes here


#include <iostream>
#include <string>

class Sales_item {
friend bool operator==(const Sales_item&, const Sales_item&);
// other members as before
public:
    // added constructors to initialize from a string or an istream
    Sales_item(const std::string &book):
              isbn(book), units_sold(0), revenue(0.0) { }
    Sales_item(std::istream &is) { is >> *this; }
    friend std::istream& operator>>(std::istream&, Sales_item&);
    friend std::ostream& operator<<(std::ostream&, const Sales_item&);
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);
    // other members as before
    
public:
    // operations on Sales_item objects
    double avg_price() const;
    bool same_isbn(const Sales_item &rhs) const
        { return isbn == rhs.isbn; }
    // default constructor needed to initialize members of built-in type
    Sales_item(): units_sold(0), revenue(0.0) { }
// private members as before
private:
    std::string isbn;
    unsigned units_sold;
    double revenue;

};


// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);

inline bool 
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
    // must be made a friend of Sales_item
    return lhs.units_sold == rhs.units_sold &&
           lhs.revenue == rhs.revenue &&
       lhs.same_isbn(rhs);
}

inline bool 
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
    return !(lhs == rhs); // != defined in terms of operator==
}

using std::istream; using std::ostream;

// assumes that both objects refer to the same isbn
inline
Sales_item& Sales_item::operator+=(const Sales_item& rhs) 
{
    units_sold += rhs.units_sold; 
    revenue += rhs.revenue; 
    return *this;
}

// assumes that both objects refer to the same isbn
inline
Sales_item 
operator+(const Sales_item& lhs, const Sales_item& rhs) 
{
    Sales_item ret(lhs);  // copy lhs into a local object that we'll return
    ret += rhs;           // add in the contents of rhs 
    return ret;           // return ret by value
}

inline
istream& 
operator>>(istream& in, Sales_item& s)
{
    double price;
    in >> s.isbn >> s.units_sold >> price;
    // check that the inputs succeeded
    if (in)
        s.revenue = s.units_sold * price;
    else 
        s = Sales_item();  // input failed: reset object to default state
    return in;
}

inline
ostream& 
operator<<(ostream& out, const Sales_item& s)
{
    out << s.isbn << "	" << s.units_sold << "	" 
        << s.revenue << "	" <<  s.avg_price();
    return out;
}

inline
double Sales_item::avg_price() const
{
    if (units_sold) 
        return revenue/units_sold; 
    else 
        return 0;
}


#endif
View Code

Code:

// 1、编写简单的C++程序 ------------------------------------------------



int main()
{
  return 0;
}


// 2、初窥输入/输出 ---------------------------------------------------


#include <iostream>
int main()
{
  std::cout << "Enter two numbers:" << std::endl; 
  int v1, v2;
  std::cin >> v1 >> v2;   
  std::cout << "The sum of " << v1 << " and " << v2
            << " is " << v1 + v2 << std::endl;
  return 0;
}


// 3、关于注释 --------------------------------------------------------------


/* Simple main function: Read two numbers and write their sum */
#include <iostream>
int main()
{
  // prompt user to enter two numbers
  std::cout << "Enter two numbers:" << std::endl; 
  int v1, v2;           //uninitialized
  std::cin >> v1 >> v2; // read input  
  return 0;
}



// 4、控制结构 -------------------------------------------------------------


// while
#include <iostream>
int main()
{
  int sum = 0, val = 1;
  // keep executing the while until val is greater than 10
  while(val <= 10)
  {
    sum += val; // assigns sum + val to sum
    ++val; // add 1 to val
  }
  std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
  return 0;
}


// for
#include <iostream>
int main()
{
  int sum = 0;
  // sum values from 1 up to 10 inclusive
  for(int val = 1; val <= 10; ++val)
    sum += val;
  // equivalent to sum = sum + val
  std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
  return 0;
}


// if
#include <iostream>
int main()
{
  std::cout << "Enter two numbers:" << std::endl;
  int v1, v2;
  std::cin >> v1 >> v2; // read input
  // use smaller number as lower bound for summation
  // and larger number as upper bound
  int lower, upper;
  if(v1 <= v2)
  {
    lower = v1;
    upper = v2;
  }
  else
  {
    lower = v2;
    upper = v1;
  }
  int sum = 0;
  // sum values from lower up to and including upper
  for(int val = lower; val <= upper; ++val)
    sum += val;
  // sum = sum + val
  std::cout << "Sum of " << lower << " to " << upper << " inclusive is " << sum
    << std::endl;
  return 0;
}


// end of file. Ctrl+z 可以模拟文件尾
#include <iostream>
int main()
{
  int sum = 0, value;
  // read till end-of-file, calculating a running total of all values read
  while(std::cin >> value)
    sum += value;
  // equivalent to sum = sum + value
  std::cout << "Sum is: " << sum << std::endl;
  return 0;
}


// 5、类的简介 ----------------------------------------------------

/*
必须将Sales_item.h文件,放在程序文件的同一目录下,
这个程序才可以运行。
Sales_item.h文件,目前不要求读者制作。
Sales_item.h文件具体内容,在最前面。
*/
#include <iostream>
#include "Sales_item.h"
int main()
{
  Sales_item book;
  // read ISBN, number of copies sold, and sales price
  std::cin >> book;
  // write ISBN, number of copies sold, total revenue, and average price
  std::cout << book << std::endl;
  return 0;
}
// input:   0-201-70353-X 4 24.99


// 对象之和
#include <iostream>
#include "Sales_item.h"
int main()
{
  Sales_item item1, item2;
  std::cin >> item1 >> item2; // read a pair of transactions
  std::cout << item1 + item2 << std::endl; // print their sum
  return 0;
}
/*

0-201-78345-X 3 20.00
0-201-78345-X 2 25.00

*/



// 成员函数
#include <iostream>
#include "Sales_item.h"
int main()
{
  Sales_item item1, item2;
  std::cin >> item1 >> item2;
  // first check that item1 and item2 represent the same book
  if(item1.same_isbn(item2))
  {
    std::cout << item1 + item2 << std::endl;
    return 0; // indicate success
  }
  else
  {
    std::cerr << "Data must refer to same ISBN" << std::endl;
    return  - 1; // indicate failure
  }
}
/*

0-201-78345-X 3 20.00
0-201-78345-X 2 25.00

0-201-78345-X 3 20.00
0-888-78345-X 2 25.00

*/



// 6、C++程序 ------------------------------------------------------

#include <iostream>
#include "Sales_item.h"
int main()
{
  //  declare variables to hold running sum and data for the next record
  Sales_item total, trans;
  //  is there data to process?
  if(std::cin >> total)
  {
    // if so, read the transaction records
    while(std::cin >> trans)
      if(total.same_isbn(trans))
    //  match: update the running total
        total = total + trans;
      else
    {
      //  no match: print & assign to total
      std::cout << total << std::endl;
      total = trans;
    }
    //  remember to print last record
    std::cout << total << std::endl;
  }
  else
  {
    //  no input!, warn the user
    std::cout << "No data?!" << std::endl;
    return  - 1; //  indicate failure
  }
  return 0;
}

TOP

原文地址:https://www.cnblogs.com/xin-le/p/4088018.html