Help Finding Error In std::find code

  • Thread starter Adam Teasdale Hartshorne
  • Start date
A

Adam Teasdale Hartshorne

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
 
D

Dave Townsend

Adam Teasdale Hartshorne said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top