Vector<shared_ptr> Problem.

Hello for some reason I'm having a hard time understanding smart pointers, specifically shared_ptr in this case. Could someone please tell me what i'm doing wrong in this program. I have a strong feeling i'm messing up the memory management through copying and assigning

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
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <algorithm>

struct Review
{
    std::string title;
    int rating;
    double price;			
};

bool operator<(const Review & r1, const Review & r2);
bool worseThan(const Review & r1, const Review & r2);
bool isCostlier(const Review & r1, const Review & r2);
bool FillReview(Review & rr);
void ShowReview(const Review & rr);
void ShowOptions();

int main()
{
    using namespace std;

    // Variables
    vector<shared_ptr<Review> > booksAlpha;
    Review temp;
    char choice;
	
    // Filling the modifiable vector
    while (FillReview(temp))
        booksAlpha.push_back(shared_ptr<Review>(new Review(temp)));
		
    vector<shared_ptr<Review> > booksOri = booksAlpha;
    vector<shared_ptr<Review> > booksRating = booksOri;
    vector<shared_ptr<Review> > booksPrice = booksOri;
	
    sort(booksAlpha.begin(), booksAlpha.end());
    sort(booksRating.begin(), booksRating.end(), worseThan);
    sort(booksPrice.begin(), booksPrice.end(), isCostlier);

   cout << "Bye.\n";
    return 0;
}

bool operator<(const Review & r1, const Review & r2)
{
    if (r1.title < r2.title)
        return true;
    else if (r1.title == r2.title && r1.rating < r2.rating)
        return true;
    else
        return false;
}

bool worseThan(const Review & r1, const Review & r2)
{
    if (r1.rating < r2.rating)
        return true;
    else
        return false;
}

bool isCostlier(const Review & r1, const Review & r2)
{
	if (r1.price < r2.price)
		return true;
	else if (r1.price == r2.price && r1.title < r2.title)
		return true;
	else
		return false;
}

bool FillReview(Review & rr)
{
    std::cout << "Enter book title (quit to quit): ";
    std::getline(std::cin,rr.title);
    if (rr.title == "quit")
        return false;
    std::cout << "Enter book rating: ";
    std::cin >> rr.rating;
    if (!std::cin)
        return false;
	std::cout << "Enter book price: ";
	std::cin >> rr.price;
	if (!std::cin)
		return false;
    // get rid of rest of input line
    while (std::cin.get() != '\n')
        continue;
    return true;
}

void ShowReview(const Review & rr)
{
    std::cout << rr.rating << '\t' << rr.title << '\t' << rr.price << std::endl;
}

void ShowOptions()
{
	std::cout << "a) original order\tb) alpabetical order\n"
		     "c) increasing ratings\td) decreasing ratings\n"
		     "e) increasing price\tf) decreasing price\n"
		     "g) quit\t\t\t\t\t\t\t";
}
Last edited on
What is the problem?
You have smart pointers in your vectors. Your comparison functions need to take smart pointers.

For example:
1
2
3
4
5
6
7
bool operator<(const std::shared_ptr<Review>& a, const std::shared_ptr<Review>& b)
{
    if ( a->title < b->title )
        return true ;

    return a->title == b->title  &&  a->rating < b->rating ;
}


Although there isn't really any reason to use pointers at all in your code.
Unfortunately after making all the changes to the comparison and showing methods the code still doesn't run with a load of jargon filling up the cmd scroll bar.
Edit: Sorry it works now thank you. Forgot to change the prototypes. Anyways although this really isn't necessary I'm using this for an exercise in my c++ book, C++ Primer Plus. Here's the Exercise if your interested and seeing why I needed this.

Modify Listing 16.9 (vect3.cpp) as follows:
Add a price member to the Review structure.

Instead of using a vector of Review objects to hold the input, use a vector of shared_ptr<Review> objects. Remember that a shared_ptr has to be initialized with a pointer returned by new.

Follow the input stage with a loop that allows the user the following options for displaying books: in original order, in alphabetical order, in order of increasing ratings, in order of decreasing ratings, in order of increasing price, in order of decreasing price, and quitting.

Here’s one possible approach. After getting the initial input, create another vector of shared_ptrs initialized to the original array. Define an operator<() function that compares pointed-to structures and use it to sort the second vector so that the shared_ptrs are in the order of the book names stored in the pointed-to objects. Repeat the process to get vectors of shared_ptrs sorted by rating and by price. Note that rbegin() and rend() save you the trouble of also creating vectors of reversed order.


Either way, thank you for the help.
Last edited on
Topic archived. No new replies allowed.