您的位置:首页 > 编程语言 > C语言/C++

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:

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


③其余略
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: