C++: no matching function for call to 'min'

I have coded this program, but it returns the error: no matching function for call to 'min'.

I believe that I have declared the correct libraries and the only possibility of error is that there is a type error mismatch.

However, I don't know why an array and integer cannot be in a min function.

Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>

using namespace std;

int main() {
    ifstream fin("test.in");
    ofstream fout("test.out");

    int k, n; fin >> k >> n; 
    int data[10][20];
    int data1[10][20];
    int minimum[20];
    int count = 0;

    for(int i = 0; i < k; i++) {
        for(int j = 0; j < n; j++) {
            fin >> data[i][j];
        }
    }

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < k; j++) {
            data1[i][j] = data[j][i];
        }
    }

   for(int i = 0; i < n; i++) {
        sort(data1[i], data1[i]+k);
    }

    for(int i = 0; i < n; i++) {
        minimum[i] = min(data1[i], data1[i]+k);
    }

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < k; j++) {
            if(minimum[i] > minimum[i+1]) {
                count++;
            }
        }
    }

    fout << count;
}

Last edited on
std::min() returns the minimum value between two values. It doesn't take two iterators and give the minimum value in a sequence. If you want that function you'll have to code it yourself.
Remember to handle the case of an empty sequence.
Line 35: Since you sorted it previously it is always data1[i][0]

Line 39: What is this loop good for? You don't use j at all.
std::min_element() works with iterators
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    const int LIMIT{5};
    
    int data[LIMIT]{4,3,5,1,2};
    int minimum[LIMIT]{0};
    
    
    int COUNT{0};for(int i = 0; i < LIMIT - 1; i++)
    {
        minimum[i] = std::min(data[i], data[i + 1]);
        std::cout << data[i] << '-' << data[i + 1] << " ->> " << minimum[i] << '\n';
        COUNT++;
    }
    
    for(int i = 0; i < COUNT; i++)
        std::cout << minimum[i]  << '\n';
    
    return 0;
}


4-3 ->> 3
3-5 ->> 3
5-1 ->> 1
1-2 ->> 1
3
3
1
1
Program ended with exit code: 0
Topic archived. No new replies allowed.