How to search two vectors to find a common object?

Feb 15, 2017 at 4:58am
I have two vectors but and i want to search both vectors to make sure there are no duplicate items in the vectors. Do i do this the same way i would search a 2d array? for row for coloumn?
Feb 15, 2017 at 5:03am
1
2
3
4
5
for(int i = 0; i< vector1.size(); i++){
if(vector1[index] == vector2[index]){
//do something
}
}
Feb 15, 2017 at 5:24am
omg i feel so stupid
Feb 15, 2017 at 5:25am
What if the vectors aren't always the same size?
Feb 15, 2017 at 5:35am
You might want to look into a set rather than a vector, sets are made to only hold unique items. http://www.cplusplus.com/reference/set/set/ (also look into the unordered set since it's technically faster)

However, for the vector you actually have to use a nested for loop to make sure that you check each element of the first vector against each element of the second one.

Last edited on Feb 15, 2017 at 5:36am
Feb 15, 2017 at 5:39am
Make a for loop and compare it with the size of the second vector.
Feb 15, 2017 at 6:16am
i want to search both vectors to make sure there are no duplicate items in the vectors

it is unclear what exactly you want - each vector cannot have duplicates and/or no item in one can be present in the other
Feb 15, 2017 at 5:02pm
It sounds like i may need a set if they can be used as vectors.

Let me clarify what I'm trying to do. I'm writing a card game and i want to test to see if any of the vectors have the same card once the deck has been shuffled and passed to the number of players. So if there are two players i want to make sure they dont have any same card and same with three players (compare three vectors or sets) and the same with four players.

Hope that cleared it up! I can supply the code i have but i dont think it will help too much since i havent written any code for this portion.
Topic archived. No new replies allowed.