programming using stl

this is the program of student list using stl finding maximum,min marks nd sorting on basis of ID ..max min are working fine but error ocuurs when i add aort.. plz someone help...
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
#include<list>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
class student
{
	int ID;
	char name[50];
	int marks;
	public:
	bool operator==(const student&) const;
	bool operator<(const student&) const;
	void add();
	void display();
	friend bool comp_ID(student s1,student s2);
friend bool comp_marks(student s1,student s2); 
};
bool comp_marks(student s1,student s2) 
{
	return s1.marks<s2.marks;
}

bool comp_ID(student s1,student s2)
{
	return s1.ID<s2.ID;
}
bool student::operator==(const student & s) const
{
	return(s.ID==ID);     
}

bool student::operator<(const student& s) const
{
	if(ID< s.ID)
		return true;      
	else return false;
}
void student::add()
{

	cout<<" enter ID: ";
	cin>>ID;
	cout<<" enter name: ";
	cin>>name;
	cout<<" enter marks: ";
	cin>>marks;

}

void student::display()
{ 
	cout<<"\n "<<ID<<" "<<name<<" "<<marks;
}
int main()
{
	list<student> li;
	list<student>::iterator it;
	student st;
	st.add();
	li.push_back(st);	
	st.add();
	li.push_back(st);	
	st.add();
	li.push_back(st);	
	st.add();
	li.push_back(st);	
	it=li.begin();
	while(it!=li.end())
	{
		st=*it;
		st.display();
		it++;
	}
	cout<<"\n STUDENT WITH MAXIMUM MARKS: ";
	max_element(li.begin(),li.end(),comp_marks)->display();
	cout<<"\n STUDENT WITH MINIMUM MARKS: ";
	min_element(li.begin(),li.end(),comp_marks)->display();
	cout<<"\n arranging student according to ID: ";
	sort(li.begin(),li.end(),comp_ID);
	it=li.begin();
	while(it!=li.end())
	{
		st=*it;
		st.display();
		it++;
	}
	return 0;
}
std::sort only works with random access iterators. std::list has its own sort function you can use li.sort(comp_ID);
Last edited on
thanks ur solution works fine but wat do u mean by random access iteraotrs , if i use the same code with vectors instead of list the template function sort is working fine please explain ??
Random access just means that you can access any element at an arbitrary position in equal time. std::list is a linked list so if we want to find an element at an specific position we need to step through each element until we reach the element we want, so list is not a random access container.

Random access iterators have some extra operations that is needed in std::sort. It would possible to implement these extra operations for the list iterators as well but then it would be very slow and std::sort on a std::list would be terrible slow. Instead it's much better to use a special sorting algorithm that is better suited for linked lists, and that's what std::list::sort is.
k.. thanks a lot ..
Topic archived. No new replies allowed.