473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 40854

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

Similar topics

9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
14
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers...
13
by: Joseph | last post by:
I was doing my assignment,but encountered a problem at last step!!!!!! for easy reading, i ommited lots of other things //=====================code begin================================...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
9
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new...
8
by: jagguy | last post by:
I am a little confused with the basic concept of vector of pointers. The vector is easy enough. Say you want a vector of pointers to int. The integers are not created outside the vector so all...
5
by: Gert Van den Eynde | last post by:
Hi all, It's probably trivial but I can't figure it out... I have a templated class template <typename T, typename uclass A I wish to fill a vector with pointers to objects of class A. I...
6
by: lokchan | last post by:
i want to create a vector of pointer s.t. it can handle new and delete but also have std::vector interface can i implement by partial specialization and inherence like follow ? #include...
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.