The data.txt file contains 5 dates:

15.11.2016 30.12.1998 23.01.2013 12.12.2012 11.11.2011 

How to determine which one is early and which is late? The program reads the dates from the file and displays them on the screen.

 #include <iostream> #include <fstream> #include <string> using namespace std; int main() { const int n = 5; string* dat = new string[n]; ifstream fin("data.txt", ios_base::in); for (int i = 0; i < n; i++) { getline(fin, dat[i]); } for (int i = 0; i < n; i++) { cout << dat[i] << endl; } fin.close(); system("pause"); return 0; } 

That is, you need to somehow memorize the strings of the number in the variables of the day, month, year separately and compare further ... Or is there any library in C ++ that can be used to make this comparison?

    3 answers 3

    If you just need to compare dates, translate from to YYYYMMDD , and compare as < or there >= - in short, ordinary integers.

    Of course, you can fill in the tm structure and get the time_t value using the mktime function, but this is not necessary for comparison.

     char * s = "11.05.2016"; int d,m,y; if (sscanf(s,"%d.%d.%d",&d,&m,&y) == 3) { int date = y*10000+m*100+d; cout << date << endl; } 

    You can read from the stream directly into variables, in a word, any conversion of a formatted string to integers ...

    • string translate into int of the form YYYYMMDD? For example, 11.11.2011 will be 20111111 But how to translate? - NiggaR
    • For example, a simple sscanf - see the answer. - Harry
    • Yes, just flip the strings and compare them to strcmp() - PinkTux
    • Turn over? And what about the points then do? I have a date in this format 11/11/2011 - NiggaR
    • Points do not affect. - PinkTux

    Do not reinvent the wheel , use std::get_time() (from the standard chrono library) and compare date objects with < .

    You are not writing C to pervert with parsing lines or time_t . Use the power of C ++ 11 =)

    • You will get the same struct tm that I talked about and how to compare them? Again, transforming into something else ... - Harry
    • That approached me a way to translate into a view YYYYMMDD) - NiggaR

    First of all, it’s completely unclear why you allocate memory dynamically when the size of your array is constant.

    If the size of the array can change depending on the number of lines in the file, it is better to use the standard class std::vector .

    You can declare a simple structure that will store the selected year, month, and day values ​​for a given date.

    You can also immediately sort the vector by increasing dates.

    The program is shown below as well. Only instead of the input file, I used a stream of lines for simplicity in the program.

     #include <iostream> #include <vector> #include <string> #include <sstream> #include <algorithm> #include <functional> struct Date { unsigned int year; unsigned int month; unsigned int day; }; int main() { std::string s = "15.11.2016\n" "30.12.1998\n"; std::vector<Date> dates; std::istringstream input( s ); std::string line; while ( std::getline( input, line ) ) { Date d = {}; char c; std::istringstream is( line ); is >> d.day >> c >> d.month >> c >> d.year; dates.push_back( d ); } std::sort( dates.begin(), dates.end(), []( const Date &d1, const Date &d2 ) { return std::tie( d1.year, d1.month, d1.day ) < std::tie( d2.year, d2.month, d2.day ); } ); for ( Date d : dates ) { std::cout << d.day << '.' << d.month << '.' << d.year << std::endl; } return 0; } 

    Output of the program to the console

     30.12.1998 15.11.2016 

    To compare dates stored in structure objects, you can define operator < as follows

     bool operator <( const Date &d1, const Date d2 ) { return std::tie( d1.year, d1.month, d1.day ) < std::tie( d2.year, d2.month, d2.day ); }