473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help Finding Error In std::find code

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
Aug 14 '05 #1
1 2189

"Adam Teasdale Hartshorne" <ad**@dcs.warwick.ac.uk> wrote in message
news:dd**********@mail.dcs.warwick.ac.uk...
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;
}

Aug 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: zhou | last post by:
Hi there, We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes when I try using std:find() on vector. Since vector has no member...
3
by: Old Wolf | last post by:
In the documentation for std::find, it says: find returns the first iterator i in the range [first, last) for which the following condition holds: *i == value. However in my compiler's...
2
by: MLH | last post by:
I cut a mail function off the m'soft site. Has always worked. However, I would like to include error codes returned by the sendmail Fn and be able to understand what they mean. I had my first...
18
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
2
by: Pucca | last post by:
Where can I find pre-defined Error code like OBJECT_ALREADY_EXISTS (HRESULT) for the DirectoryServicesCOMException.ErrorCode? I need it for the try-catch coding. Thanks.
0
Yoosha
by: Yoosha | last post by:
Hi, I have this error to send App. information & Err. inforamtion with ErrorReport.dll(Private App.). IIS 5.0 VB 6.0 Error: Unable to deliver this message because the follow error was...
8
by: brekehan | last post by:
If I have a class MyClass { ...bunch o data and methods int x; }; and a stl container of MyClass objects Is there a way to use std::find to get all the elements whose member x
4
by: leaf0209 | last post by:
Hi, I am new to this forum and have been troubled by this problem for the past few days. I would appreciate if someone would take a look and help me out. I am using vb express 2005 edition... ...
1
by: jhansi | last post by:
hi... I have created a form it's working properly.... but i want to consider phone number and STD code in two different text field for a single lable phone number..... and I have considered...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.