find_if() in list

Hi,
i have a ques...
is it possible to implement a function, member function of Library, that gets a long id; and returns Student stud; that has the same id, using find_if() or find() ? (from list<Student> students;)

thnx a lot.


Library.h:
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
#ifndef _LIBRARY_H_
#define _LIBRARY_H_

#include <iostream>
#include <string>
#include <time.h>
#include <list>
#include <algorithm>
#include "Student.h"
#include "LibraryItem.h"

using namespace std;

class Library {
public:
	//Consturcor
	Library(time_t _today ){
	today = time(NULL);
	}
	void addTitle(LibraryItem* item);
	void addStudent(Student student);
	void borrowTitle(const long id, const long catNum);
	void returnTitle(const long id, const long catNum);
	void endDay(){};
	void clearFines(const long id);
	//Distructor
	~Library();
private:
	time_t today;
	list<Student> students;
	list<LibraryItem*> items;
};
#endif 


Student.h
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
#ifndef _STUDENT_H_
#define _STUDENT_H_

#include <iostream>
#include <string>
#include <list>
#include <algorithm>
#include "LibraryItem.h"

using namespace std;
class Student {
public:
	//Consturcor
	Student(long _id = 0 , string _name = ""): id(_id), name(_name), totalFine(0) {}
	long getId() const { 
		return id;
	}

	void setFine(double fine){
		totalFine = fine;
	}

	bool findInLoanList(const long id){
		list<LibraryItem*>::iterator it = loanList.end();
		if (it == find(loanList.begin(),loanList.end(),id))
			return false;
		return true;
	}

	void addToLoanList(LibraryItem* item){
		loanList.push_back(item);
	}

	void removeFromLoanList(LibraryItem* item){
		loanList.remove(item);
	}
	//Distructor
	~Student() {loanList.clear();}
private:
	const long id;
	const string name;
	double totalFine;
	list<LibraryItem*> loanList;
};
#endif 
Yep:

C++11
1
2
3
4
5
long searchId = 1;
student = find_if(student_list.begin(), studen_list.end(), 
                          [searchId](const Student& stud)
                                {return stud.getId()==searchId}
                         );


C++ 03

1
2
3
4
5
6
7
8
9
10
11
12
struct searchHelper
{
long id;
searchHelper(long id) : id(id) {}
bool operator()(const Student& stud)
{
return stud.getId() == this->id;
}
};

long searchId = 1;
student = find_if(studen_list.begin(), student_list.end(), searchHelper(searchId));
Last edited on
thnx man, it helped.
i have another brobelm while compiling the program.
i`m getting this error:
1>Library.obj : error LNK2019: unresolved external symbol "public: __thiscall StudentNotFound::StudentNotFound(void)" (??0StudentNotFound@@QAE@XZ) referenced in function "public: void __thiscall Library::clearFines(long)" (?clearFines@Library@@QAEXJ@Z)

what is that ?

exceptions.h:
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
#include <exception>


class DuplicateItemAdding: public exception {
	long index;
public:	
	DuplicateItemAdding(long i): index(i){}
	virtual const char* what() const {
		return "library cannot add item with the same id twice";
	}
	long getIndex() const { 
		return index;
	}
};

class DuplicateStudentAdding: public exception {
	long index;
public:	
	DuplicateStudentAdding(long i): index(i){}
	virtual const char* what() const {
		return "library cannot add title with the same catalogid twice";
	}
	long getIndex() const { 
		return index;
	}
};

class StudentNotFound: public exception {
public:
	StudentNotFound();
	virtual const char* what() const {
		return "Student NOT exists";
	}
};

class ItemNotFound: public exception {
public:
	ItemNotFound();
	virtual const char* what() const {
		return "Item NOT exists";
	}
};

class ItemAlreadyLoaned: public exception {
public:
	ItemAlreadyLoaned();
	virtual const char* what() const {
		return "Item Already Borrowed";
	}
};

class AlreadyBorrowedByStudent: public exception {
public:
	AlreadyBorrowedByStudent();
	virtual const char* what() const {
		return "lready borrowed";
	}
};

class NotBorrowedByStudent: public exception {
public:
	NotBorrowedByStudent();
	virtual const char* what() const {
		return "Item is not borrowed by student";
	}
};



Last edited on
Topic archived. No new replies allowed.