初始值设定项列表和基于范围的函数
Initializer list and range-based function
问题:编写一个程序,提示用户输入 1、2、5、10、20、50 和 100 美元的钞票。单独查询用户每个大小的美元数。
我认为我已经很好地完成了代码,但问题的最后一部分指出:修改程序以对向量使用初始值设定项列表,并使用基于范围的 for 语句遍历向量。注意:这些 C++11 功能在 g++-4.6 和 Visual Studio 2012 中可用,但在其他编译器上可能不可用。
我不明白最后一部分在问什么。希望这里有人能为我指出正确的方向。这是一个C++的课程,我的书没有最新版本。
对我的代码格式的任何批评也很棒!
这是我的代码:
#include "std_lib_facilities_3.h"
vector<int>bills;
int bill;
int bill_tot(){
int sum =
1*bills[0]+2*bills[1]+5*bills[2]+10*bills[3]+20*bills[4]+50*bills[5]+100*bills[6];
return sum;
}
int main()
try{
cout << "Enter the number of One Dollar Bills you have: ";
cin >> bill;
cout << 'n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.n");
cout << "Enter the number of Two Dollar Bills you have: ";
cin >> bill;
cout << 'n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.n");
cout << "Enter the number of Five Dollar Bills you have: ";
cin >> bill;
cout << 'n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.n");
cout << "Enter the number of Ten Dollar Bills you have: ";
cin >> bill;
cout << 'n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.n");
cout << "Enter the number of Twenty Dollar Bills you have: ";
cin >> bill;
cout << 'n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.n");
cout << "Enter the number of Fifty Dollar Bills you have: ";
cin >> bill;
cout << 'n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.n");
cout << "Enter the number of One Hundred Dollar Bills you have: ";
cin >> bill;
cout << 'n';
bills.push_back(bill);
if (bill<0)
error("You can't have a negative value.n");
cout << "You have " << bills[0] <<" One Dollar Bills.n";
cout << "You have " << bills[1] <<" Two Dollar Bills.n";
cout << "You have " << bills[2] <<" Five Dollar Bills.n";
cout << "You have " << bills[3] <<" Ten Dollar Bills.n";
cout << "You have " << bills[4] <<" Twenty Dollar Bills.n";
cout << "You have " << bills[5] <<" Fifty Dollar Bills.n";
cout << "You have " << bills[6] <<" One Hundred Dollar Bills.n";
cout << "The value of all your Dollar Bills are $" << bill_tot() << endl;
keep_window_open();
}
catch (runtime_error e) {
cout << e.what() << 'n';
keep_window_open();
}
这是说明性代码:
std::vector<int> denomination { 1,2,5,20,50,100}; // Initializer list for vector
int sum = 0;
int number;
for ( int bill : denomination ) // range-based for loop
{
cout << "Enter the number of $" << bill << " you have: ";
cin >> number;
sum += number * bill;
}
相关文章:
- 为什么我的变量存在于其范围之外
- set_intersection范围范围
- 满足标准::范围::范围概念
- 向量向量的范围范围
- C++参数类型以接受适用于基于范围的 for 循环的所有序列
- 代码具有特定于函数的变量,用于更改范围之外的值
- 将自适应阈值应用于范围函数 opencv c++
- 用辅助方法范围范围的问题来操纵字符串
- C++:将参数应用于函数范围
- C++11:用容器参数定义函数(类似于基于范围的)
- 在C /STL中,是否有相当于Python范围()的紧凑型
- 用于循环范围范围
- 如何制作增强多指数复合范围语句,等效于x和y
- std::copy_n 是否适用于重叠范围?
- 依赖于模板参数的范围查找
- 标准算法 any_of()、all_of() 和 none_of() 应用于空范围
- 常数,但仅适用于此范围的剩余部分
- 确定谓词是否适用于一个范围的全部、部分或全部元素
- 延长临时的生命周期,适用于块范围的聚合,但不是通过"新";为什么?
- 矢量超出范围/范围检查