Advertisement
Guest User

vector test

a guest
May 12th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1.  
  2. int count;
  3. struct foo{
  4.     foo(){id = 0;}
  5.     ~foo(){ //Catch the message here...
  6.         count++;
  7.         std::cout << "The variable id (" << id << ") is destroyed. (" << count << ")\n";
  8.     }
  9.  
  10. int id;
  11. };
  12.  
  13.  
  14. int main()
  15. {
  16.     tVector<foo> vec;
  17.     foo obj;
  18.     int i;
  19.  
  20. for(i = 0;i < 10;i++){
  21.     vec.push_back(obj); //Avoid temporary constructors & destructors
  22.     vec.back().id = i;
  23. }
  24.  
  25.     vec.reserve(1000);  // Causes an automatic reallocation
  26.     std::cout << "foo size : " << vec.size() << " - capacity : " << vec.capacity() << std::endl;
  27.  
  28.     vec.shrink_to_fit();  // Same here
  29.     std::cout << "foo size : " << vec.size() << " - capacity : " << vec.capacity() << std::endl;
  30.  
  31.     //cout << "Try deleting some elements...\n";
  32.     //vec.erase(vec.begin() + 3, vec.begin() + 8);
  33.  
  34.     printf("my vector contains : \n");
  35.     for(i = 0;i < vec.size();i++)
  36.     cout << "vec[" << i << "] = " << vec[i].id << std::endl;
  37.  
  38. return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement