Sorting 3 distinct vectors based on one of them












0












$begingroup$


I'm having trouble trying to sort 3 vectors at the same based on one vector.



movieratings.h(only focused on GenerateResults implementation)



#ifndef __MOVIE_RATINGS_H__
#define __MOVIE_RATINGS_H__

#include <algorithm>
#include <iomanip>
#include <iterator>
#include <list>
#include <vector>

#include "movie.h"
#include "rating.h"

class MovieRatings
{
private:
std::vector<Movie> m_Movies;
std::vector<Rating> m_Ratings;

public:
template<class MovieInputIterator, class RatingInputIterator>
MovieRatings(MovieInputIterator movieBegin, MovieInputIterator movieEnd, RatingInputIterator ratingBegin, RatingInputIterator ratingEnd)
: m_Movies(movieBegin, movieEnd), m_Ratings(ratingBegin, ratingEnd)
{

}

void GenerateResults(std::ostream& os) const
{
std::vector<Movie> o_Movies(m_Movies);
std::vector<Rating> o_Ratings(m_Ratings);
std::vector<float> averages;

std::for_each(o_Ratings.begin(), o_Ratings.end(), [&](Rating const& r)
{
unsigned int movieID = r.GetID();
std::string movieName = o_Movies[movieID-1].GetName();
averages.push_back(r.GetAverageRating());

});

// OUTPUT:: [movie id] [average rating] [movie name]
// movies in descending order by average rating (float point value to 2 decimals)
// TODO: Implement the GenerateResults function to output the movie id, average rating, and movie name in descending order by average rating
// NOTE: do not modify the m_Movies or m_Ratings objects in this function!
}
};

std::istream& operator>>(std::istream& is, Movie& g)
{
is >> g.m_ID;
std::getline(is, g.m_Name);
return is;
}

std::istream& operator>>(std::istream& is, Rating& r)
{
is >> r.m_ID;
r.m_Ratings.clear();
std::string line;
std::getline(is, line);
std::istringstream iss(line);
std::copy(std::istream_iterator<int>(iss), std::istream_iterator<int>(), std::back_inserter(r.m_Ratings));
return is;
}

#endif // __MOVIE_RATINGS_H__


movie.h



#ifndef __MOVIE_H__
#define __MOVIE_H__

#include <iostream>
#include <string>

class Movie
{
private:
int m_ID;
std::string m_Name;
public:
Movie() : m_ID(-1)
{

}

int GetID() const
{
return m_ID;
}

void SetID(int id)
{
m_ID = id;
}

std::string GetName() const
{
return m_Name;
}

void SetName(const std::string& name)
{
m_Name = name;
}

friend std::istream& operator>>(std::istream& is, Movie& g);
friend std::ostream& operator<<(std::ostream& os, const Movie& g);
};

std::ostream& operator<<(std::ostream& os, const Movie& g)
{
os << g.m_ID << " " << g.m_Name << std::endl;
return os;
}

#endif // __MOVIE_H__


rating.h



#ifndef __RATING_H__
#define __RATING_H__

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <numeric>

class Rating
{
private:
int m_ID;
std::list<int> m_Ratings;
public:
Rating()
{
}

int GetID() const
{
return m_ID;
}

void SetID(int id)
{
m_ID = id;
}

std::list<int> GetRatings() const
{
return m_Ratings;
}

void SetRatings(const std::list<int>& ratings)
{
m_Ratings = ratings;
}

float GetAverageRating() const
{
int totalRatings = std::accumulate(m_Ratings.begin(), m_Ratings.end(), 0);
int count = m_Ratings.size();
float result = static_cast<float>(totalRatings) / static_cast<float>(count);
return result;
}

bool operator> (const Rating& rhs) const
{
float l = GetAverageRating();
float r = rhs.GetAverageRating();
return l > r;
}

friend std::istream& operator>>(std::istream& is, Rating& r);
friend std::ostream& operator<<(std::ostream& os, const Rating& r);
};

std::ostream& operator<<(std::ostream& os, const Rating& r)
{
os << r.m_ID << " ";
std::copy(r.m_Ratings.begin(), r.m_Ratings.end(), std::ostream_iterator<int>(os, " "));
os << std::endl;
return os;
}

#endif // __RATING_H__


what I have so far in the vectors:





1 4.00  Lord of the Rings
2 4.06 The Two Towers
3 4.90 Return of the King
4 3.20 The Hobbit
5 2.20 Desolation of Smaug
6 1.90 Battle of the Seven Armies
7 4.84 The Matrix
8 3.44 Matrix Reloaded
9 3.00 Matrix Revolutions
10 1.44 The Last Jedi


and the expected output:



3 4.90  Return of the King
7 4.84 The Matrix
2 4.06 The Two Towers
1 4.00 Lord of the Rings
8 3.44 Matrix Reloaded
4 3.20 The Hobbit
9 3.00 Matrix Revolutions
5 2.20 Desolation of Smaug
6 1.90 Battle of the Seven Armies
10 1.44 The Last Jedi


I realize creating a struct and sort()ing them that way would work, but unfortunately I'm not allowed to create any new function and I'm only allowed to modify the GenerateResults implementation in the movieratings.h file. I know I could sort the averages vector with sort(averages.begin(), averages.end(), std::greater<float>) but how would I sort the others along with it based on the averages? Also, I'm not allowed to use any custom loops which is why I've used the for_each function to get the averages vector.










share|improve this question









New contributor




Hitman Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Welcome to Code Review! Can you confirm that the code functions correctly? If so, I recommend that you edit to add a summary of the testing (ideally as reproducible unit-test code). If it's not working, it isn't ready for review (see help center) and the question may be deleted.
    $endgroup$
    – Toby Speight
    12 hours ago
















0












$begingroup$


I'm having trouble trying to sort 3 vectors at the same based on one vector.



movieratings.h(only focused on GenerateResults implementation)



#ifndef __MOVIE_RATINGS_H__
#define __MOVIE_RATINGS_H__

#include <algorithm>
#include <iomanip>
#include <iterator>
#include <list>
#include <vector>

#include "movie.h"
#include "rating.h"

class MovieRatings
{
private:
std::vector<Movie> m_Movies;
std::vector<Rating> m_Ratings;

public:
template<class MovieInputIterator, class RatingInputIterator>
MovieRatings(MovieInputIterator movieBegin, MovieInputIterator movieEnd, RatingInputIterator ratingBegin, RatingInputIterator ratingEnd)
: m_Movies(movieBegin, movieEnd), m_Ratings(ratingBegin, ratingEnd)
{

}

void GenerateResults(std::ostream& os) const
{
std::vector<Movie> o_Movies(m_Movies);
std::vector<Rating> o_Ratings(m_Ratings);
std::vector<float> averages;

std::for_each(o_Ratings.begin(), o_Ratings.end(), [&](Rating const& r)
{
unsigned int movieID = r.GetID();
std::string movieName = o_Movies[movieID-1].GetName();
averages.push_back(r.GetAverageRating());

});

// OUTPUT:: [movie id] [average rating] [movie name]
// movies in descending order by average rating (float point value to 2 decimals)
// TODO: Implement the GenerateResults function to output the movie id, average rating, and movie name in descending order by average rating
// NOTE: do not modify the m_Movies or m_Ratings objects in this function!
}
};

std::istream& operator>>(std::istream& is, Movie& g)
{
is >> g.m_ID;
std::getline(is, g.m_Name);
return is;
}

std::istream& operator>>(std::istream& is, Rating& r)
{
is >> r.m_ID;
r.m_Ratings.clear();
std::string line;
std::getline(is, line);
std::istringstream iss(line);
std::copy(std::istream_iterator<int>(iss), std::istream_iterator<int>(), std::back_inserter(r.m_Ratings));
return is;
}

#endif // __MOVIE_RATINGS_H__


movie.h



#ifndef __MOVIE_H__
#define __MOVIE_H__

#include <iostream>
#include <string>

class Movie
{
private:
int m_ID;
std::string m_Name;
public:
Movie() : m_ID(-1)
{

}

int GetID() const
{
return m_ID;
}

void SetID(int id)
{
m_ID = id;
}

std::string GetName() const
{
return m_Name;
}

void SetName(const std::string& name)
{
m_Name = name;
}

friend std::istream& operator>>(std::istream& is, Movie& g);
friend std::ostream& operator<<(std::ostream& os, const Movie& g);
};

std::ostream& operator<<(std::ostream& os, const Movie& g)
{
os << g.m_ID << " " << g.m_Name << std::endl;
return os;
}

#endif // __MOVIE_H__


rating.h



#ifndef __RATING_H__
#define __RATING_H__

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <numeric>

class Rating
{
private:
int m_ID;
std::list<int> m_Ratings;
public:
Rating()
{
}

int GetID() const
{
return m_ID;
}

void SetID(int id)
{
m_ID = id;
}

std::list<int> GetRatings() const
{
return m_Ratings;
}

void SetRatings(const std::list<int>& ratings)
{
m_Ratings = ratings;
}

float GetAverageRating() const
{
int totalRatings = std::accumulate(m_Ratings.begin(), m_Ratings.end(), 0);
int count = m_Ratings.size();
float result = static_cast<float>(totalRatings) / static_cast<float>(count);
return result;
}

bool operator> (const Rating& rhs) const
{
float l = GetAverageRating();
float r = rhs.GetAverageRating();
return l > r;
}

friend std::istream& operator>>(std::istream& is, Rating& r);
friend std::ostream& operator<<(std::ostream& os, const Rating& r);
};

std::ostream& operator<<(std::ostream& os, const Rating& r)
{
os << r.m_ID << " ";
std::copy(r.m_Ratings.begin(), r.m_Ratings.end(), std::ostream_iterator<int>(os, " "));
os << std::endl;
return os;
}

#endif // __RATING_H__


what I have so far in the vectors:





1 4.00  Lord of the Rings
2 4.06 The Two Towers
3 4.90 Return of the King
4 3.20 The Hobbit
5 2.20 Desolation of Smaug
6 1.90 Battle of the Seven Armies
7 4.84 The Matrix
8 3.44 Matrix Reloaded
9 3.00 Matrix Revolutions
10 1.44 The Last Jedi


and the expected output:



3 4.90  Return of the King
7 4.84 The Matrix
2 4.06 The Two Towers
1 4.00 Lord of the Rings
8 3.44 Matrix Reloaded
4 3.20 The Hobbit
9 3.00 Matrix Revolutions
5 2.20 Desolation of Smaug
6 1.90 Battle of the Seven Armies
10 1.44 The Last Jedi


I realize creating a struct and sort()ing them that way would work, but unfortunately I'm not allowed to create any new function and I'm only allowed to modify the GenerateResults implementation in the movieratings.h file. I know I could sort the averages vector with sort(averages.begin(), averages.end(), std::greater<float>) but how would I sort the others along with it based on the averages? Also, I'm not allowed to use any custom loops which is why I've used the for_each function to get the averages vector.










share|improve this question









New contributor




Hitman Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Welcome to Code Review! Can you confirm that the code functions correctly? If so, I recommend that you edit to add a summary of the testing (ideally as reproducible unit-test code). If it's not working, it isn't ready for review (see help center) and the question may be deleted.
    $endgroup$
    – Toby Speight
    12 hours ago














0












0








0





$begingroup$


I'm having trouble trying to sort 3 vectors at the same based on one vector.



movieratings.h(only focused on GenerateResults implementation)



#ifndef __MOVIE_RATINGS_H__
#define __MOVIE_RATINGS_H__

#include <algorithm>
#include <iomanip>
#include <iterator>
#include <list>
#include <vector>

#include "movie.h"
#include "rating.h"

class MovieRatings
{
private:
std::vector<Movie> m_Movies;
std::vector<Rating> m_Ratings;

public:
template<class MovieInputIterator, class RatingInputIterator>
MovieRatings(MovieInputIterator movieBegin, MovieInputIterator movieEnd, RatingInputIterator ratingBegin, RatingInputIterator ratingEnd)
: m_Movies(movieBegin, movieEnd), m_Ratings(ratingBegin, ratingEnd)
{

}

void GenerateResults(std::ostream& os) const
{
std::vector<Movie> o_Movies(m_Movies);
std::vector<Rating> o_Ratings(m_Ratings);
std::vector<float> averages;

std::for_each(o_Ratings.begin(), o_Ratings.end(), [&](Rating const& r)
{
unsigned int movieID = r.GetID();
std::string movieName = o_Movies[movieID-1].GetName();
averages.push_back(r.GetAverageRating());

});

// OUTPUT:: [movie id] [average rating] [movie name]
// movies in descending order by average rating (float point value to 2 decimals)
// TODO: Implement the GenerateResults function to output the movie id, average rating, and movie name in descending order by average rating
// NOTE: do not modify the m_Movies or m_Ratings objects in this function!
}
};

std::istream& operator>>(std::istream& is, Movie& g)
{
is >> g.m_ID;
std::getline(is, g.m_Name);
return is;
}

std::istream& operator>>(std::istream& is, Rating& r)
{
is >> r.m_ID;
r.m_Ratings.clear();
std::string line;
std::getline(is, line);
std::istringstream iss(line);
std::copy(std::istream_iterator<int>(iss), std::istream_iterator<int>(), std::back_inserter(r.m_Ratings));
return is;
}

#endif // __MOVIE_RATINGS_H__


movie.h



#ifndef __MOVIE_H__
#define __MOVIE_H__

#include <iostream>
#include <string>

class Movie
{
private:
int m_ID;
std::string m_Name;
public:
Movie() : m_ID(-1)
{

}

int GetID() const
{
return m_ID;
}

void SetID(int id)
{
m_ID = id;
}

std::string GetName() const
{
return m_Name;
}

void SetName(const std::string& name)
{
m_Name = name;
}

friend std::istream& operator>>(std::istream& is, Movie& g);
friend std::ostream& operator<<(std::ostream& os, const Movie& g);
};

std::ostream& operator<<(std::ostream& os, const Movie& g)
{
os << g.m_ID << " " << g.m_Name << std::endl;
return os;
}

#endif // __MOVIE_H__


rating.h



#ifndef __RATING_H__
#define __RATING_H__

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <numeric>

class Rating
{
private:
int m_ID;
std::list<int> m_Ratings;
public:
Rating()
{
}

int GetID() const
{
return m_ID;
}

void SetID(int id)
{
m_ID = id;
}

std::list<int> GetRatings() const
{
return m_Ratings;
}

void SetRatings(const std::list<int>& ratings)
{
m_Ratings = ratings;
}

float GetAverageRating() const
{
int totalRatings = std::accumulate(m_Ratings.begin(), m_Ratings.end(), 0);
int count = m_Ratings.size();
float result = static_cast<float>(totalRatings) / static_cast<float>(count);
return result;
}

bool operator> (const Rating& rhs) const
{
float l = GetAverageRating();
float r = rhs.GetAverageRating();
return l > r;
}

friend std::istream& operator>>(std::istream& is, Rating& r);
friend std::ostream& operator<<(std::ostream& os, const Rating& r);
};

std::ostream& operator<<(std::ostream& os, const Rating& r)
{
os << r.m_ID << " ";
std::copy(r.m_Ratings.begin(), r.m_Ratings.end(), std::ostream_iterator<int>(os, " "));
os << std::endl;
return os;
}

#endif // __RATING_H__


what I have so far in the vectors:





1 4.00  Lord of the Rings
2 4.06 The Two Towers
3 4.90 Return of the King
4 3.20 The Hobbit
5 2.20 Desolation of Smaug
6 1.90 Battle of the Seven Armies
7 4.84 The Matrix
8 3.44 Matrix Reloaded
9 3.00 Matrix Revolutions
10 1.44 The Last Jedi


and the expected output:



3 4.90  Return of the King
7 4.84 The Matrix
2 4.06 The Two Towers
1 4.00 Lord of the Rings
8 3.44 Matrix Reloaded
4 3.20 The Hobbit
9 3.00 Matrix Revolutions
5 2.20 Desolation of Smaug
6 1.90 Battle of the Seven Armies
10 1.44 The Last Jedi


I realize creating a struct and sort()ing them that way would work, but unfortunately I'm not allowed to create any new function and I'm only allowed to modify the GenerateResults implementation in the movieratings.h file. I know I could sort the averages vector with sort(averages.begin(), averages.end(), std::greater<float>) but how would I sort the others along with it based on the averages? Also, I'm not allowed to use any custom loops which is why I've used the for_each function to get the averages vector.










share|improve this question









New contributor




Hitman Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I'm having trouble trying to sort 3 vectors at the same based on one vector.



movieratings.h(only focused on GenerateResults implementation)



#ifndef __MOVIE_RATINGS_H__
#define __MOVIE_RATINGS_H__

#include <algorithm>
#include <iomanip>
#include <iterator>
#include <list>
#include <vector>

#include "movie.h"
#include "rating.h"

class MovieRatings
{
private:
std::vector<Movie> m_Movies;
std::vector<Rating> m_Ratings;

public:
template<class MovieInputIterator, class RatingInputIterator>
MovieRatings(MovieInputIterator movieBegin, MovieInputIterator movieEnd, RatingInputIterator ratingBegin, RatingInputIterator ratingEnd)
: m_Movies(movieBegin, movieEnd), m_Ratings(ratingBegin, ratingEnd)
{

}

void GenerateResults(std::ostream& os) const
{
std::vector<Movie> o_Movies(m_Movies);
std::vector<Rating> o_Ratings(m_Ratings);
std::vector<float> averages;

std::for_each(o_Ratings.begin(), o_Ratings.end(), [&](Rating const& r)
{
unsigned int movieID = r.GetID();
std::string movieName = o_Movies[movieID-1].GetName();
averages.push_back(r.GetAverageRating());

});

// OUTPUT:: [movie id] [average rating] [movie name]
// movies in descending order by average rating (float point value to 2 decimals)
// TODO: Implement the GenerateResults function to output the movie id, average rating, and movie name in descending order by average rating
// NOTE: do not modify the m_Movies or m_Ratings objects in this function!
}
};

std::istream& operator>>(std::istream& is, Movie& g)
{
is >> g.m_ID;
std::getline(is, g.m_Name);
return is;
}

std::istream& operator>>(std::istream& is, Rating& r)
{
is >> r.m_ID;
r.m_Ratings.clear();
std::string line;
std::getline(is, line);
std::istringstream iss(line);
std::copy(std::istream_iterator<int>(iss), std::istream_iterator<int>(), std::back_inserter(r.m_Ratings));
return is;
}

#endif // __MOVIE_RATINGS_H__


movie.h



#ifndef __MOVIE_H__
#define __MOVIE_H__

#include <iostream>
#include <string>

class Movie
{
private:
int m_ID;
std::string m_Name;
public:
Movie() : m_ID(-1)
{

}

int GetID() const
{
return m_ID;
}

void SetID(int id)
{
m_ID = id;
}

std::string GetName() const
{
return m_Name;
}

void SetName(const std::string& name)
{
m_Name = name;
}

friend std::istream& operator>>(std::istream& is, Movie& g);
friend std::ostream& operator<<(std::ostream& os, const Movie& g);
};

std::ostream& operator<<(std::ostream& os, const Movie& g)
{
os << g.m_ID << " " << g.m_Name << std::endl;
return os;
}

#endif // __MOVIE_H__


rating.h



#ifndef __RATING_H__
#define __RATING_H__

#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
#include <numeric>

class Rating
{
private:
int m_ID;
std::list<int> m_Ratings;
public:
Rating()
{
}

int GetID() const
{
return m_ID;
}

void SetID(int id)
{
m_ID = id;
}

std::list<int> GetRatings() const
{
return m_Ratings;
}

void SetRatings(const std::list<int>& ratings)
{
m_Ratings = ratings;
}

float GetAverageRating() const
{
int totalRatings = std::accumulate(m_Ratings.begin(), m_Ratings.end(), 0);
int count = m_Ratings.size();
float result = static_cast<float>(totalRatings) / static_cast<float>(count);
return result;
}

bool operator> (const Rating& rhs) const
{
float l = GetAverageRating();
float r = rhs.GetAverageRating();
return l > r;
}

friend std::istream& operator>>(std::istream& is, Rating& r);
friend std::ostream& operator<<(std::ostream& os, const Rating& r);
};

std::ostream& operator<<(std::ostream& os, const Rating& r)
{
os << r.m_ID << " ";
std::copy(r.m_Ratings.begin(), r.m_Ratings.end(), std::ostream_iterator<int>(os, " "));
os << std::endl;
return os;
}

#endif // __RATING_H__


what I have so far in the vectors:





1 4.00  Lord of the Rings
2 4.06 The Two Towers
3 4.90 Return of the King
4 3.20 The Hobbit
5 2.20 Desolation of Smaug
6 1.90 Battle of the Seven Armies
7 4.84 The Matrix
8 3.44 Matrix Reloaded
9 3.00 Matrix Revolutions
10 1.44 The Last Jedi


and the expected output:



3 4.90  Return of the King
7 4.84 The Matrix
2 4.06 The Two Towers
1 4.00 Lord of the Rings
8 3.44 Matrix Reloaded
4 3.20 The Hobbit
9 3.00 Matrix Revolutions
5 2.20 Desolation of Smaug
6 1.90 Battle of the Seven Armies
10 1.44 The Last Jedi


I realize creating a struct and sort()ing them that way would work, but unfortunately I'm not allowed to create any new function and I'm only allowed to modify the GenerateResults implementation in the movieratings.h file. I know I could sort the averages vector with sort(averages.begin(), averages.end(), std::greater<float>) but how would I sort the others along with it based on the averages? Also, I'm not allowed to use any custom loops which is why I've used the for_each function to get the averages vector.







c++ sorting vectors






share|improve this question









New contributor




Hitman Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Hitman Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 14 mins ago









Jamal

30.3k11117227




30.3k11117227






New contributor




Hitman Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 13 hours ago









Hitman HunterHitman Hunter

31




31




New contributor




Hitman Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Hitman Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Hitman Hunter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    Welcome to Code Review! Can you confirm that the code functions correctly? If so, I recommend that you edit to add a summary of the testing (ideally as reproducible unit-test code). If it's not working, it isn't ready for review (see help center) and the question may be deleted.
    $endgroup$
    – Toby Speight
    12 hours ago


















  • $begingroup$
    Welcome to Code Review! Can you confirm that the code functions correctly? If so, I recommend that you edit to add a summary of the testing (ideally as reproducible unit-test code). If it's not working, it isn't ready for review (see help center) and the question may be deleted.
    $endgroup$
    – Toby Speight
    12 hours ago
















$begingroup$
Welcome to Code Review! Can you confirm that the code functions correctly? If so, I recommend that you edit to add a summary of the testing (ideally as reproducible unit-test code). If it's not working, it isn't ready for review (see help center) and the question may be deleted.
$endgroup$
– Toby Speight
12 hours ago




$begingroup$
Welcome to Code Review! Can you confirm that the code functions correctly? If so, I recommend that you edit to add a summary of the testing (ideally as reproducible unit-test code). If it's not working, it isn't ready for review (see help center) and the question may be deleted.
$endgroup$
– Toby Speight
12 hours ago










1 Answer
1






active

oldest

votes


















1












$begingroup$

No need to copy the movies. Copy the ranges and sort it by average, use the id as index to movies (as you're doing in your post). Note: as the movie ID comes from external input, it must be ensured that it corresponds to an actual movie ID, otherwise a user could enter an illegal movie ID crashing your application when trying to access the element in m_Movies.



void GenerateResults(std::ostream& os) const
{
std::vector<Rating> ratings(m_Ratings);
std::sort(ratings.begin(), ratings.end(),
(r1, r2){ return r1.GetAverageRating() < r2.GetAverageRating(); });

std::for_each(ratings.begin(), ratings.end(), [&](Rating const& r)
{
auto id = r.GetID();
os << id << " " << r.GetAverageRating() << " " << m_Movies[id-1].GetName() <<"n";
});
}
```





share|improve this answer








New contributor




Cornholio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Hitman Hunter is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212698%2fsorting-3-distinct-vectors-based-on-one-of-them%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1












    $begingroup$

    No need to copy the movies. Copy the ranges and sort it by average, use the id as index to movies (as you're doing in your post). Note: as the movie ID comes from external input, it must be ensured that it corresponds to an actual movie ID, otherwise a user could enter an illegal movie ID crashing your application when trying to access the element in m_Movies.



    void GenerateResults(std::ostream& os) const
    {
    std::vector<Rating> ratings(m_Ratings);
    std::sort(ratings.begin(), ratings.end(),
    (r1, r2){ return r1.GetAverageRating() < r2.GetAverageRating(); });

    std::for_each(ratings.begin(), ratings.end(), [&](Rating const& r)
    {
    auto id = r.GetID();
    os << id << " " << r.GetAverageRating() << " " << m_Movies[id-1].GetName() <<"n";
    });
    }
    ```





    share|improve this answer








    New contributor




    Cornholio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    $endgroup$


















      1












      $begingroup$

      No need to copy the movies. Copy the ranges and sort it by average, use the id as index to movies (as you're doing in your post). Note: as the movie ID comes from external input, it must be ensured that it corresponds to an actual movie ID, otherwise a user could enter an illegal movie ID crashing your application when trying to access the element in m_Movies.



      void GenerateResults(std::ostream& os) const
      {
      std::vector<Rating> ratings(m_Ratings);
      std::sort(ratings.begin(), ratings.end(),
      (r1, r2){ return r1.GetAverageRating() < r2.GetAverageRating(); });

      std::for_each(ratings.begin(), ratings.end(), [&](Rating const& r)
      {
      auto id = r.GetID();
      os << id << " " << r.GetAverageRating() << " " << m_Movies[id-1].GetName() <<"n";
      });
      }
      ```





      share|improve this answer








      New contributor




      Cornholio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      $endgroup$
















        1












        1








        1





        $begingroup$

        No need to copy the movies. Copy the ranges and sort it by average, use the id as index to movies (as you're doing in your post). Note: as the movie ID comes from external input, it must be ensured that it corresponds to an actual movie ID, otherwise a user could enter an illegal movie ID crashing your application when trying to access the element in m_Movies.



        void GenerateResults(std::ostream& os) const
        {
        std::vector<Rating> ratings(m_Ratings);
        std::sort(ratings.begin(), ratings.end(),
        (r1, r2){ return r1.GetAverageRating() < r2.GetAverageRating(); });

        std::for_each(ratings.begin(), ratings.end(), [&](Rating const& r)
        {
        auto id = r.GetID();
        os << id << " " << r.GetAverageRating() << " " << m_Movies[id-1].GetName() <<"n";
        });
        }
        ```





        share|improve this answer








        New contributor




        Cornholio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        $endgroup$



        No need to copy the movies. Copy the ranges and sort it by average, use the id as index to movies (as you're doing in your post). Note: as the movie ID comes from external input, it must be ensured that it corresponds to an actual movie ID, otherwise a user could enter an illegal movie ID crashing your application when trying to access the element in m_Movies.



        void GenerateResults(std::ostream& os) const
        {
        std::vector<Rating> ratings(m_Ratings);
        std::sort(ratings.begin(), ratings.end(),
        (r1, r2){ return r1.GetAverageRating() < r2.GetAverageRating(); });

        std::for_each(ratings.begin(), ratings.end(), [&](Rating const& r)
        {
        auto id = r.GetID();
        os << id << " " << r.GetAverageRating() << " " << m_Movies[id-1].GetName() <<"n";
        });
        }
        ```






        share|improve this answer








        New contributor




        Cornholio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Cornholio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 13 hours ago









        CornholioCornholio

        4416




        4416




        New contributor




        Cornholio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Cornholio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Cornholio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            Hitman Hunter is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            Hitman Hunter is a new contributor. Be nice, and check out our Code of Conduct.













            Hitman Hunter is a new contributor. Be nice, and check out our Code of Conduct.












            Hitman Hunter is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f212698%2fsorting-3-distinct-vectors-based-on-one-of-them%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga