C++ Find Frequency of a String in a Vector of Strings

I am currently working on a little mini program that will determine if a vector of 5 strings contains a full house. A full house in my program has a pair of cards and 3 of a kind.

For example: ["A", "A", "A", "K", "K"] would be a full house, while ["10", "J", "10", "10", "10"] would not be one.

I have written my main function such that a user can read the card values into a vector with this code below:

int main()
{
    vector<string> hand;
    string input;

    for (int i = 0; i < 5; i++)
    {
        cout << "Card " << i + 1 << ": ";
        cin >> input;

        hand.push_back(input);
    }
}

I would like to write a bool function that takes the vector as a parameter and returns true if the vector contains a full house, and false if it does not. My problem is that I am not sure of an efficient way of looping through the vector in the function and finding the frequency of each string to determine if the vector has a full house.

For example, I would like my function to be somewhat like the one below:

bool isFullHouse(vector<string> hand)
{
     // loop through vector
     // record instance of string in vector
     // determine if the string has appeared either 2 times or 3 times in the vector

     // if it contains a full house
     return true

     // else
     return false
}

Does anyone have a decent way of achieving this task?


Read more here: https://stackoverflow.com/questions/64902993/c-find-frequency-of-a-string-in-a-vector-of-strings

Content Attribution

This content was originally published by Parker Harrelson at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: