accessing binary heap in loop
Hi, I'm trying to print out a binary max heap, that is made up of classes that I've created above but nothing is printing out.
I've tried playing with how I print and how stuff gets inserted into the class and nothing has worked so far, so I'm open to ideas!
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
#include <iostream>
#include <vector>
#include<bits/stdc++.h>
using namespace std;
class name_t {
public:
string first;
string last;
name_t(){
string first = "";
string last = "";
name = first + last;
}
bool operator<(const name_t &rhs) const{
return this->last < rhs.last;
}
void print() const{
string fullname = last + ", " + first;
if(fullname.length() < 21){
while(fullname.length() != 21){
fullname = fullname + ".";
}
}
cout << fullname;
}
private:
static int w;
string name;
};
int name_t::w = 0;
class scores_t {
public:
int sum;
int realMean;
scores_t(){
int sum = 0;
int realMean = 0;
}
bool operator<(const scores_t &rhs) const{
return this->realMean < rhs.realMean;
}
void insert(int k){
scores.push_back(k);
}
void insert_done(){
sum = accumulate(scores.begin(), scores.end(), 0.0);
realMean = sum / scores.size();
}
void print(){
for(unsigned int i = 0; i < scores.size(); i++){
cout << setw(3) << scores.at(i) << " ";
}
cout << ": " << realMean;
cout << endl;
}
private:
vector<int> scores;
float mean;
};
class namescores_t {
public:
namescores_t(){
}
bool operator<(const namescores_t &rhs) const{
if(this->scores.realMean < rhs.scores.realMean){
return true;
}
if(this->scores.realMean == rhs.scores.realMean){
if(this->name.last < rhs.name.last){
return true;
}
}
return false;
}
void insertion(name_t n, scores_t s){
name = n;
scores = s;
}
void print_name(){
name.print();
}
void print_scores(){
scores.print();
}
private:
name_t name;
scores_t scores;
};
int main(int argc, char* argv[]) {
/*commandline parsing
argv[1]: K (number of records to print)
argv[2]: filename.txt*/
int k = 0;
k = stoi(argv[1]);
if(argc != 3){
cout << "usage: ./rank_byscores1 -k(number of entries) filename.txt";
return -1;
}
fstream fin;
fin.open(argv[2]);
vector<namescores_t> NS;
if(fin.is_open()){
while(true){
if(fin.eof()) break;
while(true){
if(fin.eof()){
break;
}
name_t n;
scores_t s;
namescores_t NAMESCORE;
string tmp;
getline(fin, tmp);
stringstream ss(tmp);
ss >> n.first;
ss >> n.last;
int scoresTmp;
while(ss >> scoresTmp){
int scores;
scores = scoresTmp;
s.insert(scores);
}
s.insert_done();
NAMESCORE.insertion(n, s);
NS.push_back(NAMESCORE);
}
}
}
fin.close();
make_heap(NS.begin(), NS.end());
for(int i = 0; i < k; i++){
pop_heap(NS.begin(), NS.end());
namescores_t ns;
ns = NS.back();
NS.pop_back();
ns.print_name();
ns.print_scores();
}
}
| |
Topic archived. No new replies allowed.