?
Solved

Please validate function...

Posted on 2008-06-16
17
Medium Priority
?
150 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 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

862 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