CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: reverse sort not sorting???

1. ## reverse sort not sorting???

I have two vectors of float that I need to sort in reverse order. reverse() seems to work for one of them, but not the other. I have toggled between sort() and reverse(). for one vector, the expected behavior is seen and the order reverses. For the other, there is no change in order.

This is very simple code, so it's hard to imagine what is going wrong.

Code:
```vector<float> vec1, vec2;
vec1[0] = 14.1102; vec1[1] = 14.1145;
vec2[0] = 15.8508; vec2[1] = 26.0842;

sort( vec1.begin(), vec1.end() );
sort( vec2.begin(), vec2.end() );

cout << "vector 1 sort" << endl;
cout << vec1[0] << endl; cout << vec1[1] << endl;
cout << "vector 2 sort" << endl;
cout << vec2[0] << endl; cout << vec2[1] << endl;
cout << endl;
cout << endl;

reverse( vec1.begin(), vec1.end() );
reverse( vec2.begin(), vec2.end() );

cout << "vector 1 reverse" << endl;
cout << vec1[0] << endl; cout << vec1[1] << endl;
cout << "vector 2 sort" << endl;
cout << vec2[0] << endl; cout << vec2[1] << endl;
cout << endl;```
Printout is,

Code:
```vector 1 sort
14.1102
14.1145
vector 2 sort
15.8508
26.0842

vector 1 reverse
14.1102
14.1145
vector 2 reverse
26.0842
15.8508```
You can see that the order of the first vector did not change. Am I right in suspecting that the numbers are too similar for what ever method reverse() uses to determine the difference between values?

If that is not the problem, I have no idea where to look. Suggestions would be appreciated.

LMHmedchem

2. ## Re: reverse sort not sorting???

I found that if I use sort with greater<float>(), then it will work,

sort( vec2.begin(), vec2.end(), greater<float>() );

This gives the anticipated behavior for both vectors. Can someone explain why it doesn't work with reverse() and when you would choose to use the method above?

LMHmedchem

3. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: reverse sort not sorting???

That code caused a runtime error. I changed your vector assignments using push_back, ran the rest of the code and reversed vec1 as expected. Post your real code.
Last edited by GCDEF; July 15th, 2013 at 03:18 PM.

4. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: reverse sort not sorting???

Code:
```vector<float> vec1, vec2;
vec1[0] = 14.1102; vec1[1] = 14.1145;  // <---  ???
vec2[0] = 15.8508; vec2[1] = 26.0842; // <---  ???```
This is wrong from the outset, and leads to undefined behaviour.

A vector is initially empty. There are only a few ways to populate a vector:

1) vector::push_back()
2) vector::resize()
3) vector::insert()

Using vector::operator [] is not a way to add items to a vector, but you're doing that in this code. Therefore the code you posted, at least those three lines, renders the program null and void, and any behaviour after those lines are executed could be anything.

Regards,

Paul McKenzie

5. ## Re: reverse sort not sorting???

I guess I should have indicated that I was posting pseudo code, at least for the assignments. These vectors are populated with push_back and I have printed them to confirm their contents. This src code file is at least 3000 lines, so I just wanted to show the values that are currently in these vectors. I suppose that vec1[14.1102, 14.1145] might have been more conventional.

This definitely compiles and runs, so I am not at all sure why the first vector won't reverse sort, especially since the second method works fine.

Any insights?

LMHmedchem

6. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,518

## Re: reverse sort not sorting???

Originally Posted by LMHmedchem
I guess I should have indicated that I was posting pseudo code, at least for the assignments. These vectors are populated with push_back and I have printed them to confirm their contents. This src code file is at least 3000 lines, so I just wanted to show the values that are currently in these vectors. I suppose that vec1[14.1102, 14.1145] might have been more conventional.

This definitely compiles and runs, so I am not at all sure why the first vector won't reverse sort, especially since the second method works fine.

Any insights?

LMHmedchem
As I said, it worked for me once I used push_back. Without seeing your real code, who knows. Post a small, compilable example that reproduces the problem.

7. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: reverse sort not sorting???

Originally Posted by LMHmedchem
I guess I should have indicated that I was posting pseudo code, at least for the assignments.
What compiler and compiler version are you using?
These vectors are populated with push_back and I have printed them to confirm their contents. This src code file is at least 3000 lines, so I just wanted to show the values that are currently in these vectors. I suppose that vec1[14.1102, 14.1145] might have been more conventional.
If you did the same as what GCDEF did, what results did you get? If you get anything other than the obvious results, then your compiler library has a bug.

Also, it isn't a good idea to pluck lines out of a 3,000 line C++ program, not knowing what state your code, memory, data, etc. are in at runtime, and guess that this can be extrapolated to psuedo-code. C++ doesn't work that way -- if you have any sort of bug that could affect how your program runs internally, then you're going to be on a wild goose chase.

Regards,

Paul McKenzie

8. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: reverse sort not sorting???

Here is the code:
Code:
```#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;
int main()
{
vector<float> vec1(2), vec2(2);
vec1[0] = 14.1102; vec1[1] = 14.1145;
vec2[0] = 15.8508; vec2[1] = 26.0842;

sort( vec1.begin(), vec1.end() );
sort( vec2.begin(), vec2.end() );

cout << "vector 1 sort" << endl;
cout << vec1[0] << endl; cout << vec1[1] << endl;
cout << "vector 2 sort" << endl;
cout << vec2[0] << endl; cout << vec2[1] << endl;
cout << endl;
cout << endl;

reverse( vec1.begin(), vec1.end() );
reverse( vec2.begin(), vec2.end() );

cout << "vector 1 reverse" << endl;
cout << vec1[0] << endl; cout << vec1[1] << endl;
cout << "vector 2 sort" << endl;
cout << vec2[0] << endl; cout << vec2[1] << endl;
cout << endl;
}```
This produces the desired output.

Regards,

Paul McKenzie

9. ## Re: reverse sort not sorting???

I guess I should have indicated that I was posting pseudo code
It is always better to post actual code so that others can see exactly what you are doing. Not withstanding Paul's comment in post #4, a reason why reverse may not work is that the actual number of elements in the vector as stated by the size method is not the number of elements expected.

I've modified your code slightly to size the vectors at creation and to show the vector sizes before the reverse and also to display the vectors using iterators. In your own code that doesn't produce the expected results, what are the values of vec1.size() and vec2.size() just before the reverse? If they are not 2 then the problem is how you are manipulating/creating the vectors before the reverse. Also, as the vectors are initially sorted, you don't know whether sort has worked or not. IMO I think it hasn't. If you change the initial setup so that the numbers are in desending order rather than ascending order I think you'll find that what is being printed is just the original numbers as the sizes are not correct.

Code:
```#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

typedef vector<float> vecflt;
typedef vecflt::const_iterator viter;

void show_vec(const vecflt& vec)
{
for (viter i = vec.begin(); i != vec.end(); i++)
cout << *i << "\t";

cout << endl;
}

int main()
{
vecflt	vec1(2),
vec2(2);

vec1[0] = 14.1102f; vec1[1] = 14.1145f;
vec2[0] = 15.8508f; vec2[1] = 26.0842f;

cout << "Initial vectors by iterator" << endl;
show_vec(vec1);
show_vec(vec2);
cout << endl;

sort( vec1.begin(), vec1.end() );
sort( vec2.begin(), vec2.end() );

cout << "Sorted vectors by index" << endl;
cout << vec1[0] << "\t" << vec1[1] << endl;
cout << vec2[0] << "\t" << vec2[1] << endl;
cout << endl;

cout << "Sorted vectors by iterator" << endl;
show_vec(vec1);
show_vec(vec2);
cout << endl;

cout << "size of vector 1 = " << vec1.size() << endl;
cout << "size of vector 2 = " << vec2.size() << endl << endl;

reverse( vec1.begin(), vec1.end() );
reverse( vec2.begin(), vec2.end() );

cout << "Reverse vectors by index" << endl;
cout << vec1[0] << "\t" << vec1[1] << endl;
cout << vec2[0] << "\t" << vec2[1] << endl;
cout << endl;

cout << "Reverse vectors by iterator" << endl;
show_vec(vec1);
show_vec(vec2);
cout << endl;

return 0;
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.