Vector repeats elements with Classes

Ok, so im having trouble figuring out how to compare elements of a vector to see if there're are any repeats. The idea is that each player has a hand of cards but i need to check the hand to see if there are any duplicates in each hand. There're are 116 cards so there can be some repeats.
Now, what is confusing me is that everything is in a class and im not sure what statement i should put in main to have it compare each element of each players hand. Hand for each player is stored into a vector.
I overloaded the == already. Yes, im required to have that.
I get how to compare scores (player1.getValue() < player2.getValue) but how would i compare if a player has a repeat of one card (statement == statement)
Everything i use just gives me a red line under it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  #include <string>
#include <algorithm>
#include <iterator>
#include <utility>
#include <iostream>
#include <vector>
#include "Card.h"
using namespace std;


int main()
{

	Hand player;
	//vector<vector<Card>> dealtcards;
	CardDeck playingCards;
	int num;

	cout << playingCards.toString();

	system("pause");
	system("CLS");

	playingCards.shuffle();

	cout << playingCards.toString();
	system("pause");
	system("CLS");

	
	Hand player1, player2, player3, player4, player5, player6, player7;
	//vector<Hand> playersHand;

	cout << "Enter number of players: ";
	cin >> num;

	if (num < 2 || num > 7)
	{
		cout << "Enter a valid amount of players (2-7): ";
		cin >> num;
	}
	
	if (num == 2)
	{
		//for (int i = 0; i < num; i++)
		//{
		while (player1.numCards() < 9)
		{
			//for (int j = 0; j < 9; j++)
			//{
				player1.addCard(playingCards.deal());
				player2.addCard(playingCards.deal());
			//}
		}
		//}
		cout << "Player 1 Hand: \n" << endl;
		cout << player1.toString() << "for a total of " << player1.getValue() << "\n" << endl;
		cout << "Player 2 Hand: \n" << endl;
		cout << player2.toString() << "for a total of " << player2.getValue() << "\n" << endl;
	

	}
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  #include "stdafx.h"
#include "Card.h"
#include <ctime>
#include <iostream>
using namespace std;

Card::Card(Face faceArg, Suit suitArg)
	:
	faceValue(faceArg),
	suitValue(suitArg)
{	}
int Card::getValue()
{
	switch (faceValue) {
	case joker: 
		return 20;
	default:
		return faceValue;
	}
}
Face Card::getFace()
{
	return faceValue;
}
Suit Card::getSuit()
{
	return suitValue;
}
string Card::toString()		//turning each enum into a string and saving it all to the CardAsString.
{

	switch (faceValue)
	{
	case three: CardAsString = "Three of ";
		break;
	case four: CardAsString = "Four of ";
		break;
	case five: CardAsString = "Five of ";
		break;
	case six: CardAsString = "Six of ";
		break;
	case seven: CardAsString = "Seven of ";
		break;
	case eight: CardAsString = "Eight of ";
		break;
	case nine: CardAsString = "Nine of ";
		break;
	case ten: CardAsString = "Ten of ";
		break;
	case jack: CardAsString = "Jack of ";
		break;
	case queen: CardAsString = "Queen of ";
		break;
	case king: CardAsString = "King of ";
		break;
	}
	switch (suitValue)
	{
	case diamonds: CardAsString += "Diamonds";
		break;
	case spades: CardAsString += "Spades";
		break;
	case hearts: CardAsString += "Hearts";
		break;
	case stars: CardAsString += "Stars";
		break;
	case clubs: CardAsString += "Clubs";
		break;
	}
	if (faceValue == joker)  //checking for a joker and returning only a joker, no suit
	{
		CardAsString = "Joker";
	}

	return CardAsString;   // returning everything as a string
}
bool operator<(Card &first, Card &second)
{
	if (first.getValue() < second.getValue())
	{
		return true;
	}
	return false;
}
bool operator==(Card &first, Card &second)
{
	if (first.getFace() == second.getFace() && first.getSuit() == second.getSuit())
	{
		return first.getFace() + first.getSuit();
	}
	return false;

}
ostream &operator<<(ostream &out, Card &c)
{
	return out << c.face();
}
Hand::Hand()
{

}
int Hand::addCard(Card newCard)
{
	cardHand.push_back(newCard);
	return cardHand.size();
}

int Hand::getValue()
{
	int value = 0;
	for (unsigned i = 0; i < cardHand.size(); i++)
		value += cardHand[i].getValue();
	return value;
}

int Hand::numCards()
{
	return cardHand.size();
}

string Hand::toString()
{
	//print out the deck
	int i = 0 + 1;
	string hand = "Players Hand: \n";
	for (auto&& A : cardHand) {
		hand += A.toString() + "\n";
	}
	return hand;
}
CardDeck::CardDeck()
{
	srand(unsigned int(time(NULL)));

	for (unsigned int suitValue = 0; suitValue <= 4; suitValue++)  // cycling through the values and pushing them into vector
	{
		for (unsigned int faceValue = 3; faceValue <= 13; faceValue++)
		{
			Card adding = Card(static_cast<Face>(faceValue), static_cast<Suit>(suitValue));
			deckOfCards.push_back(adding);  //pushing them in twice, since there are doubles 
			deckOfCards.push_back(adding);
		}
	}
	// making sure only 6 jokers make it into the deck
	for (int i = 0; i < 6; i++) {
		Card adding = Card(joker, none);
		deckOfCards.push_back(adding);
	}

}
Card CardDeck::deal()
{
	int i = rand() % deckOfCards.size();
	Card cardDrawn = deckOfCards[i];
	deckOfCards.erase(deckOfCards.begin() + i);
	return cardDrawn;
}

int CardDeck::numCards()
{
	return deckOfCards.size();
}

int CardDeck::addCard(Card newCard)
{
	deckOfCards.push_back(newCard);
	return deckOfCards.size();
}

string CardDeck::toString() //prints deck
{
	//print out the deck
	string deck = "Deck of cards: \n";
	for (auto&& A : deckOfCards) {
		deck += A.toString() + "\n";
		
	}
	return deck;
}

void CardDeck::shuffle()
{
	for (unsigned i = 0; i < deckOfCards.size(); i++) {
		int randomIndex = rand() % deckOfCards.size();
		swap(deckOfCards[i], deckOfCards[randomIndex]);

	}
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #include <string>
#include <vector>
#include <iostream>
using namespace std;

enum Face { three = 3, four, five, six, seven, eight, nine, ten, jack, queen, king, joker };
enum Suit { diamonds, spades, hearts, stars, clubs, none };

class Card {
	friend bool operator<(const Card &first, const Card &second);
	friend bool operator==(const Card &first, const Card &second);
	friend ostream &operator<<(ostream &out, const Card &c);
public:

	Card(Face face, Suit suit);  //constructor 
	int getValue();
	Face getFace();
	Suit getSuit();
	string toString();
	string CardAsString;	// storing the face and suit as a string
	string face() const { return CardAsString; } //returning the face and suit as a string in the overload<< so it can be shuffled
private:
	Face faceValue;
	Suit suitValue;
};

class CardDeck {
private:
	std::vector<Card> deckOfCards; //store the all the cards into a vector 
public:
	CardDeck();
	Card deal();
	int numCards();
	int addCard(Card newCard);
	string toString();
	void shuffle();
};
class Hand {
	vector<Card> cardHand;
public:
	Hand();
	int addCard(Card newCard);
	int getValue();
	int numCards();
	string toString();
};
Last edited on
should just be hand[i]==hand[i] if the == operator has been overloaded. or else you would have to use the . (dot) operator to access which variables you want to compare.

What is the exact error you are getting?
Are you saying the hand[i]==hand[i] should be in the operator== or should i put that in main.

What i need to happen to print the deck
Shuffle and print the deck again
Then display each players hand
Compare each players hand to see if they have an duplicates in there own individual hand.

The operator== is fine it never gives an error, just when i try to put a statement in main (right after the players hand is displayed ) to compare each individual card in the hand. Im not sure what function i would put in there since each time i try to access the vector for the hand it wont let me there even if i use the dot. Everything i think of to set equal to so i can compare in main(), It always says i need a pointer.

Do you think i should do something like this then call it using the dot in the main
1
2
3
4
string Hand::getSearch()
{
    //Put something here to cycle through the vector
}
Last edited on
how to compare elements of a vector to see if there're are any repeats.

std::sort() the vector : http://en.cppreference.com/w/cpp/algorithm/sort
then check if std::unique(vector.begin(), vector.end()) == vector.end();
http://en.cppreference.com/w/cpp/algorithm/unique
Topic archived. No new replies allowed.