Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <fstream>
- // nisam ukljucio using namespace std, a string se nalazi u std namespace, pa moram ispred string pisati std ::
- class Student
- {
- private:
- std::string name;
- int points, index;
- public:
- Student(const std :: string& name = "", const int points = 0, const int index = 0) :
- name(name), points(points), index(index) {} //konstruktor sa podrazumijevanim parametrima, zamjena za prazan i sa parametrima
- //inace bi morali praviti posebno prazan, a posebno sa parametrima
- //ovako jednim udarcem dvije muve
- std::string get_name() const {return name;}
- int get_points() const {return points;}
- int get_index() const {return index;}
- void set_name(const std::string& name) {this->name = name;} //ako hocemo ista imena da koristimo, moramo preko this pokazivaca
- void set_points(const int points) {this->points = points;}
- void set_index(const int index) {this->index = index;}
- friend bool operator < (const Student& s1, const Student& s2) //prijateljska funkcija, moze koristiti polja iz private
- { //prototip funkcije mora biti u publicu, a tijelo moze a ne mora biti unutra, moze i van...
- if(s1.points != s2.points)
- {
- return s1.points < s2.points; //praksa je da ako su funkcije inline, tijelo bude zajedno sa prototipom, a ako su duz van..
- }
- else
- {
- return s1.index < s2.index;
- }
- }
- friend std::ostream& operator << (std::ostream& out, const Student& s)
- {
- out << s.name << ": points=" << s.points << ", index=" << s.index;
- return out;
- }
- };
- void print_list(std::vector<Student>& lst)
- {
- std::vector<Student>::iterator it;
- for(it = lst.begin(); it != lst.end(); ++it)
- std::cout << *it << std::endl;
- std::cout << std::endl;
- }
- void scan_infile(std::vector<Student>& lst)
- {
- std::string name;
- int points, index;
- std::ifstream infile;
- infile.open("students.txt");
- if (infile.is_open())
- while(!infile.eof())
- {
- infile >> name >> points >> index;
- lst.push_back(Student(name,points,index));
- }
- }
- int main()
- {
- std::vector<Student> st;
- scan_infile(st);
- print_list(st);
- std::sort(st.begin(), st.end());
- print_list(st);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement