c++ - 初始化列表和基于范围的函数

标签 c++ vector c++11 iostream initializer-list

问题: 编写一个程序,提示用户输入面值分别为 1、2、5、10、20、50 和 100 的美元钞票。分别向用户查询每个尺寸美元的数量。

我认为我已经很好地完成了代码,但问题的最后一部分指出: 修改您的程序以使用 vector 的初始化列表,并使用基于范围的 for 语句遍历 vector 。注意:这些 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;
}

关于c++ - 初始化列表和基于范围的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12700660/

相关文章:

c++ - 我正在尝试使用 stoi(),但编译器正在尝试给我替换并且不会编译

c++ - NaCl 和共享内存

c++ - 外部全局变量仍未在此范围内声明

javascript - 根据circle1的速度计算两个圆接触的点

c++ - 获取 std::vector<std::string>::iterator 的索引

c++ - Template模板函数及参数推导

c++从特定位置在其他 vector 中插入 vector

c++ - 当引用指向父类的指针时,对象会丢失其成员的数据

c++ - 关于在omnetpp.ini中将两个不同的应用程序随机分配到50个节点

javascript - Openlayers 3 geojson 与矢量源