Solved

Please validate function...

Posted on 2008-06-16
17
141 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

21 Experts available now in Live!

Get 1:1 Help Now