Hello folks! I have just joined this forum.
This is actually my first Thread on the internet and need help with using STL sort function. I searched the forum but no related answer was found.
Okay...here's my code;

#include<iostream> 
#include<vector> 
#include<algorithm> 
using namespace std;
struct MyStruct{
 int array[10];
};

vector<MyStruct> str_a(100);  //array of structures

int main(){
sort(str_a.begin(), str_a.end()); //call to STL sort
}

I am using a Dev C++ compiler 4.9.9.2 (latest)
It gave several errors related to intra_sort(or intro_sort), partial_sort...
Half of the errors are unknown (the compiler isnt friendly at all)
I tried some modifications..for example:

struct MyStruct{
 int array[10];
bool operator< (const MyStruct &);
};

and

std::sort(phone.begin(), phone.end());

I added the operator overloading because one of the errors i get is something like this:
no match found for
intra_sort(_first, _last......)
if(_a < _b){
......

i just dont seem to compile it.
All I wants to do is sort the array in the structure using sort().
If any specifications about the code or errors are needed please let me know.

Thank You.

All I wants to do is sort the array in the structure using sort().

If your only purpose is to sort a vector, then why are you using a structure? By doing so, you are creating a vector of structures, wherein each structure consists of a 10-element array. How do you propose to sort numbers stored in such an arrangement?

Instead, try using simply an integer vector and then sorting. I tried it and got no errors with the compiler you've specified.

Thank u for replying..
Yes I do understand that I can sort it using built in types and I can execute the code.
However I want to do it using structures you see.
Have you read C++ complete reference Stroustrup?
Theres a code in it and the author has used structure.
But he has used a string and an integer in his structure and has created array of structures. 1000 is the size to be specific.


thanks again.

std::sort only works with arrays such as a vector. If you want to use a structure then great, but you need an array of structures.

Theres a code in it and the author has used structure.
But he has used a string and an integer in his structure and has created array of structures. 1000 is the size to be specific.

True. But, again how do you propose to sort such an arrangement? You see, it no longer remains a simple vector. It becomes a vector of structures. There is where the difference lies. In such a case, there are two ways, you can either sort the numbers stored in each structure or sort the nth elements all the structure variables.

Ah! Ancient Dragon and I again replied at about the same time...

Yes. I now agree with you that it is not organized.
However I tried this:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct Entry{
  int array[10];
};

int main(){
vector<Entry> phone(1000);
sort(phone[1].begin(), phone[1].end());
}

Still doesnt compile. You think any issue with the syntax or something else
some errors:
class "Entry" has no member "begin"
sort(phone[1].begin(), phone[1].end());

error: class "Entry" has no member "end"
sort(phone[1].begin(), phone[1].end());

>>sort(phone[1].begin(), phone[1].end());
try this: sort(phone.begin(), phone.end()); And you will have to supply a comparison function because sort() doesn't know how to compare structures and classes.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Entry
{
  int array[10];
};


bool compare(Entry& e1, Entry& e2)
{
    // compare the two structures
    return e1.array[0] < e2.array[0];

}

int main()
{
    vector<Entry> phone(1000);
    sort(phone.begin(), phone.end(), compare );
}

Thank you so much guys. It worked ! However not without some modifications.
The references that you passed ancient_dragon, I passed them as constants.
And i dropped the array in the structure and used a string.
It just ran with perfect results. I realized the mistakes and now I know that my initial code just wasnt logically correct. Thanks Jishnu for that.
All the while I was thinking that I need to sort only the array in the structure when the sort algorithm sorts array of structures.
Thanks again guys..really appreciate it. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.