Solved

references of <vector> elements

Posted on 2004-09-20
13
243 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now