Вам разрешено использовать весь C ++ или вам действительно разрешено использовать C только с std :: string и std :: cout?
Если вы можете использовать C ++, то вот как я бы это сделал (все еще используя связанный список, поскольку вы указали, что в вашем вопросе std :: vector и std :: sort будут более производительными, если вам не нужно использовать связанный список):
#include <iostream>
#include <sstream>
#include <iterator>
#include <locale>
#include <vector>
#include <list>
// define a facet that adds slash to the list of delimiters
class slash_is_space : public std::ctype<char> {
public:
mask const *get_table() {
static std::vector<std::ctype<char>::mask>
table(classic_table(), classic_table()+table_size);
table['/'] = (mask)space;
return &table[0];
}
slash_is_space(size_t refs=0) : std::ctype<char>(get_table(), false, refs) { }
};
// define a class (with public members) that adds splits the date using the new facet
struct extract_date
{
int day, month, year;
extract_date(std::string date) {
std::stringstream ss(date);
ss.imbue(std::locale(std::locale(), new slash_is_space));
ss >> day >> month >> year;
}
};
// your struct containing the date that will be used for sorting
struct carInsurance
{
std::string carNo;
std::string expireDate;
std::string carUsage;
std::string manufacturingDate;
};
// a function that can print your struct
std::ostream& operator << ( std::ostream& out, const carInsurance& rhs )
{
out << rhs.carNo << ", "
<< rhs.expireDate << ", "
<< rhs.carUsage << ", "
<< rhs.manufacturingDate;
return out;
}
int main()
{
// your linked list of data
std::list<carInsurance> cars{{"a","00/01/0000","a","a"},
{"b","00/03/0000","b","b"},
{"c","00/02/0000","c","c"}};
// sort the list by expireDate month
cars.sort([](carInsurance& a, carInsurance& b){
extract_date a_date(a.expireDate);
extract_date b_date(b.expireDate);
return a_date.month < b_date.month;
});
// print the sorted list
std::copy(cars.begin(), cars.end(), std::ostream_iterator<carInsurance>(std::cout, "\n"));
return 0;
}