Sorting STL Cards

UPDATED: I followed John’s guidance and changed his code, which solved my problem by creating a comparator function and inserting it into the Compare parameter on the STL card. Since my string date is strictly in the format shown, using substr will be fine. My conclusion and codes are below for your reference.

Date            Total Sales
01JAN1900       $4
20JAN1902       $40
18NOV1912       $2500
19NOV1912       $2500
19OCT1923       $25
01JAN1991       $22
15NOV1991       $300
Grand Total:    $5391


 struct CompareDates 
:
  public std::binary_function <bool, std::string, std::string>
{
  bool operator() (const std::string& lhs, const std::string& rhs)
  {


     if(lhs.substr(5,4) < rhs.substr(5,4))
     {
         return true;

     }
     else if (lhs.substr(5,4) == rhs.substr(5,4) && lhs.substr(2,3) < rhs.substr(2,3))
     {
         return true;
     }
     else if (lhs.substr(5,4) == rhs.substr(5,4) && lhs.substr(2,3) == rhs.substr(2,3) && lhs.substr(0,2) < rhs.substr(0,2))
     {
         return true;

     }
     else
     {
         return false;
     }


  }
};

map<string, double,CompareDates> dailyDatePrices;

: . STL map . , , STL . , , , . ( ) , . . ? !

Raw Data
STRAW:10:15NOV1991
TOY:10:15NOV1991
BARLEY:5:01OCT1992

Undesired Current Report Output
01OCT1992 5
15NOV1991 20

Expected Report Output
15NOV1991 20
01OCT1992 5
+4
4

A std::map . map, .

, map. , .

, - , string. map , string - , , ? . , .

:

YYYYMMDD

Y, M D . NOV - 11.

unsigned long string s. , .


, . , :

[ ++ 03]

struct CompareDates 
:
  public std::binary_function <bool, std::string, std::string>
{
  bool operator() (const std::string& lhs, const std::string& rhs)
  {
    // return true if lhs < rhs
    // return false otherwise

    // step 1:  compare years.  if lhs.year < rhs.year, return true.  else, continue
    // step 2: compare months.  if lhs.month < rhs.month, return true.  else, continue.
    //    note:  don't just compare the strings, else "AUG" < "JAN" etc
    // step 3: compare days.  if lhs.day < rhs.day, return true.  else, return false.
  }
};

, , , .:)

, , :

std::map <Key, Value, CompareDates> myMap;
+9

, , - , , std::greater , std::less.

std::map<date, other_type, std::greater<date>> example;
// Otherwise use example

:

#include <iostream>
#include <functional>
#include <map>

int main() {
    std::map<int, float, std::greater<int>> example;
    example.emplace(std::make_pair(10, 10.0));
    example.emplace(std::make_pair(12, 12.0));

    for (auto const& entry : example)
    {
        std::cout << "Key: " << entry.first << " " << entry.second << std::endl;
    }

    return 0;
}

http://ideone.com/9Guuil

+1

, :

From: 01OCT1992
To: 1992-10-01

< , .

+1

- Date // ( , , , 1...12 ..).

operator<, Date.

std::map<Date, int> ( int ), " ", Date map-key.

, , Date , string ( , /, "-" ).

( VS2012):

#include <iostream> // for std::cout, std::endl
#include <map>      // for std::map
#include <sstream>  // for std::ostringstream
#include <string>   // for std::string
#include <vector>   // for std::vector
using namespace std;

// A simple date.
// In real world code, this should be a class with private
// date members, and proper accessors.
struct Date {
    int day;
    int month;
    int year;

    Date() : day(0), month(0), year(0) {}

    Date(int d, int m, int y)
        : day(d), month(m), year(y)
    {}
};

// Define proper sorting for dates
bool operator<(const Date& d1, const Date& d2) {
    // First compare years
    if (d1.year < d2.year)
        return true;
    if (d1.year > d2.year)
        return false;

    // Same year, compare months
    if (d1.month < d2.month)
        return true;
    if (d1.month > d2.month)
        return false;

    // Same year and month, compare days
    return (d1.day < d2.day);
}

// Format dates in a specific format
string FormatDate(const Date& date) {
    // NOTE: bounds checking for day and month omitted.

    ostringstream os;

    if (date.day < 10)
        os << '0';
    os << date.day;

    static const char* monthNames[] = {
        "JAN", "FEB", "MAR", "APR",
        "MAY", "JUN", "JUL", "AUG",
        "SEP", "OCT", "NOV", "DEC"
    }; 
    os << monthNames[date.month - 1];

    os << date.year;

    return os.str();
}

struct Item {
    string type;
    int price;
    Date date;

    Item(const string& t, int p, const Date& d)
        : type(t), price(p), date(d)
    {}
};


int main() {
    vector<Item> items;
    items.push_back(Item("STRAW", 10, Date(15, 11, 1991)));
    items.push_back(Item("TOY",   10, Date(15, 11, 1991)));
    items.push_back(Item("BARLEY", 5, Date( 1, 10, 1992)));

    map<Date, int> priceData;
    for (const auto& item : items) {
        auto where = priceData.find(item.date);
        if (where != priceData.end()) {
            where->second += item.price;
        } else {
            priceData[item.date] = item.price;
        }
    }

    for (const auto& e : priceData) {
        cout << FormatDate(e.first) << " " << e.second << endl;
    } 
}

:

15NOV1991 20
   01OCT1992 5

0
source

All Articles