?
Solved

Using STL to sort a vector

Posted on 2003-03-17
9
Medium Priority
?
462 Views
Last Modified: 2013-12-14
I am trying to sort the STL vector Record with the given sort statement.  The vector is reordered thoough not as I expected, nor does my

    cout << szColName << endl;

statement seem to execute.  How can I sort this vector?



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

class Record
{
public:
  UCHAR szQualifier [STR_LEN];
  UCHAR szOwner [STR_LEN];
  UCHAR szTableName [STR_LEN];
  UCHAR szTableNameX [STR_LEN];
  UCHAR szColName [STR_LEN];
  UCHAR szTypeName [STR_LEN];
  UCHAR szRemarks [REM_LEN];
  SWORD DataType;
  SWORD Scale;
  SWORD Radix;
  SWORD Nullable;
  SDWORD Precision, Length;
  int Record::operator > (const Record& obj)
  {
    cout << szColName << endl;
    return strcmp((char*)szColName,(char*)obj.szColName);

  }
};


  sort(theVector.begin(),theVector.end(),greater<Record*>());
0
Comment
Question by:Kalvyn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 

Author Comment

by:Kalvyn
ID: 8155924
I forgot the definition...

typedef vector <Record*> Vector;
Vector recs;
Vector::iterator Iterator;
0
 

Author Comment

by:Kalvyn
ID: 8155927
opps...
typedef vector <Record*> Vector;
Vector theVector;
Vector::iterator Iterator;

sort(theVector.begin(),theVector.end(),greater<Record*>());
0
 
LVL 11

Expert Comment

by:bcladd
ID: 8156367
greater<Record *> should be the give away: You are comparing the pointers, not the records themselves. You need a comparison function that takes two Record pointers  and compares the Records.

-bcl
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:efn
ID: 8156369
Hi Kalvyn,

The third argument to sort is telling it to sort by pointer values.  So it is sorting the records by their addresses, not by their contents, and it's not calling your > operator, which is why you don't see any output.

Try greater<Record>() instead of greater<Record*>().

--efn
0
 
LVL 15

Expert Comment

by:efn
ID: 8156395
My answer would have made sense if theVector were a vector of Records, but since it's a vector of pointers to records, bcladd's answer is right and my suggestion to try greater<Record>() was bogus.  Sorry I didn't read the code more carefully.
0
 

Author Comment

by:Kalvyn
ID: 8156411
So how do I define the comparison function to sort by szColName and call it with sort?
0
 
LVL 11

Expert Comment

by:bcladd
ID: 8156450
typedef Record * RecordPtr
bool lessThan(const RecordPtr & x, const RecordPtr & y)
{
  return strcmp((char *)x->szColName, (char *)y->szColName) < 0);
}

Modify direction of > or < to suit your needs.

-bcl

0
 
LVL 11

Accepted Solution

by:
bcladd earned 500 total points
ID: 8156453
Oh, and

sort(theVector.begin(), theVecotor.end(), lessThan);

-bcl
0
 

Author Comment

by:Kalvyn
ID: 8156491
Thank bcl,

With a slight syntax change, it worked for me.

typedef Record * RecordPtr;
bool lessThan(const RecordPtr & x, const RecordPtr & y)
{
 return (strcmp((char *)x->szColName, (char *)y->szColName) < 0);
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question