我有这样的结构在这里与向量从矢量中移除结构,解构它?
//Record Structure
struct Record {
std::string date;
std::string name;
int dollars = 0;
int cents = 0;
std::string getCost() {
//Dollars First
std::stringstream dollar;
dollar << dollars;
std::string DOLLAR = dollar.str();
//Cents Second
std::stringstream cent;
cent << cents;
std::string CENT = cent.str();
//Display cents as double zeros
DOLLAR = DOLLAR.length() == 1 ? "0" + DOLLAR : DOLLAR;
CENT = CENT.length() == 1 ? "0" + CENT : CENT;
//Result
std::string result = "$" + DOLLAR + "." + CENT;
//Return
return result;
}
void Constructor(std::string NM, std::string DT, int DOLLAS, int PENNY) {
name = NM;
date = DT;
dollars = DOLLAS;
cents = PENNY;
}
};
//Vector that holds instances of the Record structure
std::vector <Record> Records;
说我已经附加一个实例的矢量
Record newInstance;
newInstance.Constructor("Name", "Date", 10, 0);
Records.push_back(newInstance);
如果我删除它像这样,将整个结构被解构并释放空间?
Records.erase(Records.begin());
如果它没有得到解构和释放空间,我该怎么做?
如何正确实现元素类型,请您解释一下吗? – Skelelele
@Skelelele可能通过实现构造函数和析构函数。阅读关于RAII习语的更多信息,您将更多地了解对象以及如何在C++中使用它们。 –
@Skelelele:你只要确保销毁你的元素不会泄漏任何东西。你的'Record'已经正确实现了,因为它没有使用任何指针。 –