0

I made this program for taking in input in two vectors ( long long int considering the fact that the numbers can be really large that a normal int cannot accomodate ) and then I wish to sort them.

#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; bool myfunct1( A first, A second ); bool myfunct2( A first, A second ); class A { public: long long int foo; }; int main (void) { vector<A> values1(1000); vector<A> values2(1000); int temp1 = 0; int temp2 = 0; int faltu; while (temp1<n) { cin>>faltu; values1[temp1].foo = faltu; temp1++; } while (temp2<n) { cin>>faltu; values2[temp2].foo = faltu; temp2++; } sort(values1.begin(), values1.end(),&myfunct1); // Sorting the elements in ascending order sort(values2.begin(), values2.end(),&myfunct2); // Sorting in descending order for ( i = 0; i < n; i++ ) printf("%lld",values1[i].foo); printf("\n"); for ( i = 0; i < n; i++ ) printf("%lld",values2[i].foo); printf("\n"); } bool myfunct1( A first, A second ) { if ( first.foo < second.foo ) return true; else return false; } bool myfunct2( A first, A second ) { if ( first.foo > second.foo ) return true; else return false; } 

When I see the output of the vectors after sorting, it is not correct. For eg for the input as : 2 1 3 7 8 9 ( for the two vectors ), it shows the output as, 000 987. Why is this happening? I think that the sort function is not functioning properly. But I did sorting after

8
  • 1
    Also, why use the if statement in the functions? You can just as well write return first.foo < second.foo; Commented Aug 7, 2014 at 15:45
  • 4
    vector<A> values1(1000) - what does this do? Commented Aug 7, 2014 at 15:49
  • 4
    What is n? How is it declared`How is it initialized? Commented Aug 7, 2014 at 15:49
  • 2
    "...for the input as : 2 1 3 7 8 9". Really? You created two vectors of 1000 elements each, all elements initialized to 0. Then you set some of these elements (3 in each vector apparently) to some non-zero values, leaving remaining 997 elements as zeros. And then you sorted the vectors. Why do you find it surprising that those 997 zeros drifted to the beginning of the vector after sorting in ascending order? Commented Aug 7, 2014 at 17:08
  • 1
    This is almost a sscce.org but is not compilable, since n is not declared. Commented Aug 7, 2014 at 17:13

1 Answer 1

3

The reason the first vector is printing 000 is probably because you initialize the vector with 1000 initial values, which probably default to 0. I am not 100% sure of this, but the reason the second sort works is because the largest numbers are placed first. In the case of the first, there are 997 0s before your 1,2,3. What I would suggest is (if your n is 3), initialize the vectors with just 3 and do your test.

Also, IDK why you are initializing your vector to have 1000 elements of type A immediately, but you can also attempt to test your program and the sort function by not initializing the vector with any elements and utilizing the push_back method on it instead to insert your variables.

Your syntax is otherwise perfectly alright, there should be no issue with the sort.

Sign up to request clarification or add additional context in comments.

1 Comment

The elements of the vector are initialized with A(); in this case, it zero initializes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.