C++ Sort Algorithm for data structures [duplicate] - c++

This question already has an answer here:
Ordered sort in STL containers
(1 answer)
Closed 9 years ago.
I want to use the stl sort algorithm to sort some numbers, but i also want to remember their initial position.
I have a data structure like this:
struct Numbers {
int position;
int value;
};
I have created a vector of Numbers like this:
vector<Numbers> a;
How to use the stl sort algorithm, such that i sort the data structures based on the value?

You can use a functor too :
struct comp {
bool operator()(const Numbers &lhs, const Numbers& rhs) const{
lhs.value < rhs.value;
}
};
std::sort(a.begin(),a.end(), comp());
With C++11, you can use a lambda function :
std::sort( a.begin() , a.end() ,
[](const Numbers& lhs , const Numbers& rhs)
{ return lhs.value < rhs.value; }
);

You'll need to overload the "<" operator, like so:
bool Numbers::operator<(Numbers temp)
{
return value < temp.value;
}

Use std::sort and provide a custom comparator (template arg Compare)
#include <algorithm>
#include <vector>
//...
std::vector<Numbers> a;
//fill the vector a and set Numbers::position of each element accordingly...
struct {
bool operator()(const Numbers& a,const Numbers& b)const
{
return a.value < b.value;
}
} my_comparator;
std::sort(a.begin(),a.end(),my_comparator);
//...

Related

<set> with custom struct contains duplicates

I've been learning c++. I am stuck with this problem.
I have set that contains a custom struct that contains two long int's a & b. I have a custom comparer struct that compares the numbers and returns true if either a or b is different.
typedef long int li;
struct number {
number(li a1,li b1): a(a1), b(b1) {}
li a, b;
};
struct compare {
bool operator() (const number &lhs, const number& rhs) const{
return lhs.a != rhs.a || lhs.b != rhs.b;
}
};
int main() {
set<number, compare> nums;
nums.insert(number(1, 2));
nums.insert(number(1, 1));
nums.insert(number(2, 1));
nums.insert(number(1, 2));
for (auto &i : nums) {
cout << i.a << " " << i.b << endl;
}
return 0;
}
The output here is
1 2
2 1
1 1
1 2
It has two entries of 1 2. Any clarification would be appreciated.
Your comparison function should return whether some element is smaller than another, not whether or not they are equal. (More formally, it must define a "Strict weak ordering" on the elements of your set.)
Use something like
struct compare {
bool operator() (const number &lhs, const number& rhs) const{
return std::tie(lhs.a, lhs.b) < std::tie(rhs.a, rhs.b);
}
};
If you don't care about ordering, you may want to define a suitable hash function for your type and use std::unordered_set.
To avoid future problems like this, make sure to read the docs. They clearly explain what your comparison function is supposed to do.
For reference: std::tie as used above constructs tuples of references to its arguments which can then be compared lexicographically with <. This is an easy, generic and fast way to build some ordering for collections of less-than-comparable stuff.
Your comparison function needs to meet strict/weak ordering requirements.
(I actually prefer the answer using std::tie, but this may be more illustrative for newcomers)
bool compare(const number& lhs, const number& rhs)
{
if(lhs.a < rhs.a)
return true;
else if(lhs.a > rhs.a)
return false;
else
return lhs.b < rhs.b;
}

Two overloading functions for the same operator and class

I want to use a sorting algorithm to sort my vector of clients, but the problem is I have two different criteria to sort them.
To display them to the screen and to save them into a file I need to sort them by ID, but to use it for some other stuff (like knowing the top ten worst clients) I need to sort them by the sum of money they've spent.
These are the overloading functions of the operator== for the client class, but obviously they can't co-exist. Can someone give me a solution for this?
class Client
{
public:
//...
unsigned int getID() const;
double getSum() const;
//...
private:
unsigned int ID;
//...
double sum;
};
bool operator==(const Client &LHS, const Client &RHS)
{
return (LHS.getID() == RHS.getID());
}
bool operator==(const Client &LHS, const Client &RHS)
{
return (LHS.getSum() == RHS.getSum());
}
One of the std::sort function overloads takes a comparator, use that form and provide it with two independent functions or functors (or lambdas) for each instantiation.
class Client
{
public:
//...
unsigned int getID() const;
double getSum() const;
//...
private:
unsigned int ID;
//...
double sum;
};
bool CompareByID(const Client &LHS, const Client &RHS)
{
return (LHS.getID() < RHS.getID());
}
bool CompareBySum(const Client &LHS, const Client &RHS)
{
return (LHS.getSum() < RHS.getSum());
}
// ...
std::sort(container.begin(), container.end(), CompareByID);
Note the sort requires a comparison that obeys its ordering requirements, usually it uses a less than comparison to order the elements. The exact comparison can be different, but needs to obey the same ordering requirements (for further reading, see the information for the std::sort algorithm, and this on strict weak ordering).
As suggested you could use std:sort algorithm.
You need to create 2 function each for sum and Id comparison and pass them as function pointer for comparison.
bool compare_by_ID(const Client &LHS, const Client &RHS)
{
return (LHS.getID() < RHS.getID());
}
bool compare_by_sum(const Client &LHS, const Client &RHS)
{
return (LHS.getSum() < RHS.getSum());
}
and to use them you need to invoke sort in this way
vector<Client> v;
// assume v contains list of clients.
// for comparison by sum
std:sort(v.begin(),v.end(),compare_by_sum);
// for comparison by Id
std:sort(v.begin(),v.end(),compare_by_ID);
Apart from comparison function you could use more sophisticated approach by using functors or function objects.
Instead of relying on operator< to do your sorting, you can explicitly pass a comparator to std::sort:
std::vector<Client> vec;
...
auto sortByID = [](auto lhs, auto rhs){ return lhs.getID() < rhs.getID(); };
std::sort(vec.begin(), vec.end(), sortByID);
//vec is now sorted by ID
auto sortBySum = [](auto lhs, auto rhs){ return lhs.getSum() < rhs.getSum(); }
std::sort(vec.begin(), vec.end(), sortBySum);
//vec is now sorted by Sum

Multiple Overloading of Operators

as you can see from the code I want to overload the < operator twice. 1 to sort by dist and the other by nodeID. I would like to check if there is any way to call the different overloaded methods. For example in the compLoc method, when I use the sort() method I want it to be sorted by nodeID but in other methods I want it to be sorted by dist.
struct AttSet{
int nodeID;
double dist;
bool operator < (const AttSet & str) const{
return (dist < str.dist);
}
/*
bool operator <(const AttSet & str){
return (nodeID < str.nodeID);
*/
bool operator == (const AttSet & str){
return nodeID == str.nodeID;
}};
void compLoc(Edge *edge, vector<Node*> &vertices){
int l = edge->length;
int vl = edge->head->nodeID;
int vr = edge->tail->nodeID;
/*
sort(vertices[vl]->attSet.begin(), vertices[vl]->attSet.end());
sort(vertices[vr]->attSet.begin(), vertices[vr]->attSet.end());
vector<AttSet> vInterSec;
set_intersection(vertices[vl]->attSet.begin(), vertices[vl]->attSet.end(), vertices[vr]->attSet.begin(), vertices[vr]->attSet.end(), back_inserter(vInterSec));
*/}
You cannot have overloads that have the same signature. This holds for any function. How would you try to decide which version to use?
If you want sort the object based on different criteria you should use the sort version that takes a custom comparer function as the third argument.
Edit:
Of course you need to provide the comparer. I would suggest providing the comparers as static functions of the class if you have such power. This way you will not pollute enclosing namespace and you can access privates of the class with out exposing any getters. Since your properties are public the lambda would suffice, and probably be the best/cleanest approach.
Feeling adventurous I made a simple c++11 exercise program. For what it's worth, if you ever decided to go for proper encapsulation, I've shown both approaches:
#include <iostream>
#include <algorithm>
#include <vector>
#include <initializer_list>
#include <cassert>
using namespace std;
template<typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& v){
for(const auto& el : v){
out << el << '\n';
}
return out;
}
class A {
int a;
int b;
public:
A(std::initializer_list<int> l){
assert(l.size() == 2);
auto i = l.begin();
a = *i;
++i;
b = *i;
}
friend std::ostream& operator<<(std::ostream& stream, const A& e){
return stream << e.a << ' ' << e.b;
}
static bool compareViaA(const A& lhs, const A& rhs){
return rhs.a > lhs.a;
}
static bool compareViaB(const A& lhs, const A& rhs){
return rhs.b > lhs.b;
}
};
int main() {
std::vector<A> v {{2,3}, {3,2}, {1,4}, {4,1}};
//sort(v.begin(), v.end(), [](const A& a, const A& b){return a.a > b.a;}) // fails because of privacy violation
sort(v.begin(), v.end(), A::compareViaA);
std::cout << v << '\n';
sort(v.begin(), v.end(), A::compareViaB);
std::cout << v << '\n';
return 0;
}
Live: http://ideone.com/lDMujx.
I think you can implement this by using functor and take the comparator(operator< overload) outside the AttSet.
Here is a simple example:
struct AtrComparator {
bool distcmp;
AttrComparator(bool distcmp): distcmp(distcmp) {}
bool operator() (const AttSet &s1, const AttSet &s2) {
if(distcmp) {
return s1.dist < s2.dist;
} else {
return s1.nodeID < s2.nodeID;
}
}
}
And then you can do the sort through different feed, dist or nodeID.
.e.g:
sort(vertices[vl]->attSet.begin(), vertices[vl]->attSet.end(), AttComparator(true));
sort(vertices[vl]->attSet.begin(), vertices[vl]->attSet.end(), AttComparator(false));
You can't do that. They have the same signature exactly.
Use a functor or a lambda and pass it to whatever algorithm you want.
std::sort(std::begin(container), std::end(container),
[](const element_type& lhs, const element_type& rhs) { return ...; });
Another way to do this:
struct compare_by_node_id {
bool operator()(const AttSet& lhs, const AttSet& rhs) const {
return lhs.nodeID < rhs.nodeID;
}
};
struct compare_by_dist {
bool operator()(const AttSet& lhs, const AttSet& rhs) const {
return lhs.dist < rhs.dist;
}
};
And you could pass that to the algorithm like:
std::sort(std::begin(container), std::end(container), compare_by_node_id());
you cannot do that because compiler doesn't see difference between:
bool operator < (const AttSet & str) const; //this const doesn't allow to override any property of object(instance of AttSet) if I remember
and
bool operator < (const AttSet & str);
there're the same same return type, same parameter (same signature)
compiler cannot choose which one is better
There's not a great way to do this as far as I am aware, since the compiler will see these as the exact same and will throw an error. If you need to do this, use the < operator as whatever will occur the most often, and then write a method that you can call to compare two object. Something like this:
bool operator< (const Blah &blah) const {
return (most often operation)
}
bool Blah::other_operation(const Blah &blah) const {
return (other operation)
}

C++ Vector Range Constructor

I was looking over some C++ documentation when it occurred to me that the vector container doesn't have a constructor that 'easily' allows the user to pass a range of values - a min and a max - and have a vector constructed which has elements from min -> max. I thought this was odd so I tried writing my own and discovered it was non-trivial. Here's my solution.
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
template <typename T>
class MyIterator: public std::iterator<std::input_iterator_tag, int>
{
public:
MyIterator(T val):value(val) {}
MyIterator(const MyIterator & m):value(m.value) {}
MyIterator& operator ++()
{
++value;
return *this;
}
MyIterator operator ++(int)
{
MyIterator temp(*this);
operator ++();
return temp;
}
bool operator ==(const MyIterator & m) const { return value == m.value; }
bool operator !=(const MyIterator & m) const { return !(value == m.value); }
T& operator *() { return value; }
private:
T value;
};
int main(int argc, char** argv)
{
std::vector<int> my_vec(MyIterator<int>(100), MyIterator<int>(400));
std::copy(my_vec.begin(), my_vec.end(), std::ostream_iterator<int>(std::cout, "\n"));
return 0;
}
Does the new C++ have a solution for this?
In C++11 there is the std::iota function. Otherwise you have e.g. std::fill and std::fill_n, or std::generate and std::generate_n.
For C++11 there was no substantial work on changing the algorithms or containers to make them easier usable (there are a couple of new algorithms and the containers were made aware of allocators and rvalues). Neither is there a substantial change for C++14.
There is some hope for C++17, though: the Ranges Study Group is looking at concepts to make the algorithms and containers easier to use. That said, it isn't clear where that journey will travel to.

How to create set of integers with non-standard order in C++?

In C++03, I'd like to create a std::set where when iterating, one integer comes first, after that, I don't care what order, but I need an ordering to ensure there are no duplicates in the set. For example, if I had a set of years, and when iterating I want 2010 to be processed before all other years.
std::set<int> years;
// I do not know the set of years up front, so cannot just make a vector, plus
// there could potentially be duplicates of the same year inserted more than
// once, but it should only appear once in the resultant set.
years.insert(2000);
years.insert(2001);
years.insert(2010);
years.insert(2011);
years.insert(2013);
for (std::set<int>::iterator itr = years.begin(); itr != years.end(); ++itr) {
process_year(*itr);
}
Basically, I need to provide a comparator for which some year, known at runtime, (e.g. 2010) compares less than all other years, but the remaining years are ordered, but not in any necessary order, just ordered to ensure no duplicates in the set.
struct Comparer
{
int val;
Comparer(int v):val(v) {}
bool operator()(int lhs, int rhs) const {
if (rhs == val) return false;
if (lhs == val) return true;
return lhs < rhs;
}
};
To create an instance of the std::set that orders based on Comparer:
std::set<int, Comparer> instance( Comparer(2010) );
struct my_compare {
my_compare(int y) : allw_less(y) {}
bool operator() (const int& lhs, const int& rhs) const{
if(rhs == allw_less)
return false;
if(lhs == allw_less)
return true;
else
return lhs < rhs;
}
private:
int allw_less;
};
typedef std::set<int, my_compare> setType;
setType years(my_compare(2010));