Binary search template to class

So far i can do basic template binary search to any type. But i'm stuck when i have to search for objects. I don't know which operator shall i overload to make my code works. Anybody can help me? This is what i did

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
using namespace std;

class Age
{
    private:
        int age;

    public:
        int operator = (int);

};

int Age::operator=(int num)
{
    age = num;
}


// iterative
template <class T>
T bsearch(T a[], int sizee, T x)
{
    int low = 0;
    int high = sizee -1;

    while(low <= high)
    {
        int mid = (low+high)/2;

        if(x < a[mid])
        high = mid - 1;

        else if(x > a[mid])
        low = mid + 1;
        else

        return a[mid];
    }
    return -1;
}

void print(int val)
{
    if(val == -1)
        cout << "not found" << endl;

    else
        cout << "found" << endl;

}

int main()
{
    int a[5] = {3, 7, 9, 16, 23};
    char b[5] = {'a', 'b', 'c', 'd', 'e'};
    float c[5] = {0.1, 0.2, 0.3, 0.4, 0.5};
    Age people = {3,6,8,11,22};


    int arraySize = 5;
    int result;
    float input;

    result = bsearch(a, arraySize, 7);
    print(result);
    result = bsearch(a, arraySize, 16);
    print(result);
    result = bsearch(a, arraySize, 77);
    print(result);

    cout << endl << endl;

    result = bsearch(b, arraySize, 'a');
    print(result);
    result = bsearch(b, arraySize, 'z');
    print(result);
    result = bsearch(b, arraySize, 'e');
    print(result);

    cout << endl << endl;

    input = 0.11;
    result = bsearch(c, arraySize, input);
    print(result);

    input = 0.7;
    result = bsearch(c, arraySize, input);
    print(result);

    input = 0.1;
    result = bsearch(c, arraySize, input);
    print(result);

    cout << endl << endl;

    result = bsearch(people, arraySize, 22);
    print(result);

    return 0;
}
Your bsearch function returns T and then you have a statment return -1 (line 40) which is certainly not type T. Also I can see that your POD arrays are in ascending order but the order of Age[] is undefined as none of the relational operators have been overloaded, you have overloaded the assignment operator which is not the same thing. And, class Age needs another data member to be interesting, otherwise if it's sole data member is type int, as in your example, then it's a 1-to-1 mapping with the ordering of POD int and objects of this class. I have therefore extended your framework to class Person with two data members - name and age - and a binary search with an int/age key i.e. in an array of Person's you want to see if there's anyone of that age. You could also search by name of course if you wish after making the corresponding changes in your code.

Full code is given below, please read, research and if something is still unclear do come back here:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<iostream>
using namespace std;

class Person
{
	private:
		string m_name;
		int m_age;
	public:
	    Person(){}//default ctor;
		Person(string name, int age) : m_name(name), m_age(age){}//member initialization list ctor;
		~Person(){}//defined destructor;
		Person& operator = (const Person& rhs);//copy assignment operator;
		Person (const Person& rhs);//copy ctor;
		bool operator < (const Person& rhs)const;
		bool operator < (int n)const;
		bool operator == (int n)const;
		bool operator > (int n)const;
		friend ostream& operator<<(ostream& os, const Person& p);
};
Person& Person::operator = (const Person& rhs)
{
    if(this != &rhs)
    {
        m_name = rhs.m_name;
        m_age = rhs.m_age;
    }
    return *this;
}
Person::Person (const Person& rhs)
{
    m_name = rhs.m_name;
    m_age = rhs.m_age;
}
bool Person::operator < (const Person& rhs)const
{
	return (m_age <=  rhs.m_age);
}
bool Person::operator < (int n)const
{
    return (m_age < n);
}
bool Person::operator == (int n)const
{
    return (m_age == n);
}
bool Person::operator > (int n)const
{
    return (m_age > n);
}
ostream& operator<<(ostream& os, const Person& p)
{
    os<<"The person is: "<<p.m_name<<" with age equal to: "<<p.m_age<<"\n";
    return os;
}
template <typename T>
void sort(T array[], size_t n)
{
    for (size_t i = 0; i < n; i++)
    {
        for(size_t j = 0; j < n; j++)
        {
            if(array[j] < array[i])
            {
                T temp;
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
}
template <typename T>
void bsearch (T a[], size_t n, int x)
{
    size_t left = 0;
    size_t right = n - 1;
    int match = 0;
    while(left <= right)
    {
        size_t middle = (left + right) / 2;
        if(a[middle] == x)
        {
            cout<<"Success! \n";
            cout<<a[middle];
            match++;
            break;
        }
        if(a[middle] < x)
        {
            left = middle + 1;
        }
        else
        {
            right = middle - 1;
        }
    }
    if(match == 0)
    {
        cout<<"Failure! \n";
    }
}

int main()
{
    Person a("John", 43), b("Sam", 12), c("Jerry", 89);
    Person array[3];
    array[0] = a;
    array[1] = b;
    array[2] = c;
    sort(array, 3);
    bsearch(array, 3, 43);
}
Topic archived. No new replies allowed.