template sort function

Hi,

I tried to write the sort function for vector <Record>
but I not complete it.
Can you help.

thanks in advance..

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
class Record{
	int count;
	int price;
	public:
		Record(int price1, int count1 = 0):price(price1),count(count1){}

		friend bool operator>(const Record &r1, const Record &r2);
		friend ostream &operator<<(ostream &os,  const Record &r);
		
	};
	bool operator >(const Record &r1, const Record &r2)
	{
		if (r1.price > r2.price)
			return true;
		else
			return false;

	}

	ostream &operator<<(ostream &os,  const Record &r)
	{

		return os << "(" << r.price << ")" << endl;
	}

	


	
	bool cmp(Record &r1, Record &r2)
	{
		if (r1 > r2)
			return true;
		return false;
	}



template<class Iter, class functor>
void sorts(Iter beg, functor f)
{
		for (int k = 0; k < size -1; k++){
			for (int i = 0; i < size -1 -k; i++)
				if (f(it[i], iter[i + 1]))
			swap(iter[i],iter[i + 1]);
		}
}
Why would you do that? Just use std::sort. There is a version of it that takes a functor or predicate and it works for c-arrays and std sequence containers and it will be more efficient than a bubble sort.

kempofighter (1070) Dec 15, 2010 at 11:25pm
Why would you do that? Just use std::sort. There is a version of it that takes a functor or predicate and it works for c-arrays and std sequence containers and it will be more efficient than a bubble sort.


I want to write myself.To see how it is

I wrote the following.

1
2
3
4
5
6
7
8
9
10
11
12
template<class Iter, class functor>
void sorts(Iter beg, Iter end, functor f)
{  
	int size = end - beg;

	for (int k = 0; k < size -1; k++){
	   for (int i = 0; i < size -1 -k; i++)
	     if (f(beg[i], beg[i + 1]))
	         swap(beg[i],beg[i + 1]);
		
              }
}
Topic archived. No new replies allowed.