View Full Version : c++ for-in loop?
Trinithis
11-24-2007, 07:55 PM
Is there a c++ for-in loop? If so, what is the syntax?
tricolaire
11-24-2007, 08:09 PM
I don't know about for-in, but there is a for-each loop, things like that are found in algorithm.h
what behavior are you trying to get?
Trinithis
11-24-2007, 08:28 PM
I want to iterate through a typesafe enum.
This case, I have Rank and Suit as an nested classes of Card, and I want to have a card array serving as a deck holding each combination of Rank and Suit in a unique Card without having to write 52 lines of code.
I want something like:
//psuedo code
Card* cards[52] = {};
int i = 0;
for(rank in Card::Rank)
for(suit in Card::Suit)
cards[i++] = new Card(rank, suit);
Here's the Card.h and Card.cpp:
#ifndef CARD_H_
#define CARD_H_
class Card {
public:
class Suit {
private:
Suit(std::string name);
const std::string name;
public:
static const Suit* DIAMONDS;
static const Suit* CLUBS;
static const Suit* HEARTS;
static const Suit* SPADES;
std::string toString() const;
};
class Rank {
private:
Rank(std::string name);
const std::string name;
public:
static const Rank* TWO;
static const Rank* THREE;
static const Rank* FOUR;
static const Rank* FIVE;
static const Rank* SIX;
static const Rank* SEVEN;
static const Rank* EIGHT;
static const Rank* NINE;
static const Rank* TEN;
static const Rank* JACK;
static const Rank* QUEEN;
static const Rank* KING;
static const Rank* ACE;
std::string toString() const;
};
Card(const Rank* rank, const Suit* suit);
const Rank* rank() const;
const Suit* suit() const;
std::string toString() const;
private:
const Rank* _rank;
const Suit* _suit;
};
#include "Card.cpp"
#endif
#include "Card.h"
Card::Suit::Suit(std::string name): name(name) {}
const Card::Suit* Card::Suit::DIAMONDS = new Card::Suit("diamonds");
const Card::Suit* Card::Suit::CLUBS = new Card::Suit("clubs");
const Card::Suit* Card::Suit::HEARTS = new Card::Suit("hearts");
const Card::Suit* Card::Suit::SPADES = new Card::Suit("spades");
inline std::string Card::Suit::toString() const {
return name;
}
Card::Rank::Rank(std::string name): name(name) {}
////////////////////////////////////////////////////////////////
const Card::Rank* Card::Rank::TWO = new Card::Rank("two");
const Card::Rank* Card::Rank::THREE = new Card::Rank("three");
const Card::Rank* Card::Rank::FOUR = new Card::Rank("four");
const Card::Rank* Card::Rank::FIVE = new Card::Rank("five");
const Card::Rank* Card::Rank::SIX = new Card::Rank("six");
const Card::Rank* Card::Rank::SEVEN = new Card::Rank("seven");
const Card::Rank* Card::Rank::EIGHT = new Card::Rank("eight");
const Card::Rank* Card::Rank::NINE = new Card::Rank("nine");
const Card::Rank* Card::Rank::TEN = new Card::Rank("ten");
const Card::Rank* Card::Rank::JACK = new Card::Rank("jack");
const Card::Rank* Card::Rank::QUEEN = new Card::Rank("queen");
const Card::Rank* Card::Rank::KING = new Card::Rank("king");
const Card::Rank* Card::Rank::ACE = new Card::Rank("ace");
inline std::string Card::Rank::toString() const {
return name;
}
////////////////////////////////////////////////////////////////
Card::Card(const Rank* rank, const Suit* suit):
_rank(rank), _suit(suit) {}
inline const Card::Rank* Card::rank() const {
return _rank;
}
inline const Card::Suit* Card::suit() const {
return _suit;
}
std::string Card::toString() const {
return _rank->toString() + " of " + _suit->toString();
}
Trinithis
11-24-2007, 10:13 PM
If there is no way to do as I asked, is the following an acceptable (alternate) solution?
Have each of my enum classes contain a private static vector-wrapping object that only allows pushing and read-only access. Then have that class's constructor push each instance into that object. Then when that is all done, have a public static accessor method that returns that vector-wrapped object wrapped in an object that disallows the pushing.
Now I have an iterable typesafe enum . . . ?
Is this flawed and/or are there better ways of doing this?
rpgfan3233
11-26-2007, 06:18 PM
You could use a vector in order to use the for_each algorithm:
http://cplusplus.com/reference/algorithm/for_each.html
Iterators are member types IIRC, so you would need an iterator of that specific type in order to iterate through the values of a variable, whether it is a vector or something else.
Edit: I should note that with a vector, you would no longer have the ability to use Card::Rank::TWO, Card::Rank::THREE, etc. That is the advantage that an enum has over an actual variable.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.