当前位置: 首页>>代码示例>>C++>>正文


C++ Ticket::push_back方法代码示例

本文整理汇总了C++中Ticket::push_back方法的典型用法代码示例。如果您正苦于以下问题:C++ Ticket::push_back方法的具体用法?C++ Ticket::push_back怎么用?C++ Ticket::push_back使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Ticket的用法示例。


在下文中一共展示了Ticket::push_back方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Lotto

Ticket Lotto(int Tot, int Ran) { // generate and return randomized & sorted array of lottery numbers
    Ticket temp; // create a ticket that will be returned once generated with random lottery numbers with respect to arguments passed to function
    int i = 0;

    if (Tot < 5) { // check for the first argument passed to function
        cout << "Invalid number range, resetting to default: [1-5]" << endl; // if range of lottery number max is below 5, change it to min allowed - 5
        Tot = 5;
    }

    if (Ran < 2) { // check the second argument, if less than 2, set it to default value of 2, we need at least 2 winning numbers to play with
        cout << "Invalid ticket request, setting number of winning numbers to default - 2." << endl;
        Ran = 2;
    }

    for (i = 0; i < Tot; i++) // generate non-random sequence of numbers from 1 to Tot and store it in vector<int> array
        temp.push_back(i+1);

    for (i = 0; i < Ran; i++) // shuffle the numbers "Ran" many times to have better randomization of numbers
        random_shuffle(temp.begin(), temp.end());

    temp.erase(temp.begin()+Ran, temp.end()); // erase unnecessary numbers from Ran+, numbers from 1 to Ran are kept in.
    sort(temp.begin(), temp.end()); // finally, sort the numbers

    return temp; // and return the sorted and randomized array
}
开发者ID:marcialwushu,项目名称:C---Primer-Plus-Exercises-Solutions,代码行数:25,代码来源:main.cpp


注:本文中的Ticket::push_back方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。