binary heap of user classes

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
  // range heap example
#include <iostream>     // std::cout
#include <algorithm>    // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap
#include <vector>       // std::vector
#include <fstream>
#include <bits/stdc++.h>
#include<sstream>
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:
    

    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 print_name(){
      name.print();
    }
    void print_scores(){
      scores.print();
    }

  public:
    name_t name;
    scores_t scores;
};




int main (int argc, char* argv[]) {
    fstream fin;
    if(argc !=3){
        cout << "usage incorrect";
        return -1;
    }
    int k = atoi(argv[1]);
    fin.open(argv[2]);
    vector<namescores_t> v;
    if(fin.is_open()){
        
       
        while(true){
            name_t n;
            scores_t s;
            if(fin.eof()) break;
            string tmp;
            getline(fin, tmp);
            stringstream ss(tmp);
            ss >> n.first;
            ss >> n.last;
            int tmpScore;
            while(ss >> tmpScore){
                int realScore;
                realScore = tmpScore;
                s.insert(realScore);
            }
            s.insert_done();
            namescores_t t;
            t.name = n;
            t.scores = s;
            v.push_back(t);
        }
        

    }

    fin.close();

    make_heap(v.begin(), v.end());

    vector<namescores_t>::iterator it;
    for(it = v.begin(); it != v.end() && !v.empty(); ++it){
        it->print_name();
        it->print_name();
    }



    return 0;


}
George you put me into an infinite loop! Help!

(Okay let's just agree to use this thread)
Not that is really matters to me one way or the other, but......

...since a binary heap construct is likely to not be something for a really truly beginner I'd simply opine the discussion should be done in the general forum thread.

Beyond that I'm opinion-less.
@SSDEEZ,
Just as a matter of interest ... what is your code supposed to do?
Is the file data being read correctly? Has it opened ok? What's the format of the data file? Please provide a sample for testing.

Reading the file looks wrong - as .eof() is set when attempting to read - not after a read has completed. getline() would usually be the while condition.
Without having proper test data, I've tried to deduce the format. Perhaps something like this which reads and processes the data without using heap:

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
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
#include <numeric>
#include <iomanip>

struct name_t {
	std::string first;
	std::string last;

	name_t() = default;

	bool operator<(const name_t& rhs) const {
		return last < rhs.last;
	}

	void print() const {
		std::string fullname {last + ", " + first};

		if (fullname.length() < 21)
			fullname += std::string(21 - fullname.size(), '.');

		std::cout << fullname;
	}
};

class scores_t {
public:
	double sum {};
	double realMean {};

	scores_t() = default;

	bool operator<(const scores_t& rhs) const {
		return realMean < rhs.realMean;
	}

	void insert(double k) {
		scores.push_back(k);
	}

	void insert_done() {
		sum = std::accumulate(scores.begin(), scores.end(), 0.0);
		realMean = sum / scores.size();
	}

	void print() const {
		for (unsigned int i {}; i < scores.size(); ++i)
			std::cout << std::setw(3) << std::fixed << std::setprecision(1) << scores.at(i) << " ";

		std::cout << ": " << realMean << '\n';
	}

private:
	std::vector<double> scores;
};

class namescores_t {
public:
	namescores_t(const name_t& n, const scores_t& s) : name(n), scores(s) {}

	bool operator<(const namescores_t& rhs) const {
		if (scores.realMean < rhs.scores.realMean)
			return true;

		if (scores.realMean == rhs.scores.realMean)
			if (name.last < rhs.name.last)
				return true;

		return false;
	}

	void print_name() const {
		name.print();
	}

	void print_scores() const {
		scores.print();
	}

public:
	name_t name;
	scores_t scores;
};

int main(int argc, char* argv[]) {
	if (argc != 3) {
		std::cout << "usage incorrect\n";
		return -1;
	}

	std::fstream fin(argv[2]);

	if (!fin)
		return (std::cout << "Cannot open file\n"), 1;

	std::vector<namescores_t> v;

	for (name_t n; fin >> n.first >> n.last; ) {
		scores_t s;

		for (double tmpScore; fin >> tmpScore; s.insert(tmpScore));
		fin.clear();
		s.insert_done();
		v.emplace_back(n, s);
	}

	//make_heap(v.begin(), v.end());

	for (const auto& e : v) {
		e.print_name();
		e.print_scores();
	}
}


Using data file:


Ronald Jones
7.5 8.8 7 8.1 8 9.8 9.3 8.9 9.1 9

Mirabella Jones
6.5 9.8 6 7.1 8 8.8 6.3 7.9 9.1 8

Ryan Sheckler
8.5 7.8 6 9.1 7 8.8 7.3 9.9 8.1 7


displays:


Jones, Ronald........7.5 8.8 7.0 8.1 8.0 9.8 9.3 8.9 9.1 9.0 : 8.6
Jones, Mirabella.....6.5 9.8 6.0 7.1 8.0 8.8 6.3 7.9 9.1 8.0 : 7.8
Sheckler, Ryan.......8.5 7.8 6.0 9.1 7.0 8.8 7.3 9.9 8.1 7.0 : 8.0

Last edited on
@lastchance its supposed to take a text file of scores and print them out in decreasing order based on their score averages. That's why I wanted to use a heap.
What does the input text file look like, and why do you need three separate classes?

A single "Person" class with data members lastname, firstname, averageScore would be sufficient to put through std::sort.
print them out in decreasing order based on their score averages.


OK. In my code above replace L109 with:

 
	std::sort(v.begin(), v.end(), [](const auto& a, const auto& b) { return a.scores.realMean > b.scores.realMean; });


which now displays:


Jones, Ronald........7.5 8.8 7.0 8.1 8.0 9.8 9.3 8.9 9.1 9.0 : 8.6
Sheckler, Ryan.......8.5 7.8 6.0 9.1 7.0 8.8 7.3 9.9 8.1 7.0 : 8.0
Jones, Mirabella.....6.5 9.8 6.0 7.1 8.0 8.8 6.3 7.9 9.1 8.0 : 7.8

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
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;


struct Person
{
   string firstname, lastname;
   double averageScore;
};


istream & operator >> ( istream &in, Person &person )
{
   string line;
   getline( in, line );
   stringstream ss( line );
   ss >> person.firstname >> person.lastname;
   int n = 0;
   double sum = 0;
   for ( double value; ss >> value; n++ ) sum += value;
   person.averageScore = sum / n;
   return in;
}


bool compScore( const Person &p, const Person &q )
{
   if ( p.averageScore != q.averageScore ) return p.averageScore > q.averageScore;
   if ( p.lastname != q.lastname ) return p.lastname > q.lastname;
   return p.firstname > q.firstname;
}


int main()
{
// ifstream in( "input.txt" );
   istringstream in( "Josef Stalin 1.4 1.7 2.2 0.5\n"
                     "Volodymyr Zelensky 9.1 9.9 9.8 9.6\n"
                     "Vladimir Putin 0.4 0.3 0.1 0\n" );
   
   vector<Person> people;
   for ( Person p; in >> p; ) people.push_back( p );
   sort( people.begin(), people.end(), compScore );
   for ( Person &p : people ) cout << p.firstname << " " << p.lastname << ": " << p.averageScore << '\n';
}


Volodymyr Zelensky: 9.6
Josef Stalin: 1.45
Vladimir Putin: 0.2
Last edited on
@lastchance yeah, a single class would be much easier, alas professors these days like to have their way.

last time I'll take a class for this stuff at a uni
a single class would be much easier

Not really. Not in this case. You essentially have "one class". It just happens to have members that have members. A composite structure.

If you had separate arrays that you had to reorder simultaneously -- that would be more a chore.


[EDIT]
Looking at your operator< definitions creates an impression that you don't have a clue of what you are doing -- you don't exploit the encapsulation that the multiple classes offer.
Last edited on
Topic archived. No new replies allowed.