Solved

Please validate function...

Posted on 2008-06-16
17
142 Views
Last Modified: 2010-04-21
Please have a look over the following function. I've tested it and it seems fine, but it would be nice just to make sure...

Thanks,
Uni
/*********************************************************************************

This returns true if the needle was found in the haystack and the

index is given in the aFoundIndex parameter. If the needle wasn't

found then the method returns false and aFoundIndex is undefined.

This method is bound safe.

*********************************************************************************/

bool findNeedleInHaystack(vector<BIT> &aHaystack, unsigned int aStartIndex, vector<BIT> &aNeedle, unsigned int &aFoundIndex){

	if(aStartIndex>=aHaystack.size())

		return false;

	vector<BIT>::iterator searchResult=search(aHaystack.begin()+aStartIndex, aHaystack.end(), aNeedle.begin(), aNeedle.end());

	if(searchResult==aHaystack.end())

		return false;

	aFoundIndex=searchResult-aHaystack.begin();

	return true;

}

/********************************************************************************/

Open in new window

0
Comment
Question by:Unimatrix_001
  • 9
  • 4
  • 4
17 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 100 total points
ID: 21796334
That looks good, yes. So, you've gone with indexes instead of iterators then :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 21796354
Cheers, yeah - the iterators were causing all sorts of problems. Shame because I'd have liked to keep it pure STL without resorting to indexes. Clearly though as the other question shows it's not going to work without a nasty fight... Oh well. :)

Thanks again,
Uni
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21796439
Heh :) Good old indexes never let you down eh :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 21796981
They're strong (and as pretty) as a rock... :)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21796994
Now you're getting all poetic ... what's up today ? ;)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 21797024
Haha, I don't know what you mean... now where did I leave that Shakespeare sonnet volume...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21797054
/me hides ... This is getting too much for me lol
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 21797073
:D
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:evilrix
ID: 21797951
I don't mean to be picky, but I'm gonna :)

You are passing in unsigned int, really the correct type should be vector<BIT>::size_type and the reason for this is that size_type might have a different value on different platforms. For example, on a 64 bit platform it might be a 64 bit type (same as size_t). By using unsigned int you might not be able to address the full range.

Like I said, I'm being picky :)
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31467713
Sorry, just realised I hadn't closed this question!
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 21797963
Hiya rix, yeah I know what you mean although this application is purely for me, so I'm not too worried, although I'll bare that in mind...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21797975
No worries... I couldn't find anything else to object to :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 21797985
:)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21797992
Come on Uri... post more questions... I'm bored and EE is quiet tonight :)

*slap* :-s
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 21798011
Hahahaha! :D I'm all out of them at the mo... trying to figure out why something silly isn't working... :( Try your hand at... erm I dunno *looks at profile*... hardware?... or give yourself something to do for work tomorrow and create a uber-virus? ;)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21798029
uber-virus, you know that wouldn't not go down to well with my employer :)

I'm currently working on my CV atm... actually, more correctly my Wife is currently working on my CV!

I'm off to be in 5 anyway, have a good night my friend, until the next time.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 21798049
>>uber-virus, you know that wouldn't not go down to well with my employer :)
Think of it more of a form as job security... ;)

>>I'm off to be in 5 anyway, have a good night my friend, until the next time.
Catch you later mate... I'm planning on knocking off about now (I said that an hour ago though!) as well. Don't want to run past midnight if I can help it...

Laters. :)
Uni
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now