Program to find largest and smallest among 5 numbers without using array

前端 未结 15 2245
遇见更好的自我
遇见更好的自我 2021-01-06 07:25

Yesterday I went for an interview where I have been asked to create a program to find largest and smallest among 5 numbers without using array.

I know how to create

15条回答
  •  旧巷少年郎
    2021-01-06 07:59

    You could use list (or vector), which is not an array:

    #include
    #include
    #include
    using namespace std;
    int main()
    {
        list l;
        l.push_back(3); 
        l.push_back(9); 
        l.push_back(30);    
        l.push_back(0); 
        l.push_back(5); 
    
        list::iterator it_max = max_element(l.begin(), l.end());
        list::iterator it_min = min_element(l.begin(), l.end());
    
        cout << "Max: " << *it_max << endl;
        cout << "Min: " << *it_min << endl;
    }
    

提交回复
热议问题