Solved

references of <vector> elements

Posted on 2004-09-20
13
244 Views
Last Modified: 2010-04-01
this seems to be unsafe, right?:

vector<int> intvec;
intvec.push_back(2004);
int  &ref = intvec.back();
intvec.push_back(2005);
....
intvec.push_back(2100);

is now ref == 2004? seems not.
its the same with pointers. how to keep track of certain elements within the vector if
vector<of_pointers> is unacceptable:

vector<int*> intvec;
int* ip = new int(2004);
intvec.push_back(ip);
....
//push back some more pointers;
...
still *ip == 2004;


thanks

0
Comment
Question by:podoboq
13 Comments
 
LVL 17

Expert Comment

by:rstaveley
ID: 12102118
If you reserve enough space in the first place, your elements won't be moved and references won't be invalidated.
0
 
LVL 13

Expert Comment

by:SteH
ID: 12102122
I am not really sure why the vector<int*> is not acceptable?

I would have a pointer for adding:

int* pIntAdd = new int (2001);
pintvec.push_back (pIntAdd);
pIntAdd = new int (2003);
pintvec.push_back (pIntAdd);
pIntAdd = new int (2006);
pintvec.push_back (pIntAdd);

And if you need to keep track of a certain one add another pointer:
pIntAdd = new int (2006);
int* pTrackThis = pIntAdd; // copy address
pintvec.push_back (pIntAdd); // push it onto the stack

*pTrackThis will remain 2006 until you change it.

0
 

Author Comment

by:podoboq
ID: 12102162
wow, thats fast:)
mm, so, there is no other option if no pointers/ reserving space?
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:podoboq
ID: 12102251
i dont want any pointers, for i need to free mem after that
i feel its ugly having those nice vectors and something like
for(vector<int>::iterator i = ...)  delete *i;
0
 
LVL 13

Expert Comment

by:SteH
ID: 12102293
What is your goal with this vector. Perhaps there is a solution (Another type of container perhaps?). If you give more details there might be better help available.
0
 

Author Comment

by:podoboq
ID: 12102389
yes, probably i have to rethink it over
basicly it is like that:

class node;

class spring{
public: apply_tenssion_to_nodes();
private: node* node1, *node2;  
};

so ive got some nodes and springs and would like to have them neatly stuffed in two vectors. as there can be a node with a number of springs attached to it, i need the references/pointers in the spring class, not the node objects themselves.
0
 
LVL 13

Expert Comment

by:SteH
ID: 12102429
And how do you create instances of these classes? If the number is variable you will do something like new node or new spring. Then you need to iterate over all nodes and springs and delete them anyhow. So what is wrong with a vector iterator?
0
 

Author Comment

by:podoboq
ID: 12102482
i create them on the stack:

vector<node> nodes;
vector<spring> springs;
...
{
     node new_node(...);
     nodes.push_back(node);
}
...
{
    //get somehow n1, n2 from nodes
    ....
     spring new_spring(&n1,& n2,...);
     springs.push_back(new_spring);
}
0
 

Author Comment

by:podoboq
ID: 12102510
i just  want to skip delete and so on. i would have written it that way 2 hours ago, but  want more "beautiful" solution. how about  auto_ptr?
0
 

Author Comment

by:podoboq
ID: 12102646
or is there some kind of ptr_vector class?
i should google it before posting , though:)
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 100 total points
ID: 12104373
if you have this

class node
{
private:
    int m_ref;  
public:
    node()  : m_ref(1) { }
    ...
    friend class nodePtr;
};

class nodePtr
{
public:
     nodePtr(node* n) : m_n(n) { n.m_ref++; }
     nodePtr(const nodePtr& np) : m_n(np.m_n) {  n.m_ref++; }
     ~nodePtr() { if (--m_n.m_ref == 0) delete m_n; }
     nodePtr& operator=(nodePtr& np)
     {  if (np == this || np.m_n == m_n) return; // handle x = x;
         if (--m_n.m_ref == 0) delete m_n;
         m_n = np.m_n;
         m_n.m_ref++;
     }
private:
     node* m_n;
}

Now you should be able to have

    vector<nodePtr> nodes;

where deletion is done automatically and you have only one node object.

Do the same with class  springPtr and use nodePtr instead of node as members.

Regards, Alex




0
 

Author Comment

by:podoboq
ID: 12467783
sorry for being so late!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 149
Which IDE to use to begin C++ training? 5 63
Cross compile release version of c++ program for linux 2 132
Beginner to Unreal Engine 4 5 79
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question