C++ vector的delete与clear的区别 - 析构函数

C++ vector的delete与clear的区别 - 析构函数

分享
标签: C++  vector  delete  clear  析构函数  分类: CPlusPlus 2011-04-14 22:52

1.clear()   不调用析构函数 

2.delete   *it   调用析构函数 
可用以下程序测试出来:

#include <iostream>

#include <string>

#include <fstream>

#include <vector>

using namespace std;

 

ofstream txtout( "test.txt");

 

class A

{

public:

  A()

  txtout << "A constructor! " <<endl;

  }

  virtual ~A();

};

 

A::~A()

txtout << "A destructor! " <<endl;

}

 

const int test_size = 10;

int main()

{

  vector < A* > the_vector,the_vector_copy;

      A* pa;

 

      for ( int i=0; i <test_size; i++ )

      pa = new A();

         the_vector.push_back(pa);

         the_vector_copy.push_back(pa);

      }

 

      txtout <<endl;

      txtout << "beforedelete... " <<endl;

      pa = the_vector[0];

      delete pa;

      txtout << "afterdelete... " <<endl;

      txtout <<endl;

 

      txtout <<endl;

      txtout << "beforeclear... " <<endl;

      the_vector.clear();

      txtout << "after clear..." <<endl;

      txtout <<endl;

 

      txtout <<endl;

      txtout << "before alldeleting... " <<endl;

      for int i=1; i <test_size; i++ )

         delete the_vector_copy[i];

  }

  txtout << "after all deleting... " <<endl;

  txtout <<endl;

}


  code::blocks 运行结果:

A constructor! 
A constructor! 
A constructor! 
A constructor! 
A constructor! 
A constructor! 
A constructor! 
A constructor! 
A constructor! 
A constructor! 

before delete... 
A destructor! 
after delete... 


before clear... 
after clear... 


before all deleting... 
A destructor! 
A destructor! 
A destructor! 
A destructor! 
A destructor! 
A destructor! 
A destructor! 
A destructor! 
A destructor! 
after all deleting...
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是用 C++ 实现通讯录管理系统的示例代码,其中包含添加、存储、删除、查找、修改、清空、备份和还原等功能,并且使用析构函数来释放动态分配的内存。 ```c++ #include <iostream> #include <string> #include <vector> #include <fstream> using namespace std; struct Contact { string name; string phone; }; class ContactBook { public: ContactBook() { loadFromFile(); // 从文件中加载联系人 } ~ContactBook() { saveToFile(); // 保存联系人到文件 for (auto contact : contacts) { delete contact; } } void addContact() { Contact* contact = new Contact(); cout << "Enter name: "; cin >> contact->name; cout << "Enter phone: "; cin >> contact->phone; contacts.push_back(contact); cout << "Contact added: " << contact->name << endl; } void listContacts() const { cout << "Contacts:" << endl; for (const auto& contact : contacts) { cout << "Name: " << contact->name << ", Phone: " << contact->phone << endl; } } void deleteContact() { string name; cout << "Enter name to delete: "; cin >> name; for (auto it = contacts.begin(); it != contacts.end(); ++it) { if ((*it)->name == name) { cout << "Contact deleted: " << (*it)->name << endl; delete *it; contacts.erase(it); return; } } cout << "Contact not found: " << name << endl; } void findContact() const { string name; cout << "Enter name to find: "; cin >> name; for (const auto& contact : contacts) { if (contact->name == name) { cout << "Name: " << contact->name << ", Phone: " << contact->phone << endl; return; } } cout << "Contact not found: " << name << endl; } void modifyContact() { string name; cout << "Enter name to modify: "; cin >> name; for (auto it = contacts.begin(); it != contacts.end(); ++it) { if ((*it)->name == name) { cout << "Enter new phone: "; cin >> (*it)->phone; cout << "Contact modified: " << (*it)->name << endl; return; } } cout << "Contact not found: " << name << endl; } void clearContacts() { for (auto contact : contacts) { delete contact; } contacts.clear(); cout << "All contacts cleared." << endl; } void backupContacts() const { ofstream file("backup.txt"); if (!file) { cout << "Error: unable to open backup file." << endl; return; } for (const auto& contact : contacts) { file << contact->name << ' ' << contact->phone << endl; } cout << "Contacts backed up to file." << endl; } void restoreContacts() { ifstream file("backup.txt"); if (!file) { cout << "Error: unable to open backup file." << endl; return; } clearContacts(); string name, phone; while (file >> name >> phone) { Contact* contact = new Contact(); contact->name = name; contact->phone = phone; contacts.push_back(contact); } cout << "Contacts restored from file." << endl; } private: vector<Contact*> contacts; void loadFromFile() { ifstream file("contacts.txt"); if (!file) { return; } string name, phone; while (file >> name >> phone) { Contact* contact = new Contact(); contact->name = name; contact->phone = phone; contacts.push_back(contact); } } void saveToFile() const { ofstream file("contacts.txt"); if (!file) { cout << "Error: unable to save contacts to file." << endl; return; } for (const auto& contact : contacts) { file << contact->name << ' ' << contact->phone << endl; } } }; int main() { ContactBook book; while (true) { cout << "Enter command (add, list, delete, find, modify, clear, backup, restore, exit): "; string command; cin >> command; if (command == "add") { book.addContact(); } else if (command == "list") { book.listContacts(); } else if (command == "delete") { book.deleteContact(); } else if (command == "find") { book.findContact(); } else if (command == "modify") { book.modifyContact(); } else if (command == "clear") { book.clearContacts(); } else if (command == "backup") { book.backupContacts(); } else if (command == "restore") { book.restoreContacts(); } else if (command == "exit") { break; } else { cout << "Unknown command: " << command << endl; } } return 0; } ``` 这个示例中,我们使用了动态内存分配来管理联系人,使用析构函数释放动态分配的内存。我们还实现了添加、存储、删除、查找、修改、清空、备份和还原等功能。我们还从文件中加载联系人,并在程序退出时将联系人保存到文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值