[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

references of <vector> elements

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
podoboq
Asked:
podoboq
1 Solution
 
rstaveleyCommented:
If you reserve enough space in the first place, your elements won't be moved and references won't be invalidated.
0
 
SteHCommented:
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
 
podoboqAuthor Commented:
wow, thats fast:)
mm, so, there is no other option if no pointers/ reserving space?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
podoboqAuthor Commented:
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
 
SteHCommented:
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
 
podoboqAuthor Commented:
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
 
SteHCommented:
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
 
podoboqAuthor Commented:
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
 
podoboqAuthor Commented:
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
 
podoboqAuthor Commented:
or is there some kind of ptr_vector class?
i should google it before posting , though:)
0
 
itsmeandnobodyelseCommented:
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
 
podoboqAuthor Commented:
sorry for being so late!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now