Solved

Please validate function...

Posted on 2008-06-16
17
147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

717 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