c++ primer 学习笔记-第二章
2015-07-26 09:36
513 查看
一、auto 与 decltype 的讲解:
1.auto
①auto定义的变量必须有初始值;
②auto可在一条语句中声明多个变量,但只能有一个基本数据类型;
③auto会忽略顶层const,保留底层const(复制不会修改原对象,顶层const忽略后不会产生影响;引用是原对象的别名,必须保留顶层const才能保证不修改原对象);
④用于声明引用的const都是底层const;
2.decltype
①如果decltype使用的表达式是一个变量,则返回该变量的类型(包括顶层const和引用在内);
②如果使用的是一个引用,则必须初始化;
③如果decltype使用的表达式不是一个变量,则返回表达式结果对应的类型;
④如果表达式的内容是解引用操作或是变量名加上一对括号,则得到引用。
两者之间的区别:
①赋值时auto会忽略顶层const,如果希望推断出的auto是顶层const,需要明确指出;引用时保留顶层const;而decltype会保留顶层const;
②decltype多了个针对非变量表达式、解引用和加括号变量的特定使用。
所以对于习题2-38:
例子1:
例子2:
【习题】
2.41
①
②
③其余略
1.auto
①auto定义的变量必须有初始值;
②auto可在一条语句中声明多个变量,但只能有一个基本数据类型;
③auto会忽略顶层const,保留底层const(复制不会修改原对象,顶层const忽略后不会产生影响;引用是原对象的别名,必须保留顶层const才能保证不修改原对象);
④用于声明引用的const都是底层const;
2.decltype
①如果decltype使用的表达式是一个变量,则返回该变量的类型(包括顶层const和引用在内);
②如果使用的是一个引用,则必须初始化;
③如果decltype使用的表达式不是一个变量,则返回表达式结果对应的类型;
④如果表达式的内容是解引用操作或是变量名加上一对括号,则得到引用。
两者之间的区别:
①赋值时auto会忽略顶层const,如果希望推断出的auto是顶层const,需要明确指出;引用时保留顶层const;而decltype会保留顶层const;
②decltype多了个针对非变量表达式、解引用和加括号变量的特定使用。
所以对于习题2-38:
例子1:
int i = 0; auto m = 1; decltype(i) n = 1;
例子2:
const int i = 0; auto m = 1; decltype(i) n = 1;
【习题】
2.41
①
#include <iostream> #include <string> struct Sales_data { std::string bookIsbn; unsigned units_sold = 0; double price = 0.0; double revenue = 0.0; }; void input(Sales_data &book); void display(Sales_data book); int addBook(Sales_data &bookSum, Sales_data &bookNew); void practice1_21(Sales_data &book1, Sales_data &book2); int main(){ std::cout << "this is a test:" << std::endl; Sales_data book1, book2; practice1_21(book1, book2); getchar(); getchar(); return 0; } void input(Sales_data &book) { std::cout << "Please input in the format: 编号 已售数量 单价" << std::endl; std::cin >> book.bookIsbn >> book.units_sold >> book.price; book.revenue = book.units_sold*book.price; } void display(Sales_data book) { std::cout << "编号:"<<book.bookIsbn << " " << "已售:"<<book.units_sold << " " << "平均价格:" << book.price << " "<< "总利润:"<< book.revenue << std::endl; } int addBook(Sales_data &bookSum, Sales_data &bookNew)//求和后的price不再表示单价 而是平均价格 { if (bookSum.bookIsbn == bookNew.bookIsbn) { bookSum.units_sold += bookNew.units_sold; bookSum.revenue += bookNew.revenue; if (bookSum.units_sold != 0) { bookSum.price = bookSum.revenue / bookSum.units_sold; } else { bookSum.price = 0; } return 0; } else { std::cout << "different books" << std::endl; return -1; } } void practice1_21(Sales_data &book1, Sales_data &book2) { input(book1); input(book2); addBook(book1, book2); display(book1); }
②
#include <iostream> #include <string> struct Sales_data { std::string bookIsbn; unsigned units_sold = 0; double price = 0.0; double revenue = 0.0; }; int input(Sales_data &book); void display(Sales_data book); int addBook(Sales_data &bookSum, Sales_data &bookNew); void practice1_21(Sales_data &book1, Sales_data &book2); void practice1_22(Sales_data &bookSum, Sales_data &bookNew); int main(){ std::cout << "this is a test:" << std::endl; Sales_data bookSum, bookNew; practice1_22(bookSum, bookNew); getchar(); getchar(); return 0; } int input(Sales_data &book) { std::cout << "Please input in the format: 编号 已售数量 单价" << std::endl; if (std::cin >> book.bookIsbn) { std::cin >> book.units_sold >> book.price; book.revenue = book.units_sold*book.price; return 1; } else { return 0; } } void display(Sales_data book) { std::cout << "编号:"<<book.bookIsbn << " " << "已售:"<<book.units_sold << " " << "平均价格:" << book.price << " "<< "总利润:"<< book.revenue << std::endl; } int addBook(Sales_data &bookSum, Sales_data &bookNew)//求和后的price不再表示单价 而是平均价格 { if (bookSum.bookIsbn == bookNew.bookIsbn) { bookSum.units_sold += bookNew.units_sold; bookSum.revenue += bookNew.revenue; if (bookSum.units_sold != 0) { bookSum.price = bookSum.revenue / bookSum.units_sold; } else { bookSum.price = 0; } return 0; } else { std::cout << "different books" << std::endl; return -1; } } void practice1_21(Sales_data &book1, Sales_data &book2) { input(book1); input(book2); addBook(book1, book2); display(book1); } void practice1_22(Sales_data &bookSum, Sales_data &bookNew) { input(bookSum); while (input(bookNew)) { addBook(bookSum, bookNew); } display(bookSum); }
③其余略
相关文章推荐
- 一起talk C栗子吧(第二十三回:C语言实例--队列二)
- C语言缺陷与陷阱(3)
- c++ 复杂类型指针 类指针 浅谈
- C++生产和使用的临时对象
- 对于程序性能优化的理解
- 对于程序性能优化的理解
- C++面向对象编程(五)同名成员变量和同名函数
- 【C++探索之旅】第一部分第三课:第一个C++程序
- 【C++探索之旅】第一部分第二课:C++编程的必要软件
- C++运算符详解问题详解
- 《C++编程思想》(第二版)第3章 C++中的C(笔记、习题及答案)(二)
- 基于Qt5.5.0的sql,C++备忘录软件的编写
- 浅谈C语言嵌入式系统编程注意事项
- C语言int类型指针指向char类型变量简单实例
- C语言中对文件的操作
- 深入搜索C++对象模型------构造函数
- C++学习,总结一
- C++11 -----一切从lambda说起
- C++实现禁忌搜索解决TSP问题
- C语言中数组与指针的结合