473,549 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ;
getTriangleVert ices(t[0], allVertices) ; //Fills the allVertices vector
getTriangleVert ices(t[1], allVertices) ;

print("All Vertices", allVertices) ;

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

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

int index = it - allVertices.beg in() ;
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 2199

"Adam Teasdale Hartshorne" <ad**@dcs.warwi ck.ac.uk> wrote in message
news:dd******** **@mail.dcs.war wick.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 ;
getTriangleVert ices(t[0], allVertices) ; //Fills the allVertices vector
getTriangleVert ices(t[1], allVertices) ;

print("All Vertices", allVertices) ;

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

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

int index = it - allVertices.beg in() ;
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.pus h_back(72);
allVertices.pus h_back(6);
allVertices.pus h_back(50);
allVertices.pus h_back(50);
allVertices.pus h_back(6);
allVertices.pus h_back(46);
tempSpareVertex .push_back(46);
tempSpareVertex .push_back(72);

vector<int>::co nst_iterator it (std::find(allV ertices.begin() ,
allVertices.end (), tempSpareVertex[0] ));
int index = it - allVertices.beg in() ;
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
12804
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 function find() and I have to use std::find(). The linker fails on unsatisified symbols in find(). I think this is because find() is a template...
3
1807
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 headers the implementation is: template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& value)
2
19023
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 occasion to experience a failure and got a code of 2??? Would like to know just what that means. Here's how I call the fn... Result =...
18
7176
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 <bitset> using std::vector;
2
4505
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
1972
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 encountered: "Error is processing file in pickup directory.". The specific error code was 0xC00402CE.
8
8622
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
5798
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... Dim sql As String = "SELECT * FROM SB_ADWarning" Dim strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\SB_2000.mdb" Dim Con...
1
5893
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 the fields like first name,last name,Date of birth, Phone Number ,mobile number and address... 1)If i enter all the fields then only the values...
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7957
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.