C++ - Min value in queue

c++, queue, struct

Solution

Since you need a queue of integers the easiest solution is to use `std::deque<int>`. Then you could use `std::min_element` to find the minimum element in the queue:

std::deque<int> q{5, 1, 3};
std::deque<int>::iterator it = std::min_element(q.begin(), q.end());
std::cout << *it << std::endl;

By doing so, you do not need to use the `struct test`. This is especially true since it seems to just store an integer. If, on the other hand, `struct test` is more complex (having more fields) then you can use exactly the same approach but defining a compare function for `struct test` (see @fljx answer for an example of such a compare function).

If you can only use a `queue` you are restricted on the type of operations that you can do. Therefore, you would need to do something like:

std::queue<int> q;
/* fill queue ... */
int min_value = INT_MAX;
std::size_t size = q.size();
while (size-- > 0) {
    int x = q.front();
    q.pop();
    q.push(x);
    if (x < min_value)
        min_value = x;
}

Problem

I have queue from struct type ``` struct test { int numbers; }; queue<test> q; ``` how to find min value from: ``` q.front().numbers; ``` For example if in numbers have 5,1,3 I need to found 1.

Original source