我想知道从现在开始的确切日期。我有一个如下所示的输入数据框。年龄列是日期的差异。
我想获得显示的输出数据帧。它是按日期 = 当前日期 - 差值(AGE 数据)计算得出的日期。
有什么办法吗?
输入:
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
// Abstratcion of one line with values
struct LineData {
// Data
double d{};
int i{};
// Extraction operator. Read data with standard approach
friend std::istream& operator >> (std::istream& is,LineData& d) {
char c{};
return is >> d.d >> c >> d.i;
}
// Simple insertion operator
friend std::ostream& operator << (std::ostream& os,const LineData& d) {
return os << d.d << '\t' << d.i << '\n';
}
};
// Abstraction of one record. So,many lines with data,with a last line of stars as separator
struct Record {
// Our data. All lines with the double and integer values
std::vector<LineData> lineData{};
// Extractor: Read all values and the last line with starts. Ignore blank lines.
friend std::istream& operator >> (std::istream& is,Record& r) {
// Read data,until we hit the end of e record,denoted by stars
for (bool readingRecord{ true }; readingRecord and is;) {
// Read a complete line from the stream
if (std::string line{}; std::getline(is,line))
// Ignore empty kines
if (not line.empty())
// In case of stars,stop reding line data for this record
if (line[0] == '*') readingRecord = false;
else {
// Put the already read line into a std::istringstream for further extraction
std::istringstream iss{ line };
LineData ld{};
// Call extractor from LineData and add this line data to the record,inc ase of success
if (iss >> ld) r.lineData.emplace_back(std::move(ld));
}
}
return is;
}
// Simple inserter
friend std::ostream& operator << (std::ostream & os,const Record & r) {
std::copy(r.lineData.begin(),r.lineData.end(),std::ostream_iterator<LineData>(os));
return os << '\n';
}
};
// Abstratcion of Data,of the complete file
struct Data {
// Afile consists of records
std::vector<Record> data{};
// Simple extractor operator
friend std::istream& operator >> (std::istream& is,Data& d) {
// Read records until end of file
for (Record r{}; is >> r; d.data.emplace_back(std::move(r)))
;
return is;
}
// Simple inserter
friend std::ostream& operator << (std::ostream& os,const Data& d) {
std::copy(d.data.begin(),d.data.end(),std::ostream_iterator<Record>(os));
return os << '\n';
}
};
int main() {
// Her all data from the file will be stored
Data data{};
// Open file and and check,if it is open
if (std::ifstream ifs{ "test.txt" }; ifs) {
// Read complete file and parse it
ifs >> data;
// Show result
std::cout << data;
}
return 0;
}
输出:
AGE
0 '1 years 2 days ago'
1 '3 hours 4 mins ago'
2 '5 mins 6 secs ago'
...