Hello, consider this complete program (sorry, it's not minimal but I hope
it's readable at least):
#include <algorithm>
#include <iostream>
#include <vector>
class Row
{
public:
Row(int row)
:
m_row(row) {}
int get_row() const
{
return m_row;
}
private:
int m_row;
};
static void print_collection(const std::vector<Row>&);
bool less_wrt_row(const Row& lhs, const Row& rhs)
{
return lhs.get_row() < rhs.get_row();
}
int
main()
{
std::vector<Row> collection;
collection.push_back(Row(39));
collection.push_back(Row(3));
collection.push_back(Row(202));
collection.push_back(Row(1));
std::cout << "Before sorting:" << std::endl;
print_collection(collection);
std::sort(collection.begin(), collection.end(), less_wrt_row);
std::cout << "After sorting:" << std::endl;
print_collection(collection);
return 0;
}
static void
print_collection(const std::vector<Row>& collection)
{
std::vector<Row>::const_iterator itr = collection.begin();
while(itr != collection.end())
{
std::cout << itr->get_row() << std::endl;
++itr;
}
}
This program compiles and links and runs without noticable error. But if I
change the type of collection from std::vector to std::list, my compiles
becomes very displeased with me. The output is very long and quite
unreadable for a beginner like me. I assume std::list and std::vector are
not "compatible" with each other with regards to the sorting functions in
the standard library. But say I do have a std::list that I want sorted using
a binary predicate, how should I do it? Am I crazy wanting a std::list if I
want it to be sorted? What data structure should I use then?
Thanks for any replies