Last active
June 1, 2022 18:30
-
-
Save drpventura/de677a8d8a83763fdf920599205ad3d3 to your computer and use it in GitHub Desktop.
Shows a class that is a wrapper over a vector of objects. Includes using STL copy, and find_if, along with C++11 for each loop. See video at https://youtu.be/usBts0ccGsw.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Student.h" | |
#include "Roster.h" | |
#include <iostream> | |
using namespace std; | |
int main() { | |
Student s1 {"Cecelia", 3.8 }; | |
Student s2 {"Sam", 3.1}; | |
// cout << s1 << endl; | |
// cout << s2 << endl; | |
Roster roster; | |
roster.add(s1); | |
roster.add(s2); | |
cout << roster << endl; | |
cout << roster.find("Cecelia") << endl; | |
cout << roster.find("Sam") << endl; | |
cout << roster.find("Max") << endl; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iterator> | |
#include <algorithm> | |
#include "Roster.h" | |
Roster::Roster() { } | |
void Roster::add(Student& student) { | |
students.push_back(student); | |
} | |
Student Roster::find(string name) const { | |
// for (int i = 0; i < students.size(); i++) { | |
// if (students.at(i).get_name() == name) { | |
// return students.at(i); | |
// } | |
// } // end for loop | |
// for (auto student : students) { | |
// if (student.get_name() == name) { | |
// return student; | |
// } | |
// } // end for loop | |
auto name_match_fn = [name](auto student) { | |
return student.get_name() == name; | |
}; | |
auto student_it = find_if(begin(students), end(students), name_match_fn); | |
if (student_it == end(students)) { | |
// no match found | |
return Student(); | |
} | |
else { | |
return *student_it; | |
} | |
} | |
/** | |
* Overloaded output operator for Roster. | |
* @param ostr the output stream to write to. | |
* @param roster the Roster to output. | |
* @return the (modified) ostream. | |
*/ | |
ostream& operator<<(ostream& ostr, const Roster& roster) { | |
ostr << "{ "; | |
// copy up to but not including the last element | |
// putting a "; " after each one | |
copy(begin(roster.students), end(roster.students) - 1, | |
ostream_iterator<Student>(ostr, "; ")); | |
// now output the last element (without a trailing "; ") | |
if (roster.students.size() > 0) { | |
ostr << roster.students.at(roster.students.size() - 1) << ' '; | |
} | |
ostr << "}"; | |
return ostr; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef STUDENTDB_ROSTER_H | |
#define STUDENTDB_ROSTER_H | |
#include "Student.h" | |
#include <vector> | |
#include <iostream> | |
using namespace std; | |
class Roster { | |
vector<Student> students; | |
public: | |
Roster(); | |
void add(Student& student); | |
Student find(string name) const; | |
friend ostream& operator<<(ostream& ostr, const Roster& roster); | |
}; | |
ostream& operator<<(ostream& ostr, const Roster& roster); | |
#endif //STUDENTDB_ROSTER_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Student.h" | |
using namespace std; | |
Student::Student(string theName, double theGpa) : name(theName), gpa(-1) { | |
set_gpa(theGpa); | |
} | |
// accessor | |
string Student::get_name() const { | |
return name; | |
} | |
// mutator | |
void Student::set_gpa(double newGpa) { | |
if (newGpa >= 0 && newGpa <= 4.0) { | |
gpa = newGpa; | |
} | |
} | |
ostream& operator<<(ostream& ostr, const Student& stud) { | |
ostr << stud.get_name() << ", " << stud.gpa; | |
return ostr; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef STUDENTDB_STUDENT_H | |
#define STUDENTDB_STUDENT_H | |
#include <string> | |
#include <iostream> | |
using namespace std; | |
class Student { | |
//private: | |
string name; | |
double gpa; | |
public: | |
Student() : name(""), gpa(-1) { } | |
Student(string theName, double theGpa); | |
// accessor | |
string get_name() const; | |
// mutator | |
void set_gpa(double newGpa); | |
friend ostream& operator<<(ostream& ostr, const Student& stud); | |
}; | |
ostream& operator<<(ostream& ostr, const Student& stud); | |
#endif //STUDENTDB_STUDENT_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment