DEQUE and sorting

I'd like to use DEQUE for numbers 1 -100 and place the odd numbers on the front of the list and the even numbers on the back of the list and  then print them to the screen.
Wookie68Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
See http://www.sgi.com/tech/stl/partition.html

Example
Reorder a sequence so that even numbers precede odd numbers.

int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int N = sizeof(A)/sizeof(int);
partition(A, A + N,
          compose1(bind2nd(equal_to<int>(), 0),
                   bind2nd(modulus<int>(), 2)));
copy(A, A + N, ostream_iterator<int>(cout, " "));
// The output is "10 2 8 4 6 5 7 3 9 1". [1]
Wookie68Author Commented:
jkr -
Thanks for the code. I'm attempting to work an exercise in a book (trying to learn the basics of C++ with several different books) and I was looking for a way to do it using DEQUE and not pushing each individual value on the deque.

Thanks again - joe
jkrCommented:
>>I was looking for a way to do it using DEQUE and not pushing each individual value on the deque.

You can do that using 'copy()', e.g. like

#include <algorithm>
#include <functional>
#include <deque>
#include <iostream>

using namespace std;

int main () {

int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const int N = sizeof(A)/sizeof(int);

deque<int> dq;
copy(A, A + N, dq.begin());
partition(dq.begin(), dq.end(),
          compose1(bind2nd(equal_to<int>(), 0),
                   bind2nd(modulus<int>(), 2)));
copy(A, A + N, ostream_iterator<int>(cout, " "));
// The output is "10 2 8 4 6 5 7 3 9 1". [1]

return 0;
}
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

PaulCaswellCommented:
Hi Wookie68,

This seems to work:

// Return whether first element is greater than the second but odds must come first.
bool EOGreater ( int i1, int i2 )
{
    return ( (i1 & 1) == 1 ?
        ((i2 & 1) == 1 ? /* Both Odd */ i1 < i2: /* Odd Even */ true):
        ((i2 & 1) == 1 ? /* Even Odd */ false: /* Even Even */ i1 < i2));
}

int _tmain(int argc, _TCHAR* argv[])
{
    deque <int> d;

    // Fill it.
    for ( int i = 1; i < 10; i++ ) d.push_back(i);
    // Sort it.
    sort(d.begin(),d.end(),EOGreater);
    // Print it.
    for ( deque <int>::iterator i1 = d.begin( ) ; i1 != d.end( ) ; i1++ )
      cout << *i1 << " ";

    return 0;
}


Paul

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wookie68Author Commented:
Can you tell me what the compose1 is above? When I compile the code, I get "compose1" identifier not found.
Wookie68Author Commented:
Paul -

Thanks for the code. I'm using Visual C++ to test with. Is this the correct way to implement your code? I'm still learning so please excuse the question if it seems too low level. Thanks - Joe

#include <algorithm>
#include <functional>
#include <deque>
#include <iostream>
#include <list>

using namespace std;


// Return whether first element is greater than the second but odds must come first.
bool EOGreater ( int i1, int i2 )
{
    return ( (i1 & 1) == 1 ?
        ((i2 & 1) == 1 ? /* Both Odd */ i1 < i2: /* Odd Even */ true):
        ((i2 & 1) == 1 ? /* Even Odd */ false: /* Even Even */ i1 < i2));
}

int _tmain(int argc, _TCHAR* argv[])
{
    deque <int> d;

    // Fill it.
    for ( int i = 1; i < 10; i++ ) d.push_back(i);
    // Sort it.
    sort(d.begin(),d.end(),EOGreater);
    // Print it.
    for ( deque <int>::iterator i1 = d.begin( ) ; i1 != d.end( ) ; i1++ )
      cout << *i1 << " ";

    return 0;
}
PaulCaswellCommented:
Hi Wookie68,

If it compiles and runs and produces the results you want then it should be fine.

Note that jkr is FAR more experienced in C++ than I am so his method is probably much more efficient. I am still playing with C++ so I sometrimes do things in unusual ways. If you are learning C++ you may find it worthwhile understanding his method, I certainly dont :-)

Paul
PaulCaswellCommented:
Hi Wookie68,

Actually, thinking about it, a more correct implementation of what you are asking for would use:

bool EOGreater ( int i1, int i2 )
{
    return((i1 & 1) > (i2 & 1));
}

The one I posted earlier will also sort the rest of the numbers, that is not required.

Paul
Wookie68Author Commented:
Paul -
i'm very new as well... Thanks for trying to help me out.

When I compile the code I posted above I get syntax error:identifiier  "_TCHAR" I'll play with it some more to see if I can get it to complile.
Wookie68Author Commented:
Paul -

Thanks for the help. I tried jkr's code also, but the compiler did not like the compose1 portion of it. I ended up using your code to get it to work. I'd like to understand jkr's code as well. I have read a ton of posts by him on here and agree...he definitely knows his stuff.


#include <algorithm>
#include <functional>
#include <deque>
#include <iostream>
#include <list>

using namespace std;


// Return whether first element is greater than the second but odds must come first.
bool EOGreater ( int i1, int i2 )
{
    return ( (i1 & 1) == 1 ?
        ((i2 & 1) == 1 ? /* Both Odd */ i1 < i2: /* Odd Even */ true):
        ((i2 & 1) == 1 ? /* Even Odd */ false: /* Even Even */ i1 < i2));
}

int main()
{
    deque <int> d;

    // Fill it.
    for ( int i = 1; i < 101; i++ ) d.push_back(i);
    // Sort it.
    sort(d.begin(),d.end(),EOGreater);
    // Print it.
    for ( deque <int>::iterator i1 = d.begin( ) ; i1 != d.end( ) ; i1++ )
      cout << *i1 << " ";
      system("pause");
    return 0;
}
jkrCommented:
?
Wookie68Author Commented:
jkr

I couldn't get your code to compile completely... it kept hanging on the compose1 part. I noticed it was from an SGI page. Is it possible that it will compile under Unix and not Microsoft Visual C++?
Wookie68Author Commented:
Paul -
Not sure if I am allowed to ask under same question, but can you tell me how this section of your code above works?

bool EOGreater ( int i1, int i2 )
{
    return ( (i1 & 1) == 1 ?
       ((i2 & 1) == 1 ? /* Both Odd */ i1 < i2: /* Odd Even */ true):
       ((i2 & 1) == 1 ? /* Even Odd */ false: /* Even Even */ i1 < i2));
}
PaulCaswellCommented:
// If the first value is odd
(i1 & 1) == 1 ?
       (
              // If the second value is odd then the difference is the difference between the two numbers.
              (i2 & 1) == 1 ? /* Both Odd */ i1 < i2:
              // If the second value is even then odd is ALWAYS less than even.
              /* Odd Even */ true):
// The first value is not odd. Similar logic.
       ((i2 & 1) == 1 ? /* Even Odd */ false: /* Even Even */ i1 < i2)


As I said above, you'd be better to use:

bool EOGreater ( int i1, int i2 )
{
    return((i1 & 1) > (i2 & 1));
}

as its simpler and is just saying 'odd numbers are less than even numbers'.

Paul
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.