c++ primer 学习笔记-第二章

一、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:

        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);
}

③其余略

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值