pop from vector 
Author Message
 pop from vector

vector<Node*> nodes;

for (int i = 0;i <3; i++){
    Node* node1;
    node1= new Node();
    node1->setValue(i);
    nodes.push_back(node1);

Quote:
}

for (int j = 0; j <3;j++){
    Node* tempnode;
    tempnode = nodes.pop_back();

Quote:
}

I get error "error C2440: '=' : cannot convert from 'void' to 'Node *' "on
the line     tempnode = nodes.pop_back();, I tried to cast like this:
 tempnode = (Nodes*) nodes.pop_back(); bu that didnt work either. What would
be the correct code?

Another thig, if I want to iterate on nodes.size(), what should i do then? i
tried: for (int j = 0; j <nodes.size();j++), but that didnt work

Martin



Tue, 20 Dec 2005 06:59:48 GMT  
 pop from vector


Quote:
>     Node* tempnode;
>     tempnode = nodes.pop_back();

Should be:

Node* tempnode;
tempnode = nodes.back();
nodes.pop_back();

Ken



Tue, 20 Dec 2005 07:28:01 GMT  
 pop from vector
Hi there,
vector size changes dynamically , so size() will be dynamically updated. You
can take size of vector before any operation, and then navigate the vector.
but take care of dynamic updation of vector contents (eleemnts).

Regards
Rizi



Quote:
> vector<Node*> nodes;

> for (int i = 0;i <3; i++){
>     Node* node1;
>     node1= new Node();
>     node1->setValue(i);
>     nodes.push_back(node1);
> }
> for (int j = 0; j <3;j++){
>     Node* tempnode;
>     tempnode = nodes.pop_back();
> }

> I get error "error C2440: '=' : cannot convert from 'void' to 'Node *' "on
> the line     tempnode = nodes.pop_back();, I tried to cast like this:
>  tempnode = (Nodes*) nodes.pop_back(); bu that didnt work either. What
would
> be the correct code?

> Another thig, if I want to iterate on nodes.size(), what should i do then?
i
> tried: for (int j = 0; j <nodes.size();j++), but that didnt work

> Martin



Tue, 20 Dec 2005 13:09:06 GMT  
 pop from vector
you should use vector<class T>::back(); pop_back() will just pop it out, it
returns void.

about the iteration, it frequently looks like
for (vector<Node*>::iterator iter = nodes.begin(); iter != nodes.end(); iter
++)

but in this case you should be even more aware about changes in  the
container, since if you commit some, this will invalidate your current
iterators and references. I don't know what actually you re planning to do
in that iteration, but if it involves secuence reorganisation, mind that
facts.



Quote:
> vector<Node*> nodes;

> for (int i = 0;i <3; i++){
>     Node* node1;
>     node1= new Node();
>     node1->setValue(i);
>     nodes.push_back(node1);
> }
> for (int j = 0; j <3;j++){
>     Node* tempnode;
>     tempnode = nodes.pop_back();
> }

> I get error "error C2440: '=' : cannot convert from 'void' to 'Node *' "on
> the line     tempnode = nodes.pop_back();, I tried to cast like this:
>  tempnode = (Nodes*) nodes.pop_back(); bu that didnt work either. What
would
> be the correct code?

> Another thig, if I want to iterate on nodes.size(), what should i do then?
i
> tried: for (int j = 0; j <nodes.size();j++), but that didnt work

> Martin



Tue, 20 Dec 2005 18:23:21 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. VECTOR within a VECTOR

2. vector question when replacing array[10][20] with vectors

3. proper way of refering to pointer of vector to vector

4. Vector of vectors - compiler doesn't understand?

5. How to iterate through a vector of vectors?

6. Vectors Within Vectors

7. vector::pop_back and vector::erase leaking memory?

8. Problem initializing a vector of vectors

9. Launching a Web Form as a Pop Up Box

10. Launching a Form as a pop up type form

11. Pop up MessageBox from C# webform

12. DotNet browse pop context

 

 
Powered by phpBB® Forum Software