Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include "StudentList.h"
  2. #include<string>
  3. #include<vector>
  4. #include<list>
  5. #include<fstream>
  6. #include<random>
  7. #include<chrono>
  8. #include<algorithm>
  9.  
  10.  
  11. using namespace std;
  12.  
  13. std::vector<int> ids;
  14. std::vector<string> StudentNames;
  15.  
  16. StudentList::StudentList(string FileName) {
  17.     ifstream myFile;
  18.     myFile.open(FileName);
  19.     char delim = ',';
  20.     string s;
  21.  
  22.  
  23.     while (getline(myFile, s, delim)) {
  24.         StudentNames.push_back(s);
  25.     }
  26.    
  27.     GenerateRandomIDs(ids, StudentNames.size);
  28.  
  29.     for (int i = 0; i < ids.size; i++) {
  30.         Student *st = new Student(ids[i], StudentNames[i]);
  31.         Student_List.push_back(*st);
  32.  
  33.         Student_Vector.push_back(*st);
  34.     }
  35.  
  36. }
  37.  
  38. double StudentList::sort_vector() {
  39.     chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  40.  
  41.     std::sort(Student_Vector.begin(), Student_Vector.end());
  42.  
  43.     chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
  44.     chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  45.     return time_span.count();
  46. }
  47. double StudentList::search_vector_by_id(int id) {
  48.     chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  49.  
  50.     std::find(ids.begin, ids.end, id);
  51.  
  52.     chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
  53.     chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  54.     return time_span.count();
  55. }
  56.  
  57. double StudentList::sort_list() {
  58.     chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  59.  
  60.     std::sort(Student_List.begin(), Student_List.end());
  61.  
  62.     chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
  63.     chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  64.     return time_span.count();
  65. }
  66.  
  67. double StudentList::search_list_by_id(int id) {
  68.  
  69.     chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  70.  
  71.     std::sort(Student_List.begin(), Student_List.end());
  72.  
  73.     chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
  74.     chrono::duration<double> time_span = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  75.     return time_span.count();
  76.     return 0;
  77. }
  78.  
  79.  
  80. ostream& operator<<(ostream& os, const StudentList& sl) {
  81.  
  82.     for (int i = 0; i < StudentNames.size; i++) {
  83.         os << StudentNames[i];
  84.     }
  85.     return os;
  86. }
  87.  
  88. static void GenerateRandomIDs(vector<int>& ids, const int number_of_ids) {
  89.  
  90.     int r;
  91.  
  92.     for (int i = 0; i < number_of_ids; i++) {
  93.         //give random r
  94.         do {
  95.             r = 1 + (rand() % static_cast<int>(number_of_ids));
  96.         }while (std::find(ids.begin, ids.end, r) = ids.end);
  97.  
  98.         ids.push_back(r);
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement