470,815 Members | 2,439 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

C++ vector of pointers

so what's the deal with a vector of pointers. i know that the pointers after some point can all be invalidated if an insertion or deletion is made or done, and all can be invalidated if the vector is resized. but can a deletion and then an insertion, or vice-versa cause just one pointer to be invalidated. Consider the following:

Expand|Select|Wrap|Line Numbers
  1.  vector<object*> a_s; 
  2. vector<object*> b_s;
  3.  
  4. int b_count = 0;
  5. double random = 0;
  6.  
  7. object* point_1 = new object();//constructed with next and previous pointers
  8. a_s.insert(a_s.begin(), point_1);
  9. point_1 = NULL;
  10.  
  11. for(int i=0; i<some_big_huge_number;i++)
  12. {
  13. object* point_2 = new object();//constructed with next and previous pointers
  14. b_s.insert(b_s.begin(),point_2);
  15. point_2 = NULL;
  16. }
  17.  
  18. vector<object*>::iterator a_iter = a_s.begin();
  19. vector<object*>::iterator b_iter = b_s.begin();
  20.  
  21. while(b_s.size()!=0)
  22. {
  23. random = (rand()%100);
  24. b_count = random/100*b_s.size();
  25.  
  26. b_iter = b_s.begin();
  27.  
  28. for(int j=0;j!=b_count;j++)
  29. ++b_iter;
  30.  
  31. a_s[0]->next = &*(b_s[b_count]);
  32. b_s[b_count]->previous = &*(a_s[0]);
  33.  
  34. a_iter = a_s.erase(a_iter);
  35. a_iter = a_s.insert(a_iter, b_s[b_count]);
  36. b_iter = b_s.erase(b_iter);
  37. }
  38.  
  39.  
At the end there is a doubly linked list accessable by the only remaining pointer in the a_s vector which points to the last node of the list. There's some random pointing going on.... any ideas where it's coming from?? is this use of a vector defined?? stable?? bad idea??
Apr 9 '06 #1
0 40619

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by luigi | last post: by
14 posts views Thread by Roland Bengtsson | last post: by
13 posts views Thread by Joseph | last post: by
11 posts views Thread by koperenkogel | last post: by
9 posts views Thread by kathy | last post: by
8 posts views Thread by jagguy | last post: by
5 posts views Thread by Gert Van den Eynde | last post: by
6 posts views Thread by lokchan | last post: by
6 posts views Thread by Jia | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.