我有這樣的結構在這裏與向量從矢量中移除結構,解構它?
//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'已經正確實現了,因爲它沒有使用任何指針。 –