Getting last element from vector

May 13, 2009 at 10:39am
link_Node *pNode,*tempsegnode;; // here link_node is a class
vector<link_Node*> *pList;

pNode = (*pList)[i];

i want to find that a pNode is last in pList and if so i have to take some corrective measure.
i tried using end() API but not able to make it working

can someone help how to find last element from pList and then compare pNode and last element.
May 13, 2009 at 10:56am
i had just started working in C++ and started modifying a code which make use of vectors.
please help me out with this.

link_Node *pNode,*tempsegnode;; // here link_node is a class
vector<link_Node*> *pList;

pNode = (*pList)[i];

i want to find that a pNode is last in pList and if so i have to take some corrective measure.
i tried using end() API but not able to make it working

in other words i need to know what is last element in vector list pList and compare with pNode and if they r same i need to do some processing.

can someone help how to find last element from pList and then compare pNode and last element.
May 13, 2009 at 11:08am
closed account (z05DSL3A)
I assume you want something along these lines:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
//-----------------------------------------------------------------------------
class link_Node 
{
    // Some stuff
};
//-----------------------------------------------------------------------------
int main() 
{
    link_Node *pNode       = 0;
    link_Node *tempsegnode = 0;

    std::vector< link_Node* > *pList;

    pList = new std::vector< link_Node* >;

    pList->push_back(new link_Node);

    pNode = (*pList)[0];

    if(pNode == pList->back())
    {
        std::cout <<"at end" << std::endl;
    }
    else
    {
        std::cout <<"not at end" << std::endl;
    }

    pList->push_back(new link_Node);

    if(pNode == pList->back())
    {
        std::cout <<"at end" << std::endl;
    }
    else
    {
        std::cout <<"not at end" << std::endl;
    }

    // TODO: Clean up code...

    return 0;
}

/* End of file ***************************************************************/


PS: Only post your question in one forum.
Last edited on May 13, 2009 at 11:09am
May 13, 2009 at 11:11am
Thanks for your reply.

i did the same thing

link_Node *pSegNode,*temppp;
temppp = (*(pSegList)->end()-1);



printf(" name is %s",temppp->sGetName().c_str());

but i m getting the segmentation fault.

am i missing something here?
May 13, 2009 at 11:39am
closed account (z05DSL3A)
temppp = *(pSegList->end()-1); ??
May 13, 2009 at 2:34pm
As Grey Wolf demonstrated in the sample code:

vector::back() will return a direct reference to the last element in your vector
http://www.cplusplus.com/reference/stl/vector/back/

vector::end() will return an iterator to the past-the-end element in your vector
http://www.cplusplus.com/reference/stl/vector/end/
Topic archived. No new replies allowed.