#include <iostream> #include <vector> using namespace std; class CDemo { public: CDemo:str(NULL){} ~CDemo { if(str) delete [] str; } char * str; }; int main() { CDemo d1; d2.str = new char[32]; strcpy(d1.str,"my name"); vector<CDemo> * a1 = new vector<CDemo>(); a1->push_back(d1); delete a1; //line 1 vector<int> v1(100); v1.reserve(200); v1[190] = 190; //line2 cout<<v1.size()<<endl; //line3 return 0; }
v1.resize(200);
#include <iostream> #include <vector> using namespace std; class CDemo { public: CDemo():str(NULL){} ~CDemo() { if(str) delete [] str; } char * str; }; int main() { CDemo d1; d1.str = new char[32]; strcpy(d1.str,"my name"); vector<CDemo> * a1 = new vector<CDemo>(); a1->push_back(d1); vector<int> v1(100); v1.resize(200); v1[190] = 190;//line2 cout<<v1.size()<<endl; //line3 system("pause"); return 0; }
#include <iostream> #include <vector> using namespace std; class CDemo { public: CDemo():str(NULL){} ~CDemo() { if(str) delete [] str; } char * str; }; int main() { CDemo d1; d1.str = new char[32]; strcpy(d1.str,"my name"); vector<CDemo> * a1 = new vector<CDemo>(); a1->push_back(d1); vector<int> v1(100); v1.resize(200); v1.reserve(300);//这个是最小容量 v1[190] = 190;//line2 cout<<v1.size()<<v1.capacity()<<endl; //line3,size表示已经用的,capacity表示可以接受的 system("pause"); return 0; }
#include <cstdlib> [code=C/C++]#include <iostream> #include <vector> using namespace std; class CDemo { public: CDemo():str(NULL){} ~CDemo(){if(str) delete [] str;} char * str; }; int main(int argc, char *argv[]) { CDemo d1; d1.str = new char[32]; strcpy(d1.str,"my name"); vector<CDemo> * a1 = new vector<CDemo>(); a1->push_back(d1); delete a1; system("PAUSE"); return EXIT_SUCCESS; }