我正在處理在結構中輸入書籍數據的程序,並根據用戶的請求刪除元素。但是,我很難找出刪除所需書籍的最佳方法。你認爲我是否朝着正確的方向前進?C++刪除動態數組的1個元素?
struct Data{ //struct of data
string title;
string author;
string publisher;
};
void remove (Data *ptr, string title, string author, string publisher, int num)
{
string book_rem;
cout << "What book do you want to remove?" << endl;
getline (cin, book_rem);
for (int i=0;i<num;i++)
{
if (ptr[i].title == book_rem) // check for equality
{
for (int j = 0; j < num; j++) //shift over elements in new array
ptr[j] = ptr[j+1];
ptr[j-1] = 0;
}
else
{
cout << "book not found!" << endl;
}
}
}
我知道我的邏輯是關閉的?..但我的導師說我是close..if您有什麼好的資源或鏈接,請給他們這樣這些指針有我在,以獲得最佳的方式略有困惑它完成了。
爲什麼不使用std :: vector?所有的工作已經爲你完成了。 – Lalaland 2012-03-01 05:31:30
嗯,我們還沒有對向量做過什麼:/我會查找他們thnx – gamergirl22 2012-03-01 05:32:04
for(int j = num; j
srbhkmr
2012-03-01 05:35:56