Solved

references of <vector> elements

Posted on 2004-09-20
13
242 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

747 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

13 Experts available now in Live!

Get 1:1 Help Now