C++ 将具有不同整数的字符数组组合为类似日期和时间

C++ 将具有不同整数的字符数组组合为类似日期和时间,c++,char,int,C++,Char,Int,我需要创建一个以日期和时间格式返回字符数组的方法 class DateTime { int *_day, *_month, *_year, *_hours, *_minutes; public: DateTime(int day = 1, int month = 1, int year = 2000, int hours = 0, int minutes = 0) { _day= new int(day); _month= new int(m

我需要创建一个以日期和时间格式返回字符数组的方法

class DateTime {

    int *_day, *_month, *_year, *_hours, *_minutes;

public:

    DateTime(int day = 1, int month = 1, int year = 2000, int hours = 0, int minutes = 0) {
        _day= new int(day);
        _month= new int(month);
        _year= new int(year);
        _hours = new int(hours);
        _minutes= new int(minutes);
    }
}

char * ToCharArray() {

}
在给定的类赋值中,我必须使用int指针并在main中赋值

我曾尝试过stringstream将整数转换为字符,然后尝试以一种有意义的方式将它们组合到数组中,但我没有成功

我尝试使用

temp[0] = '0' + *_date;
但是,由于一些日期是两位数的,所以要计算出格式中的点和空格就太难了

预期结果如下:

DateTime date1(19, 6, 2018, 10, 15); 
cout << date1.ToCharArray() << endl;

// outputs: 19.6.2018 10:15

我愿意支持你

第一个也是最重要的:在现代C++中,你不应该或尽可能地使用原始指针来进行内存管理。始终使用std::unique:ptr或std::shared\u ptr

使用原始指针进行动态内存管理造成内存泄漏的风险太高。出于您的目的,拥有一个带有受保护的日期/时间成员的封闭类DateTime,我根本无法想象拥有指针的任何合理用例。如果是为了学术目的,那么可以。否则,根本没有必要

如果要使用指针和动态内存分配,则必须在类的析构函数中删除新的内存。否则,您将保证内存泄漏。不要忘了用nullptr初始化指针

现在谈谈你的问题:


如果您只想将类DateTime输出到任何ostream,那么除了所询问的格式问题之外,您还应该忽略,所显示的代码不是实际代码,或者所显示的类不可能工作。请回答您的问题,并包括一个演示您所问问题的类声明,而不是一点也不可能工作的假类声明。为什么要使用int指针作为DateTime类的类成员?还有,你为什么要自己滚?C++已经拥有,而不是创建另一个时间戳。如果很难计算出格式中的点和空格,那么为什么不使用标准的std::put_time而不是自己滚动呢?stringstream是正确的。你为什么放弃?
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>

class DateTime1     // Version 1
{
public:
    DateTime1() {}      // Default. Does nothing. Use standard default values
    DateTime1(int d, int m, int y, int h, int min) : day(d), month(m), year(y), hours(h), minutes(min) {}
    // Option 1. Overwrite the ostream operator
    friend std::ostream& operator<<(std::ostream& os, const DateTime1& dt) { return os << dt.day << '.' << dt.month << '.' << dt.year << ' ' << std::setfill('0') << std::setw(2) << dt.hours << ':' << std::setw(2) << dt.minutes; }
    // Option 2. Create a std String
    std::string toString() {std::ostringstream oss; oss << day << '.' << month << '.' << year << ' ' << std::setfill('0') << std::setw(2) << hours << ':' << std::setw(2) << minutes; return oss.str();}
protected:
    int day{ 1 };   int month{ 1 }; int year{ 1970 };   int hours{ 0 }; int minutes{ 0 };
};


class DateTime2     // Version 2
{
public:
    explicit DateTime2(int d, int m, int y, int h, int min) : day(new int(d)), month(new int(m)), year(new int(y)), hours(new int(h)), minutes(new int(min)) {}
    // MUST delete dynamically allocated memory
    ~DateTime2() { delete day; delete month; delete year; delete hours; delete minutes; }

    // Option 1. Overwrite the ostream operator
    friend std::ostream& operator<<(std::ostream& os, const DateTime2& dt) { return os << *dt.day << '.' << *dt.month << '.' << *dt.year << ' ' << std::setfill('0') << std::setw(2) << *dt.hours << ':' << std::setw(2) << *dt.minutes; }
    // Option 2. Create a std String
    std::string toString() { std::ostringstream oss; oss << *day << '.' << *month << '.' << *year << ' ' << std::setfill('0') << std::setw(2) << *hours << ':' << std::setw(2) << *minutes; return oss.str(); }
protected:
    int* day{ nullptr }; int* month{ nullptr }; int* year{ nullptr };   int* hours{ nullptr };  int* minutes{ nullptr };
};

using DateTime = DateTime2;   // Seletc DateTime1 or DateTime2

int main()
{
    std::cout << DateTime(19, 6, 2018, 10, 15) << '\n';

    // or
    DateTime date1(19, 6, 2018, 10, 15);    // Call the contructor
    std::cout << date1 << '\n';

    // or
    std::string dateTimeString{ date1.toString() };     // Initialize variable dateTimeString
    const char* dateTimeCharP{ dateTimeString.c_str() };

    std::cout << dateTimeString << '\n';
    std::cout << dateTimeCharP << '\n';

    return 0;
}