Need help overloading < in ADTs

Basically I need to compare data contained in an ADT of the format of 4 numbers followed by 3 letters, for example : 3000-wtz (It has the dash).

If left to its own devices (without an overload) the stl_algorithm explodes.

Here is the header file for the ADT::


#ifndef PROJECT_H
#define PROJECT_H

#include <iostream>
#include <string>
#include "staff.h"

class Division;


class Project {
public:

typedef const Staff* iterator;

Project(int maxStaff = DEFAULT_MAXSTAFF);


// Attributes of Project

// Project title
std::string getTitle() const {return theTitle;}
void setTitle(std::string title) {theTitle = title;}

// To which division does this project belong? Each project belongs
// to one division, but a division may have many projects.
Division* getDivision() const {return theDivision;}
void setDivision(Division* publ) {theDivision = publ;}

// The project ID is a unique identifier for the project.
std::string getProjectID() const {return theProjectID;}
void setProjectID(std::string projectID) {theProjectID = projectID;}

// Budget codes indicate to whome this project is charged.
std::string getBudgetCode() const {return theBudgetCode;}
void setBudgetCode(std::string projectID) {theBudgetCode = projectID;}


// Operations on Project

// Add a staff member to the project. Staff should be kept in alphabetic
// order by name.
void addStaff(const Staff&);
int numberOfStaff() const;

// Access to list of staff assigned to this project
iterator begin() const;
iterator end() const;

bool operator< (const Project& p) const;


private:
std::string theTitle;
std::string theProjectID;
std::string theBudgetCode;
Division* theDivision;

int numStaff;
int theMaxStaff;
Staff* staff; // array of Staff

static const int DEFAULT_MAXSTAFF = 12;
};


#endif




So.....


When overloading the < operator, I want to compare theProjectID using the sort algorithm. The function call looks like this:: sort (projects, projects+div.numberOfProjects());

So when I overload it looks something like this::

bool Project::operator< (const Project& p) const
{
if (theProjectID < p.getProjectID)
return true;
if (theProjectID > p.getProjectID)
return false;
return (theProjectID(suffix) < p.getProjectID(suffix);
}

I need it to first compare the numerical portion of the data, the 3000 example given, and if they have the same numerical value, it needs to compare the alphabetical portion in order, which I have crudely pseudo-coded above as (suffix).

Any idea on how to separate the numerical data from the char data of an unknown data type stored in an ADT for comparisons accomplished by an overloaded comparison operator?

Thanks :D
bump
See if this helps. You might want to do more error checking.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class Project {
  public: 
    Project(std::string s):theProjectID(s){};
    std::string getProjectID() const {return theProjectID;}
    bool operator<(const Project&p) const{
        bool ret(false);
        size_t posA(theProjectID.find('-'));
        size_t posB(p.getProjectID().find('-'));
        int a(atoi(theProjectID.substr(0,posA).c_str()));
        int b(atoi(p.getProjectID().substr(0,posB).c_str()));
        if ( a < b )
           ret = true;
        else if ( a > b )
           ret = false;
        else
           ret = (theProjectID.substr(posA+1) < p.getProjectID().substr(posB+1));
        return ret;
  };
  private:
    std::string theProjectID;
};

int main(){
  Project proA("3000-wtf");
  Project proB("3000-wtx");
  cout << "proA(" << proA.getProjectID() 
       << ") < proB(" << proB.getProjectID() << ") = " 
       << (proA < proB) << endl;
  Project proC("3000-wzf");
  cout << "proC(" << proC.getProjectID() 
       << ") < proB(" << proB.getProjectID() << ") = " 
       << (proC < proB) << endl;
  return 0;
}

$ ./a.out
proA(3000-wtf) < proB(3000-wtx) = 1
proC(3000-wzf) < proB(3000-wtx) = 0
Topic archived. No new replies allowed.