vector index.

if i have vector<string>player_0={"DK","DJ","D9","D4","H2"};
and D is for the suit of diamond H is for Heart.
how do i write a for loop to check if the player_0 has all the cards from the same suit?
Last edited on
Get suit of first card.

Look at second card. if same suit as the first, keep going. If not same suit as the first, finish with negative result.

If you get through the whole lot, finish with positive result.

can you give an example? because i have to go through index inside the index so i am confused about it a bit. How should i write the code?
1
2
3
4
5
6
7
8
9
bool are_all_same_suit(vector<string> input)
{
  char suit = input.at(0).at(0);
  for (auto comparison : input)
  {
    if (comparison.at(0) != suit) return false;
  }
return true;
}
1
2
3
4
5
6
7
8
9
bool all_cards_of_same_suit( const std::vector<std::string>& cards )
{
    if( cards.empty() ) return true ;

    const char suit = cards[0][0] ; // note: can read char at position zero even if the string is empty

    // http://en.cppreference.com/w/cpp/algorithm/all_any_none_of
    return std::all_of( cards.begin()+1, cards.end(), [suit] ( const auto& str ) { return str[0] == suit ; } ) ;
}

http://coliru.stacked-crooked.com/a/c1f583cd1d0f4c5d
Topic archived. No new replies allowed.