Pass operator as function template parameter - c++

I have to overload the basic arithmetic operators for some very complicated objects I've made. So far, I've successfully implemented operator*; now I need operator+, etc. The code for operator* is very large, but the only difference between operator* and operator+ will be one line where I use + instead of * on some complex numbers. This line will be inside of a loop that gets called many many times, so I want it to be efficient, which would seem to imply no function pointers. (Correct me if I'm wrong.)
This seems like a perfect use for templates. But I'm at a loss as to the correct syntax. I'm thinking something like this inside the ComplicatedObject class definition:
template <typename ComplexBinaryOp>
ComplicatedObject BinaryOp(const ComplicatedObject& B) const {
// Do lots of stuff
for(unsigned int i=0; i<OneBazillion; ++i) {
// Here, the f[i] are std::complex<double>'s:
C.f[i] = ComplexBinaryOp(f[i], B.f[i]);
}
// Do some more stuff
return C;
}
inline ComplicatedObject operator*(const ComplicatedObject& B) const {
return BinaryOp<std::complex::operator*>(B);
}
inline ComplicatedObject operator+(const ComplicatedObject& B) const {
return BinaryOp<std::complex::operator+>(B);
}
This question is related: "function passed as template argument". But the functions passed as the template arguments are not operators.
I've fiddled with the syntax every way I can think of, but the compiler always complains of bad syntax. How should I do this?
Edit:
For clarity, I include the complete solution in terms of my code above, along with the additional generalizations people may need:
template <typename ComplexBinaryOp>
ComplicatedObject BinaryOp(const ComplicatedObject& B) const {
// Do lots of stuff
for(unsigned int i=0; i<OneBazillion; ++i) {
// Here, the f[i] are std::complex<double>'s:
C.f[i] = ComplexBinaryOp()(f[i], B.f[i]); // Note extra ()'s
}
// Do some more stuff
return C;
}
inline ComplicatedObject operator+(const ComplicatedObject& B) const {
return BinaryOp<std::plus<std::complex<double> > >(B);
}
inline ComplicatedObject operator-(const ComplicatedObject& B) const {
return BinaryOp<std::minus<std::complex<double> > >(B);
}
inline ComplicatedObject operator*(const ComplicatedObject& B) const {
return BinaryOp<std::multiplies<std::complex<double> > >(B);
}
inline ComplicatedObject operator/(const ComplicatedObject& B) const {
return BinaryOp<std::divides<std::complex<double> > >(B);
}

I think std::plus<std::complex> and std::multiplies<std::complex> are what you're looking for, but I'm not 100% sure I understand your question (is your code snippet within a class you aren't showing us?)

You have two options. Pass the function at runtime:
#include <functional>
template <typename ComplexBinaryOp>
ComplicatedObject BinaryOp(const ComplicatedObject& B, ComplexBinaryOp op) const {
// ...
C.f[i] = op(f[i], B.f[i]);
// ...
}
// functor wrapping member function pointer
BinaryOp(B, std::mem_fn(&std::complex<double>::operator+));
// standard-issue functor
BinaryOp(B, std::plus<std::complex<double>>());
Or pass it at compile-time:
// or another floating-point type
typedef double (*ComplexBinaryOp)(double, double);
template <ComplexBinaryOp op>
ComplicatedObject BinaryOp(const ComplicatedObject& B) const {
// ...
C.f[i] = op(f[i], B.f[i]);
// ...
}
// non-member function
template<class T>
std::complex<T> add_complex(const std::complex<T>& a, const std::complex<T>& b) {
return a + b;
}
// non-member function pointer
BinaryOp<add_complex<double>>(B);
I believe you can do the same with member function pointers as well by changing the definition of ComplexBinaryOp.

Related

when I am using this comparator function without static keyword it giving error [duplicate]

trying to compile the following code I get this compile error, what can I do?
ISO C++ forbids taking the address of
an unqualified or parenthesized
non-static member function to form a
pointer to member function.
class MyClass {
int * arr;
// other member variables
MyClass() { arr = new int[someSize]; }
doCompare( const int & i1, const int & i2 ) { // use some member variables }
doSort() { std::sort(arr,arr+someSize, &doCompare); }
};
doCompare must be static. If doCompare needs data from MyClass you could turn MyClass into a comparison functor by changing:
doCompare( const int & i1, const int & i2 ) { // use some member variables }
into
bool operator () ( const int & i1, const int & i2 ) { // use some member variables }
and calling:
doSort() { std::sort(arr, arr+someSize, *this); }
Also, isn't doSort missing a return value?
I think it should be possible to use std::mem_fun and some sort of binding to turn the member function into a free function, but the exact syntax evades me at the moment.
EDIT: Doh, std::sort takes the function by value which may be a problem. To get around this wrap the function inside the class:
class MyClass {
struct Less {
Less(const MyClass& c) : myClass(c) {}
bool operator () ( const int & i1, const int & i2 ) {// use 'myClass'}
MyClass& myClass;
};
doSort() { std::sort(arr, arr+someSize, Less(*this)); }
}
As Andreas Brinck says, doCompare must be static (+1). If you HAVE TO have a state in your comparator function (using the other members of the class) then you'd better use a functor instead of a function (and that will be faster):
class MyClass{
// ...
struct doCompare
{
doCompare( const MyClass& info ) : m_info(info) { } // only if you really need the object state
const MyClass& m_info;
bool operator()( const int & i1, const int & i2 )
{
// comparison code using m_info
}
};
doSort()
{ std::sort( arr, arr+someSize, doCompare(*this) ); }
};
Using a functor is always better, just longer to type (that can be unconvenient but oh well...)
I think you can also use std::bind with the member function but I'm not sure how and that wouldn't be easy to read anyway.
UPDATE 2014: Today we have access to c++11 compilers so you could use a lambda instead, the code would be shorter but have the exact same semantic.
The solution proposed by Rob is now valid C++11 (no need for Boost):
void doSort()
{
using namespace std::placeholders;
std::sort(arr, arr+someSize, std::bind(&MyClass::doCompare, this, _1, _2));
}
Indeed, as mentioned by Klaim, lambdas are an option, a bit more verbose (you have to "repeat" that the arguments are ints):
void doSort()
{
std::sort(arr, arr+someSize, [this](int l, int r) {return doCompare(l, r); });
}
C++14 supports auto here:
void doSort()
{
std::sort(arr, arr+someSize, [this](auto l, auto r) {return doCompare(l, r); });
}
but still, you declared that arguments are passed by copy.
Then the question is "which one is the most efficient". That question was treated by Travis Gockel: Lambda vs Bind. His benchmark program gives on my computer (OS X i7)
Clang 3.5 GCC 4.9
lambda 1001 7000
bind 3716166405 2530142000
bound lambda 2438421993 1700834000
boost bind 2925777511 2529615000
boost bound lambda 2420710412 1683458000
where lambda is a lambda used directly, and lambda bound is a lambda stored in a std::function.
So it appears that lambdas are a better option, which is not too much of a surprise since the compiler is provided with higher level information from which it can make profit.
You can use boost::bind:
void doSort() {
std::sort(arr,arr+someSize, boost::bind(&MyClass::doCompare, this, _1, _2));
}
There is a way to do what you want, but you need to use a small adaptor. As the STL doesn't write it for you, can can write it yourself:
template <class Base, class T>
struct adaptor_t
{
typedef bool (Base::*method_t)(const T& t1, const T& t2));
adaptor_t(Base* b, method_t m)
: base(b), method(m)
{}
adaptor_t(const adaptor_t& copy) : base(copy.base), method(copy.method) {}
bool operator()(const T& t1, const T& t2) const {
return (base->*method)(t1, t2);
}
Base *base;
method_t method;
}
template <class Base, class T>
adaptor_t<Base,T> adapt_method(Base* b, typename adaptor_t<Base,T>::method_t m)
{ return adaptor_t<Base,T>(b,m); }
Then, you can use it:
doSort() { std::sort(arr,arr+someSize, adapt_method(this, &doCompare)); }
The third argument in the calling of std::sort() is not compatible to the function pointer needed by std::sort(). See my answer to another question for a detailed explanation for why a member function signature is different from a regular function signature.
just make your helper function, static which you are going to pass inside the sort function.
for e.g
struct Item
{
int val;
int id;
};
//Compare function for our Item struct
static bool compare(Item a, Item b)
{
return b.val>a.val;
}
Now you can pass this inside your sort function
A very simple way to effectively use a member function is to use operator<. That is, if you have a function called compare, you can call it from operator<. Here is a working example:
class Qaz
{
public:
Qaz(int aX): x(aX) { }
bool operator<(const Qaz& aOther) const
{
return compare(*this,aOther);
}
static bool compare(const Qaz& aP,const Qaz& aQ)
{
return aP.x < aQ.x;
}
int x;
};
Then you don't even need to give the function name to std::sort:
std::vector<Qaz> q;
q.emplace_back(8);
q.emplace_back(1);
q.emplace_back(4);
q.emplace_back(7);
q.emplace_back(6);
q.emplace_back(0);
q.emplace_back(3);
std::sort(q.begin(),q.end());
Updating Graham Asher answer, as you don't need the compare but can use the less operator directly.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Qaz {
public:
Qaz(int aX): x(aX) { }
bool operator<(const Qaz& aOther) const {
return x < aOther.x;
}
int x;
};
int main() {
std::vector<Qaz> q;
q.emplace_back(8);
q.emplace_back(1);
q.emplace_back(4);
q.emplace_back(7);
q.emplace_back(6);
q.emplace_back(0);
q.emplace_back(3);
std::sort(q.begin(),q.end());
for (auto& num : q)
std::cout << num.x << "\n";
char c;
std::cin >> c;
return 0;
}

how to store the logical relationship

I want to figure out a solution for automatic logical relationship check. For example, I have a function IsGood(), it will get the bool value from a, b, c .... In the main program, there is if(a||b) or if(b&&c) or if(g&&!k&&l||!z), different relationship. I want to replace all of them with IsGood(), and I want to make this function more general, it can handle different logical relationship.
So my idea is to put some ID, which will help this function to know which variables are required to handle now, for example, IsGood() got value k1,k2,k3, but the logical relationship ||,&& between k1,k2,k3 are not known by IsGood().
So I want to know how to let IsGood() automatically get the relationship between values. Store them in database??
Like : IsGood() firstly check that it is in the place1, so it queries the database, the result is : (this why I don't take parameters in IsGood(), it will retrieve the variables it needs from database or configuration file, what it needs is only the placeID.)
place 1 (the place number); k1,k2,k3 (variable name); true,true,false(value); &&, || (logical relationship).
But I don't think it is good...So, could you give me some ideas? Thanks a lot! My work is based on C++.
I want to know some ideas about this :
a||b&&c, I can store the information, like 0,1, so 0 represents ||, 1 represents &&, so the structure like a&&b||c...is easy to control.
But how to set (a||b)&&c? I also want to find a way to record this relationship. A smart method will be appreciated!! Thanks.
This can't work. Period.
In C++, variables have scope. The name k1 may mean different things in different places. Therefore, even if the function IsGood magically knew that it somehow should access a variable named k1, it still has no way whatsoever to figure out which k1 from which scope that would be.
This is not a big deal for C++ programmers. Their solution: IsGood(k1), which means: call IsGood with this k1 variable from the current scope, and not another.
Now, passing operators is a bit harder. You need lambda's for that: IsGood( [&k1,&k2,&k3](){return (k1&&k2)||k3;} );. This takes a reference to the variables k1-3, and passes the expression (k1&&k2)||k3; to IsGood. Or in two lines:
auto myLambda = [&k1,&k2,&k3](){return (k1&&k2)||k3;} ;
IsGood(myLambda);
Again, this all works because you pass IsGood the information it needs. It can't get it any other way.
I would first start off by defining a set of logical operations that work on a given object, for example:
// This is just a simple wrapper for the first argument
template <typename T>
struct FirstOp
{
FirstOp(T const& v) : _v(v)
{ }
T const & operator*() const { return _v; }
T const& _v;
};
template <typename T>
struct AndOp
{
AndOp(T const& v) : _v(v)
{ }
T const & operator*() const { return _v; }
// Then hack the stream operator
template <typename O>
O const & operator>>(O const & o) const
{
if (o)
o = o && _v; // assumes T supports safe bool
return o;
}
T const& _v;
};
template <typename T>
struct OrOp
{
OrOp(T const& v) : _v(v)
{ }
T const& operator*() const { return _v; }
// Then hack the stream operator
template <typename O>
O const & operator>>(O const & o) const
{
if (!o)
o = o || _v; // assumes T supports safe bool
return o;
}
T const& _v;
};
template <typename Op1>
struct ResultOf
{
ResultOf(Op1 const& cOp) : _o1(cOp), _r(*_o1)
{ }
ResultOf const & operator=(bool r) const
{ _r = r; return *this; }
operator bool() const { return _r; }
// Then hack the stream operator
template <typename O>
ResultOf& operator>>(O& o)
{
o >> *this;
return *this;
}
Op1 const& _o1;
mutable bool _r;
};
Then define a IsGood to accept parameters, overload to support more parameters.
template <typename T1, typename T2>
bool IsGood(T1 const& t1, T2 const& t2)
{
return ResultOf<T1>(t1) >> t2;
}
Then you can call as follows.
int main(void)
{
std::cout << IsGood(FirstOp<int>(0), OrOp<int>(1)) << std::endl;
}
So what this approach has allowed you to do is to wrap the value that you want to use for a specific logical operation with that operation and then pass it to the generic IsGood function. Now here, the actual operators that are constructed is hard-coded, but there is nothing that prevents you reading this from a file for example and then constructing the appropriate operators to pass to IsGood. NOTE: The above is short-circuiting, so will only evaluate the arguments as necessary (function calls will be made), but expressions will not be evaluated. You should be able to use the above approach to make arbitrarily complex logical relationships.
DISCLAIMER: This is my limited understanding of your problem... if it's off the mark, ah well...

Comparison with stl sort()

I'm trying to use stl sort() in a class function. I would like to sort an array of structs that look like this:
struct foo{
double num;
std::string s;
};
with a comparison function like this:
bool aGreaterThanb(foo a, foo b){
if (a.num > b.num){
if(a.num == b.num){
if (anotherOutsideComparison(a.s, b.s)){
return true;
}
}
else
return true;
}
else
return false;
}
But I'm not sure how I can format this to get it to compile. How should I format this so I can call sort(fooarray[0], fooarray[end], aGreaterThanb);? (An example would be great)
Write your comparison function as the operator() method of a structure called a functor:
struct aGreaterThanb
{
bool operator() (const foo& a, const foo& b)
{
// return true iff a is strictly less than b according to your ordering
}
};
Then pass an instance of that functor object to std::sort:
std::sort(fooarray.begin(), fooarray.end(), aGreaterThanb());
If you are using an array of foo like this:
foo fooarray[Foos];
...
sort(fooarray, fooarray + Foos, &aGreaterThanb);
The above code would sort your array in reverse order, since sort expects a less-than comparator.
Additionally to avoid copying a lot of foo-objects around just for comparison, declare your comparator to take const foo& instead of foo as arguments.
bool aGreaterThanb(const foo& a, const foo& b) {
You're supposed to pass iterators — a generalized superset of pointers — to the STL sort function:
std::sort(fooarray, fooarray + end, &aGreaterThanb);
It works just as you want already:
#include <algorithm>
int main()
{
foo data[10];
std::sort(&data[0], &data[10], aGreaterThanb);
}
But you have syntax error. You are missing a brace:
return true;
} // <--- Missing this line
else
return false;
For efficiency you should pass by const reference:
bool aGreaterThanb(foo const& a, foo const& b){
Note that in worst case sort function is up to N^2 comparsions.
And stable_sort complexity is between N*logN and N*(LogN^2)
Make it an operator.
struct foo {
double num;
std::string s;
};
bool operator>(const foo& a, const foo& b) {
return (
(a.num > b.num) ||
((a.num == b.num) &&
anotherOutsideComparison(a.s, b.s))
);
}
// note: std::sort expects operator<
bool operator<(const foo& a, const foo& b) {
return b > a;
}
If you really want to sort using operator>, pass std::greater<foo>() as the functor.
std::sort(foos.begin(), foos.end(), std::greater<foo>());

Can we declare a function with the same signature but different return type in the base class?

the question may look silly ,but i want to ask..
Is there any way we can declare a method in a class with same signature but different return type (like int fun(int) and float fun(int) ) and during the object creation can we dynamically decide which function to be executed! i have got the compilation error...is there any other way to achieve this logic may be using templates...
You can always take the return value as a template.
template<typename T> T fun(int);
template<> float fun<float>(int);
template<> int fun<int>(int);
Can you decide dynamically at run-time which to call? No.
#DeadMG proposed the template based solution, however you can simply "tweak" the signature (which is, arguably, what the template argument does).
The idea is simply to add a dummy argument:
struct Foo
{
float fun(float); // no name, it's a dummy
int fun(int); // no name, it's a dummy
};
Then for execution:
int main() {
Foo foo;
std::cout << foo.fun(int()) << ", " << foo.fun(float());
}
This can be used exactly as the template solution (ie invoked from a template method), but is much easier to pull:
less wordy
function template specialization should be defined outside the class (although VC++ will accept inline definition in the class)
I prefer to avoid function template specialization, in general, as with specialization on arguments, the rules for selecting the right overload/specialization are tricky.
You can (but shouldn't*) use a proxy class that overloads the conversion operators.
Long example with actual usecase *
Let me take my example from Dot & Cross Product Notation:
[...]
There is also the possibility of having operator* for both dot-product and cross-product.
Assume a basic vector-type (just for demonstration):
struct Vector {
float x,y,z;
Vector() {}
Vector (float x, float y, float z) : x(x), y(y), z(z) {}
};
We observe that the dot-product is a scalar, the cross-product is a vector. In C++, we may overload conversion operators:
struct VecMulRet {
public:
operator Vector () const {
return Vector (
lhs.y*rhs.z - lhs.z*rhs.y,
lhs.z*rhs.x - lhs.x*rhs.z,
lhs.x*rhs.y - lhs.y*rhs.x
);
}
operator float () const {
return lhs.x*rhs.x + lhs.y*rhs.y + lhs.z*rhs.z;
}
private:
// make construction private and only allow operator* to create an instance
Vector const lhs, rhs;
VecMulRet (Vector const &lhs, Vector const &rhs)
: lhs(lhs), rhs(rhs)
{}
friend VecMulRet operator * (Vector const &lhs, Vector const &rhs);
};
Only operator* is allowed to use struct VecMulRet, copying of VecMulRet is forbidden (paranoia first).
Operator* is now defined as follows:
VecMulRet operator * (Vector const &lhs, Vector const &rhs) {
return VecMulRet (lhs, rhs);
}
Et voila, we can write:
int main () {
Vector a,b;
float dot = a*b;
Vector cross = a*b;
}
Btw, this is blessed by the Holy Standard as established in 1999.
If you read further in that thread, you'll find a benchmark that confirms that this comes at no performance penalty.
Short example for demonstration *
If that was too much to grasp, a more constructed example:
struct my_multi_ret {
operator unsigned int() const { return 0xdeadbeef; }
operator float() const { return 42.f; }
};
my_multi_ret multi () {
return my_multi_ret();
}
#include <iostream>
#include <iomanip>
int main () {
unsigned int i = multi();
float f = multi();
std::cout << std::hex << i << ", " << f << std::endl;
}
* You can, but shouldn't, because it does not conform to the principle of least surprise as it is not common practice. Still, it is funny.

problem sorting using member function as comparator

trying to compile the following code I get this compile error, what can I do?
ISO C++ forbids taking the address of
an unqualified or parenthesized
non-static member function to form a
pointer to member function.
class MyClass {
int * arr;
// other member variables
MyClass() { arr = new int[someSize]; }
doCompare( const int & i1, const int & i2 ) { // use some member variables }
doSort() { std::sort(arr,arr+someSize, &doCompare); }
};
doCompare must be static. If doCompare needs data from MyClass you could turn MyClass into a comparison functor by changing:
doCompare( const int & i1, const int & i2 ) { // use some member variables }
into
bool operator () ( const int & i1, const int & i2 ) { // use some member variables }
and calling:
doSort() { std::sort(arr, arr+someSize, *this); }
Also, isn't doSort missing a return value?
I think it should be possible to use std::mem_fun and some sort of binding to turn the member function into a free function, but the exact syntax evades me at the moment.
EDIT: Doh, std::sort takes the function by value which may be a problem. To get around this wrap the function inside the class:
class MyClass {
struct Less {
Less(const MyClass& c) : myClass(c) {}
bool operator () ( const int & i1, const int & i2 ) {// use 'myClass'}
MyClass& myClass;
};
doSort() { std::sort(arr, arr+someSize, Less(*this)); }
}
As Andreas Brinck says, doCompare must be static (+1). If you HAVE TO have a state in your comparator function (using the other members of the class) then you'd better use a functor instead of a function (and that will be faster):
class MyClass{
// ...
struct doCompare
{
doCompare( const MyClass& info ) : m_info(info) { } // only if you really need the object state
const MyClass& m_info;
bool operator()( const int & i1, const int & i2 )
{
// comparison code using m_info
}
};
doSort()
{ std::sort( arr, arr+someSize, doCompare(*this) ); }
};
Using a functor is always better, just longer to type (that can be unconvenient but oh well...)
I think you can also use std::bind with the member function but I'm not sure how and that wouldn't be easy to read anyway.
UPDATE 2014: Today we have access to c++11 compilers so you could use a lambda instead, the code would be shorter but have the exact same semantic.
The solution proposed by Rob is now valid C++11 (no need for Boost):
void doSort()
{
using namespace std::placeholders;
std::sort(arr, arr+someSize, std::bind(&MyClass::doCompare, this, _1, _2));
}
Indeed, as mentioned by Klaim, lambdas are an option, a bit more verbose (you have to "repeat" that the arguments are ints):
void doSort()
{
std::sort(arr, arr+someSize, [this](int l, int r) {return doCompare(l, r); });
}
C++14 supports auto here:
void doSort()
{
std::sort(arr, arr+someSize, [this](auto l, auto r) {return doCompare(l, r); });
}
but still, you declared that arguments are passed by copy.
Then the question is "which one is the most efficient". That question was treated by Travis Gockel: Lambda vs Bind. His benchmark program gives on my computer (OS X i7)
Clang 3.5 GCC 4.9
lambda 1001 7000
bind 3716166405 2530142000
bound lambda 2438421993 1700834000
boost bind 2925777511 2529615000
boost bound lambda 2420710412 1683458000
where lambda is a lambda used directly, and lambda bound is a lambda stored in a std::function.
So it appears that lambdas are a better option, which is not too much of a surprise since the compiler is provided with higher level information from which it can make profit.
You can use boost::bind:
void doSort() {
std::sort(arr,arr+someSize, boost::bind(&MyClass::doCompare, this, _1, _2));
}
There is a way to do what you want, but you need to use a small adaptor. As the STL doesn't write it for you, can can write it yourself:
template <class Base, class T>
struct adaptor_t
{
typedef bool (Base::*method_t)(const T& t1, const T& t2));
adaptor_t(Base* b, method_t m)
: base(b), method(m)
{}
adaptor_t(const adaptor_t& copy) : base(copy.base), method(copy.method) {}
bool operator()(const T& t1, const T& t2) const {
return (base->*method)(t1, t2);
}
Base *base;
method_t method;
}
template <class Base, class T>
adaptor_t<Base,T> adapt_method(Base* b, typename adaptor_t<Base,T>::method_t m)
{ return adaptor_t<Base,T>(b,m); }
Then, you can use it:
doSort() { std::sort(arr,arr+someSize, adapt_method(this, &doCompare)); }
The third argument in the calling of std::sort() is not compatible to the function pointer needed by std::sort(). See my answer to another question for a detailed explanation for why a member function signature is different from a regular function signature.
just make your helper function, static which you are going to pass inside the sort function.
for e.g
struct Item
{
int val;
int id;
};
//Compare function for our Item struct
static bool compare(Item a, Item b)
{
return b.val>a.val;
}
Now you can pass this inside your sort function
A very simple way to effectively use a member function is to use operator<. That is, if you have a function called compare, you can call it from operator<. Here is a working example:
class Qaz
{
public:
Qaz(int aX): x(aX) { }
bool operator<(const Qaz& aOther) const
{
return compare(*this,aOther);
}
static bool compare(const Qaz& aP,const Qaz& aQ)
{
return aP.x < aQ.x;
}
int x;
};
Then you don't even need to give the function name to std::sort:
std::vector<Qaz> q;
q.emplace_back(8);
q.emplace_back(1);
q.emplace_back(4);
q.emplace_back(7);
q.emplace_back(6);
q.emplace_back(0);
q.emplace_back(3);
std::sort(q.begin(),q.end());
Updating Graham Asher answer, as you don't need the compare but can use the less operator directly.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Qaz {
public:
Qaz(int aX): x(aX) { }
bool operator<(const Qaz& aOther) const {
return x < aOther.x;
}
int x;
};
int main() {
std::vector<Qaz> q;
q.emplace_back(8);
q.emplace_back(1);
q.emplace_back(4);
q.emplace_back(7);
q.emplace_back(6);
q.emplace_back(0);
q.emplace_back(3);
std::sort(q.begin(),q.end());
for (auto& num : q)
std::cout << num.x << "\n";
char c;
std::cin >> c;
return 0;
}