Help Finding Error In std::find code

Discussion in 'C++' started by Adam Teasdale Hartshorne, Aug 14, 2005.

  1. I would be extremely grateful if somebody could tell me what as I
    getting wrong with this little bit of code to find the index of a
    particular element in a std::vector

    std::vector<int> allVertices ;
    getTriangleVertices(t[0], allVertices) ; //Fills the allVertices vector
    getTriangleVertices(t[1], allVertices) ;

    print("All Vertices", allVertices) ;

    std::vector<int> tempSpareVertex (getUniqueElements(allVertices)) ;
    print("Temp Spare Vertices", tempSpareVertex) ;

    std::vector<int>::const_iterator it (std::find(allVertices.begin(),
    allVertices.end(), tempSpareVertex[0]));

    int index = it - allVertices.begin() ;
    printf("Spare Vertex 0 Position:- %i \n", index) ;

    and the debug shows things like this

    All Vertices: 72 6 50 50 6 46
    Temp Spare Vertices: 46 72
    Spare Vertex 0 Position:- 2

    which is clearly the wrong index.


    Adam
    Adam Teasdale Hartshorne, Aug 14, 2005
    #1
    1. Advertising

  2. "Adam Teasdale Hartshorne" <> wrote in message
    news:ddo3ht$dao$...
    > I would be extremely grateful if somebody could tell me what as I
    > getting wrong with this little bit of code to find the index of a
    > particular element in a std::vector
    >
    > std::vector<int> allVertices ;
    > getTriangleVertices(t[0], allVertices) ; //Fills the allVertices vector
    > getTriangleVertices(t[1], allVertices) ;
    >
    > print("All Vertices", allVertices) ;
    >
    > std::vector<int> tempSpareVertex (getUniqueElements(allVertices)) ;
    > print("Temp Spare Vertices", tempSpareVertex) ;
    >
    > std::vector<int>::const_iterator it (std::find(allVertices.begin(),
    > allVertices.end(), tempSpareVertex[0]));
    >
    > int index = it - allVertices.begin() ;
    > printf("Spare Vertex 0 Position:- %i \n", index) ;
    >
    > and the debug shows things like this
    >
    > All Vertices: 72 6 50 50 6 46
    > Temp Spare Vertices: 46 72
    > Spare Vertex 0 Position:- 2
    >
    > which is clearly the wrong index.
    >
    >
    > Adam


    Please post a working piece of code, clearly the code you posted is not
    valid. Here is
    a working demonstration that std::find is working correctly for the data you
    have given:



    int main(int argc, char* argv[])
    {
    std::vector<int> allVertices ;
    std::vector<int> tempSpareVertex;
    allVertices.push_back(72);
    allVertices.push_back(6);
    allVertices.push_back(50);
    allVertices.push_back(50);
    allVertices.push_back(6);
    allVertices.push_back(46);


    tempSpareVertex.push_back(46);
    tempSpareVertex.push_back(72);

    vector<int>::const_iterator it (std::find(allVertices.begin(),
    allVertices.end(), tempSpareVertex[0] ));


    int index = it - allVertices.begin() ;
    printf("Spare Vertex 0 Position:- %i \n", index) ; // prints 5 like we
    expected.


    return 0;
    }
    Dave Townsend, Aug 14, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Jansson
    Replies:
    5
    Views:
    6,275
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    347
    Jim Langston
    Jul 7, 2005
  3. Vinu
    Replies:
    0
    Views:
    345
  4. Geoffrey S. Knauth
    Replies:
    6
    Views:
    983
    Earl Purple
    Jan 18, 2006
  5. petry

    std::string::find vs std::find

    petry, Jul 5, 2009, in forum: C Programming
    Replies:
    1
    Views:
    341
    petry
    Jul 5, 2009
Loading...

Share This Page