?
Solved

references of <vector> elements

Posted on 2004-09-20
13
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

765 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