Rozwiązuje sobie to zadanie ze spoja:
http://pl.spoj.com/problems/FR_02_16/
I oto mój (na razie częściowy) sposób rozwiązania:
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
struct Uczen{
int rok;
int miesiac;
int dzien;
int numer;
Uczen(int a, int b, int c, int d) : rok(a), miesiac(b), dzien(c), numer(d) {}
};
bool funkcja(Uczen& a, Uczen& b);
int main() {
int testy;
cin >> testy;
while(testy--){
vector<Uczen> wekUczniowie;
int uczniowie;
cin >> uczniowie;
while(uczniowie--){
int numerek;
string data;
cin >> numerek >> data;
string rokS = data.substr(0, 4);
string miesiac = data.substr(5, 2);
string dzienS = data.substr(8, 2);
int r = atoi(rokS.c_str());
int m = atoi(miesiac.c_str());
int d = atoi(dzienS.c_str());
Uczen uczen( r, m, d, numerek );
wekUczniowie.push_back( uczen );
cout << uczen.dzien << endl;
cout << uczen.miesiac << endl;
cout << uczen.rok << endl;
cout << uczen.numer << endl;
}
sort(wekUczniowie.begin(), wekUczniowie.end(), funkcja);
}
return 0;
}
bool funkcja(Uczen& a, Uczen& b){
if(a.rok < b.rok)
return true;
else if(a.rok == b.rok && a.miesiac < b.miesiac)
return true;
else if(a.rok == b.rok && a.miesiac == b.miesiac && a.dzien < b.dzien)
return true;
else if(a.rok == b.rok && a.miesiac == b.miesiac && a.dzien == b.dzien && a.numer < b.numer)
return true;
else
return false;
}
Problem w tym że wywala mi dziwny błąd:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 5) > this->size() (which is 0)
Błąd znika po wy-komentowaniu algorytmu sort(). I dodam że dane z klasy Uczen wyświetlają się poprawnie.
Co zrobiłem nie tak?