# Searching a vector of objects for pairs

• May 16th, 2013, 01:35 PM
azeotrope
Searching a vector of objects for pairs
Hello all,

Say I have a vector of objects and I want to return an object based on a pair of strings. The strings can be in either order Ie; A B==B A.

In general, what do you think is the best way to do this?

Thanks,
T.
• May 16th, 2013, 02:32 PM
Paul McKenzie
Re: Searching a vector of objects for pairs
Quote:

Originally Posted by azeotrope
In general, what do you think is the best way to do this?

There can be varying opinion on what is the "best way".

http://www.cplusplus.com/reference/algorithm/find_if/

Regards,

Paul McKenzie
• May 16th, 2013, 02:39 PM
S_M_A
Re: Searching a vector of objects for pairs
I think you have to provide more details to get a serious answer.

Edit: Sorry Paul, was typing a response but changed my mind before posting so your response was not accounted for.
• May 16th, 2013, 02:42 PM
GCDEF
Re: Searching a vector of objects for pairs
Quote:

Originally Posted by S_M_A
I think you have to provide more details to get a serious answer.

I agree, the question doesn't make much sense. Sounds to me like you may want a map.
• May 17th, 2013, 05:58 PM
soudlog1971
Re: Searching a vector of objects for pairs
You may want to consider a sorted vector or a map as stated above.
• May 17th, 2013, 06:37 PM
nuzzle
Re: Searching a vector of objects for pairs
Quote:

Originally Posted by azeotrope
Say I have a vector of objects and I want to return an object based on a pair of strings. The strings can be in either order Ie; A B==B A.

Well, if you search the vector from begin to end you'll have O(N) complexity.

But if you introduce an extra unordered_map you can get O(1) accesses into the vector.